Listen to high-quality music from a secure link

I have an external server URL where I send credentials and an ID to retrieve an audio file, like

Instead of exposing the credentials to the user by calling this URL from JavaScript, I tried to fetch the data from the backend and stream it to the UI request on my server.

using (Stream mystream = httpResponse2.GetResponseStream())
{
    using (BinaryReader reader = new BinaryReader(mystream))
    {
        int length = 2048;
        byte[] buffer = new byte[length];
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.BufferOutput = true;
        response.ContentType = "audio/wav";
        while((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
        {
            response.OutputStream.Write(buffer, 0, bytesRead);
        }
        response.End();
    }
}

Successfully, I can now play the streamed audio in an <audio> element.

However, I am facing some issues:

  • During playback, the seek control bar is always stuck at 0 as the audio length shows as Infinity. This prevents me from using the control slider to move to different parts of the audio.
  • When the audio ends, $("audio")[0].duration returns extremely large numbers like 9188187664790.943 for a 20-30 second audio clip, and the display time also shows strange negative values.
  • I am unable to find a solution that allows seeking into unbuffered areas of the audio.

I'm uncertain if this is the correct or best approach, so any suggestions on alternative methods would be greatly appreciated.

Answer №1

After investigating the problem, I successfully resolved it.

My approach involved examining how my HTTP server handled requests for standard mp3 files that were stored statically on the server. Upon closer inspection, I discovered that the server was sending two headers that I had overlooked - Accept-Ranges: bytes and Content-Length: xxx.

By implementing these headers, all of the issues mentioned in the original question vanished.

I hope this solution proves useful to others facing similar challenges.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

CSS Class Not Updating in WordPress Using JavaScript

Looking to incorporate a small JavaScript code directly into The Twenty Sixteen theme in WordPress. This HTML Code needs to be modified on the Page: <p class="site-description">Example Text</p> The goal is to change a classname: document.g ...

Stable Banner with Automatic Scroll to Sections

I have implemented a script that allows for smooth scrolling to different sections within a webpage when clicking on links in the top navigation menu. In the HTML, I've assigned IDs to each section (section1, section2, section3, etc.) and linked these ...

What is the best way to incorporate a new item into Redux with a nested data structure similar to this? Specifically, I am looking to add new

Seeking assistance with dynamically pushing new items to a deeply nested choice array in order to render react native TextInput components using the map function. Below, you will find details on the data structure, reducer design, and other relevant code s ...

How to extract cookie value in a node.js application using socket.io

How can I retrieve cookie values in a node server using socket.io? Whenever a socket sends a message to the server, I need to check the value of the cookie. However, I only want to obtain the cookie value and not the session cookie as done in Express. r ...

The anchor tag is not being used in the function call in an Angular application

Having trouble with calling the logout function inside the login controller. The setup involves a simple login and logout system using ui-router. After successfully logging in and navigating to another page, I encountered issues with calling the logout fu ...

Material UI Table dynamically updates with Firestore real-time data

My current code aims to update a Material UI table in real-time with data from a Firestore database. I can fetch the data and store it in an array, but when I assign this array to the table data, nothing changes. I've heard that I should use states fo ...

What is the method to retrieve results using 'return' from NeDB in vue.js?

Seeking assistance on retrieving data from NeDB within a method in a .vue file using electron-vue. Currently, I am aware that the data can be stored in a variable, but my preference is to fetch it using 'return' as I intend to utilize the result ...

Is it necessary to exclude a specific route from an ActionFilterAttribute application?

I am working on a MvC .Net web application where I have implemented an action filter attribute for all routes. However, I am facing a problem with excluding a specific route ("/api/ignore") from this filter in WebApiConfig.cs file. How can I achieve this w ...

Adding text after a div in React-JS using Bootstrap: A quick guide

Just starting out with website development and I have a question. As I practice making this website, I am struggling to figure out how to add the text "To know more about us click here" below the 'Get started' button. I tried adding a simple < ...

Use key input to dynamically update state with object in React

Struggling to update my component's state based on key inputs and pass it to another component. I've managed to successfully call keyboardInput() and log the correct values when arrow keys are pressed, but I can't seem to get the returned v ...

If the given response `resp` can be parsed as JSON, then the function `$

I was using this script to check if the server's response data is in JSON format: try { json = $.parseJSON(resp); } catch (error) { json = null; } if (json) { // } else { // } However, I noticed that it returns true when 'res ...

Utilizing Node.js in Phonegap

Currently, I am in the process of creating an iOS application with PhoneGap and I have an interest in incorporating node.js into a specific aspect of it. Is it possible to integrate an instance of node.js within the app using PhoneGap? ...

The encoding of Node.js using npm

Looking to install the validate .json file below using npm: { "name": "node-todo", "version": "0.0.0", "description": "Simple todo application", "main": "server.js", "dependencies": { "express": "~3.4.4", "mongoose": "~ ...

AngularJS binding variables to HTML tag attributes

Currently, I am going through the tutorials and documentation for Angularjs that is provided on the official Angularjs website. One of the examples involves adding a select box for ordering which looks like this: <select ng-model="orderProp"> ...

Steps to implement mandatory field validation for a set of dynamic radio buttons

I am dynamically generating a group of radio buttons within an asp:table control. For example: Qst1 rb1 rb2 rb3 Qyst2 rb1 rb2 rb3 Requirement: Users must select an answer for each question. I intend to use a custom validator for this purpose ...

What is the process by which the browser interprets the Connection String found in the Web.config file

I am encountering an issue with the connection string in my web.config file. It works perfectly fine with my local database, but when trying to connect to my host's database, it fails. Here is the code snippet from my web.config file: <connect ...

Oops! Next JS encountered an unhandled runtime error while trying to render the route. The

I keep receiving the error message Unhandled Runtime Error Error: Cancel rendering route Within my navBar, I have implemented the following function: const userData={ id:1, email: "", name: "", lastName: "", ...

What is the best way to extract specific values from a JSON array of objects using JavaScript?

I am facing some challenges in displaying values from the AJAX objects. My goal is to populate a select box with names using AJAX, but I seem to have made an error somewhere in my code. Can someone please assist me in identifying and correcting the mistake ...

JavaScript: function that operates asynchronously

I am new to JavaScript and encountered some issues with async functions. I have a function that fetches data from MongoDB by creating a promise, but it returns a collection with an empty object. async function getRating(item, applicant) { let arr = await ...

Is it possible to incorporate variables when updating an array or nested document in a mongodb operation?

Within the "myCollection" target collection, there exists a field named "japanese2". This field is an array or an object that contains another object with a property called "japanese2a", initially set to 0 but subject to change. My goal is to update this p ...