{"id":29581,"date":"2020-06-05T21:11:10","date_gmt":"2020-06-05T13:11:10","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=29581"},"modified":"2025-04-03T23:58:14","modified_gmt":"2025-04-03T15:58:14","slug":"mediasource-api-stream-truncated-audio","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/","title":{"rendered":"How to Stream Truncated Audio Using MediaSource API"},"content":{"rendered":"<p>With the <strong><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/MediaSource\" target=\"_blank\" rel=\"noopener noreferrer\">MediaSource API<\/a><\/strong>, you can <strong>generate and configure media streams<\/strong> right in the browser. It allows you to <strong>perform a variety of operations on media data<\/strong> held by media-related HTML tags such as <code>&lt;audio&gt;<\/code> or <code>&lt;video&gt;<\/code>. For instance, you can <strong>mix different streams<\/strong>, <strong>create overlapping media<\/strong>, <strong>lazy load media<\/strong>, and <strong>edit media metrics<\/strong> such as change the volume or the frequency.<\/p>\n<p>In this post, we\u2019ll specifically see how to <strong>stream an audio sample<\/strong> (a truncated MP3 file) with the <strong>MediaSource API<\/strong> right in the browser in order to <strong>pre-show music<\/strong> to your audience. We will cover how to <strong>detect support for the API<\/strong>, how to <strong>connect the HTML media element<\/strong> to the API, how to <strong>fetch the media<\/strong> via Ajax, and finally how to <strong>stream it<\/strong>.<\/p>\n<div class=\"ref-block ref-block--post\" id=\"ref-post-1\">\n\t\t\t\t\t<a href=\"https:\/\/www.hongkiat.com\/blog\/display-audio-transcript\/\" class=\"ref-block__link\" title=\"Read More: How to Display Timed Transcript Alongside Played Audio\" rel=\"bookmark\"><span class=\"screen-reader-text\">How to Display Timed Transcript Alongside Played Audio<\/span><\/a>\n<div class=\"ref-block__thumbnail img-thumb img-thumb--jumbo\" data-img='{ \"src\" : \"https:\/\/assets.hongkiat.com\/uploads\/thumbs\/250x160\/display-audio-transcript.jpg\" }'>\n\t\t\t\t\t\t\t<noscript>\n<style>.no-js #ref-block-post-25614 .ref-block__thumbnail { background-image: url(\"https:\/\/assets.hongkiat.com\/uploads\/thumbs\/250x160\/display-audio-transcript.jpg\"); }<\/style>\n<\/noscript>\n\t\t\t\t\t\t<\/div>\n<div class=\"ref-block__summary\">\n<h4 class=\"ref-title\">How to Display Timed Transcript Alongside Played Audio<\/h4>\n<p class=\"ref-description\">\n\t\t\t\t\t\tAudio transcript is the text version of speech, helpful in providing useful materials like recorded lectures, seminars, etc....\t\t\t\t\t\t<span>Read more<\/span><\/p>\n<\/div>\n<\/div>\n<p>If you want to see in advance what we are up to, have a look at the <strong>source code <a href=\"https:\/\/github.com\/hongkiat\/mediasourceapi\/\" target=\"_blank\" rel=\"noopener noreferrer\">on Github<\/a><\/strong>, or check out the <strong><a href=\"https:\/\/hongkiat.github.io\/mediasourceapi\/\" target=\"_blank\" rel=\"noopener noreferrer\">demo page<\/a><\/strong>.<\/p>\n<figure><a href=\"https:\/\/github.com\/hongkiat\/mediasourceapi\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/mediasource-api-stream-truncated-audio\/mediasource-api.jpg\" width=\"700\" height=\"303\" alt=\"MediaSource API\"><\/a><\/figure>\n<h2>Step 1 \u2013 Create the HTML<\/h2>\n<p>To create the HTML, add an <strong><a href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/HTML\/Element\/audio\" target=\"_blank\" rel=\"noopener noreferrer\"><code>&lt;audio&gt;<\/code><\/a> tag with a <code>controls<\/code> attribute<\/strong> to your page. For backward compatibility, also <strong>add a default error message<\/strong> for users whose browsers don\u2019t support the feature. We will use JavaScript to turn on\/off this message.<\/p>\n<pre>\r\n&lt;audio controls&gt;\r\nYour browser doesn't support HTML audio element.\r\n&lt;\/audio&gt;\r\n<\/pre>\n<h2>Step 2 \u2013 Detect browser support<\/h2>\n<p>In JavaScript, create a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/try...catch\" target=\"_blank\" rel=\"noopener noreferrer\"><code>try\u2026catch<\/code> block<\/a> that will <strong>throw an error<\/strong> if the <strong>MediaSource API is not supported<\/strong> by the user\u2019s browser, or, with other words if <code>MediaSource<\/code> (the key) <strong>does not exist<\/strong> in the <code>window<\/code> object.<\/p>\n<pre>\r\ntry {\r\n  if (!'MediaSource' in window)\r\n    throw new ReferenceError('There is no MediaSource property in window object.')\r\n} catch (e) {\r\n  console.log(e);\r\n}\r\n<\/pre>\n<h2>Step 3 \u2013 Detect MIME support<\/h2>\n<p>After the support check, also check for the <strong>support of the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Basics_of_HTTP\/MIME_types\" target=\"_blank\" rel=\"noopener noreferrer\">MIME type<\/a><\/strong>. If the MIME type of the media you want to stream is not supported by the browser, <strong>alert the user<\/strong> and <strong>throw an error<\/strong>.<\/p>\n<pre>\r\nvar mime = 'audio\/mpeg';\r\nif (!MediaSource.isTypeSupported(mime)) {\r\n  alert('Can not play the media. Media of MIME type ' + mime + ' is not supported.');\r\n  throw ('Media of type ' + mime + ' is not supported.');\r\n}\r\n<\/pre>\n<p>Note that the code snippet above needs to be <strong>placed inside the <code>try<\/code> block<\/strong>, before the <code>catch<\/code> block (for reference, follow the line numbering or check out the <a href=\"https:\/\/github.com\/hongkiat\/mediasourceapi\/blob\/master\/script.js\" target=\"_blank\" rel=\"noopener noreferrer\">final JS file<\/a> on Github).<\/p>\n<h2>Step 4 \u2013 Link the <code>&lt;audio&gt;<\/code> tag to the MediaSource API<\/h2>\n<p>Create a new <code>MediaSource<\/code> object, and <strong>assign it as the source of the <code>&lt;audio&gt;<\/code> tag<\/strong> by using the <strong><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/URL\/createObjectURL\" target=\"_blank\" rel=\"noopener noreferrer\"><code>URL.createObjectURL()<\/code><\/a><\/strong> method.<\/p>\n<pre>\r\nvar audio = document.querySelector('audio'),\r\nmediaSource = new MediaSource();\r\naudio.src = URL.createObjectURL(mediaSource);\r\n<\/pre>\n<h2>Step 5 \u2013 Add a <code>SourceBuffer<\/code> object to <code>MediaSource<\/code><\/h2>\n<p>When a HTML media element <strong>accesses a media source<\/strong> and is ready to <strong>create <code>SourceBuffer<\/code> objects<\/strong>, the MediaSource API <strong>fires a <code>sourceopen<\/code> event<\/strong> .<\/p>\n<p>The <strong><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/SourceBuffer\" target=\"_blank\" rel=\"noopener noreferrer\"><code>SourceBuffer<\/code><\/a><\/strong> object <strong>holds a chunk of media<\/strong> that is eventually decoded, processed and played. A single <code>MediaSource<\/code> object can <strong>have multiple <code>SourceBuffer<\/code> objects<\/strong>.<\/p>\n<p>Inside the <strong>event handler of the <code>sourceopen<\/code> event<\/strong>, add a <code>SourceBuffer<\/code> object to <code>MediaSource<\/code> with the <strong><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/MediaSource\/addSourceBuffer\" target=\"_blank\" rel=\"noopener noreferrer\"><code>addSourceBuffer()<\/code><\/a><\/strong> method.<\/p>\n<pre>\r\nmediaSource.addEventListener('sourceopen', function() {\r\n  var sourceBuffer = this.addSourceBuffer(mime);\r\n});\r\n<\/pre>\n<h2>Step 6 \u2013 Fetch the media<\/h2>\n<p>Now that you have a <code>SourceBuffer<\/code> object, it\u2019s time to <strong>fetch the MP3 file<\/strong>. In our example, we\u2019ll do so by <strong>using an AJAX request<\/strong>.<\/p>\n<p>Use <code>arraybuffer<\/code> as <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/XMLHttpRequest\/responseType\" target=\"_blank\" rel=\"noopener noreferrer\"><code>responseType<\/code><\/a>, which <strong>denotes binary data<\/strong>. When the response is successfully fetched, <strong>append it to <code>SourceBuffer<\/code><\/strong> with the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/SourceBuffer\/appendBuffer\" target=\"_blank\" rel=\"noopener noreferrer\"><code>appendBuffer()<\/code><\/a> method.<\/p>\n<pre>\r\nmediaSource.addEventListener('sourceopen', function() {\r\n  var sourceBuffer = this.addSourceBuffer(mime);\r\n  var xhr = new XMLHttpRequest;\r\n  xhr.open('GET', 'sample.mp3');\r\n  xhr.responseType = 'arraybuffer';\r\n  xhr.onload = function() {\r\n    try {\r\n      switch (this.status) {\r\n        case 200:\r\n          sourceBuffer.appendBuffer(this.response);\r\n          break;\r\n        case 404:\r\n          throw 'File Not Found';\r\n        default:\r\n          throw 'Failed to fetch the file';\r\n      }\r\n    } catch (e) {\r\n      console.error(e);\r\n    }\r\n  };\r\n  xhr.send();\r\n});\r\n<\/pre>\n<h2>Step 7 \u2013 Indicate the end of the stream<\/h2>\n<p>When the API has finished appending the data to <code>SourceBuffer<\/code> an <strong>event called <code>updatend<\/code> is fired<\/strong>. Inside an event handler, call the <strong><code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/MediaSource\/endOfStream\" target=\"_blank\" rel=\"noopener noreferrer\">endOfStream()<\/a><\/code><\/strong> method of <code>MediaSource<\/code> to <strong>indicate that the stream has ended<\/strong>.<\/p>\n<pre>\r\nmediaSource.addEventListener('sourceopen', function() {\r\n  var sourceBuffer = this.addSourceBuffer(mime);\r\n  var xhr = new XMLHttpRequest;\r\n  xhr.open('GET', 'sample.mp3');\r\n  xhr.responseType = 'arraybuffer';\r\n  xhr.onload = function() {\r\n    try {\r\n      switch (this.status) {\r\n        case 200:\r\n          sourceBuffer.appendBuffer(this.response);\r\n          sourceBuffer.addEventListener('updateend', function (_){\r\n            mediaSource.endOfStream();\r\n          });\r\n          break;\r\n        case 404:\r\n          throw 'File Not Found';\r\n        default:\r\n          throw 'Failed to fetch the file';\r\n      }\r\n    } catch (e) {\r\n      console.error(e);\r\n    }\r\n  };\r\n  xhr.send();\r\n});\r\n<\/pre>\n<h2>Step 8 \u2013 Truncate the media file<\/h2>\n<p>The <code>SourceBuffer<\/code> object has <strong>two properties<\/strong> called <strong><code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/SourceBuffer\/appendWindowStart\" target=\"_blank\" rel=\"noopener noreferrer\">appendWindowStart<\/a><\/code><\/strong> and <strong><code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/SourceBuffer\/appendWindowEnd\" target=\"_blank\" rel=\"noopener noreferrer\">appendWindowEnd<\/a><\/code><\/strong> representing the <strong>start and end time of the media data<\/strong> you want to filter. The highlighted code below <strong>filters the first four seconds<\/strong> of the MP3.<\/p>\n<pre>\r\nmediaSource.addEventListener('sourceopen', function() {\r\n  var sourceBuffer = this.addSourceBuffer(mime);\r\n  sourceBuffer.appendWindowEnd = 4.0;\r\n  ...\r\n});\r\n<\/pre>\n<h2>Demo<\/h2>\n<p>And that\u2019s all, our <strong>audio sample is streamed<\/strong> right from the web page. For the <strong>source code<\/strong>, have a look at our <strong><a href=\"https:\/\/github.com\/hongkiat\/mediasourceapi\/\" target=\"_blank\" rel=\"noopener noreferrer\">Github repo<\/a><\/strong> and for the final result, check out the <strong><a href=\"https:\/\/hongkiat.github.io\/mediasourceapi\/\" target=\"_blank\" rel=\"noopener noreferrer\">demo page<\/a><\/strong>.<\/p>\n<h2>Browser support<\/h2>\n<p>As of writing this post, the <code>MediaSource<\/code> API is <a href=\"https:\/\/caniuse.com\/mediasource\" target=\"_blank\" rel=\"noopener noreferrer\">officially supported<\/a> in all major browsers. But the testing shows that the <strong>implementation is buggy in Firefox<\/strong>, and Webkit browsers still have troubles with the <code>appendWindowStart<\/code> property.<\/p>\n<p>As the MediaSource API is <strong>still in its experimental stage<\/strong>, access to higher editing functions may be limited but the <strong>basic streaming<\/strong> feature is something you can <strong>make use of right away<\/strong>.<\/p>","protected":false},"excerpt":{"rendered":"<p>With the MediaSource API, you can generate and configure media streams right in the browser. It allows you to perform a variety of operations on media data held by media-related HTML tags such as &lt;audio&gt; or &lt;video&gt;. For instance, you can mix different streams, create overlapping media, lazy load media, and edit media metrics such&hellip;<\/p>\n","protected":false},"author":145,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3392],"tags":[4396],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Stream Truncated Audio Using MediaSource API - Hongkiat<\/title>\n<meta name=\"description\" content=\"With the MediaSource API, you can generate and configure media streams right in the browser. It allows you to perform a variety of operations on media\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Stream Truncated Audio Using MediaSource API\" \/>\n<meta property=\"og:description\" content=\"With the MediaSource API, you can generate and configure media streams right in the browser. It allows you to perform a variety of operations on media\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-05T13:11:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-03T15:58:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/assets.hongkiat.com\/uploads\/mediasource-api-stream-truncated-audio\/mediasource-api.jpg\" \/>\n<meta name=\"author\" content=\"Preethi Ranjit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Preethi Ranjit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mediasource-api-stream-truncated-audio\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mediasource-api-stream-truncated-audio\\\/\"},\"author\":{\"name\":\"Preethi Ranjit\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\"},\"headline\":\"How to Stream Truncated Audio Using MediaSource API\",\"datePublished\":\"2020-06-05T13:11:10+00:00\",\"dateModified\":\"2025-04-03T15:58:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mediasource-api-stream-truncated-audio\\\/\"},\"wordCount\":636,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mediasource-api-stream-truncated-audio\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/mediasource-api-stream-truncated-audio\\\/mediasource-api.jpg\",\"keywords\":[\"Audio Editors\"],\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mediasource-api-stream-truncated-audio\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mediasource-api-stream-truncated-audio\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mediasource-api-stream-truncated-audio\\\/\",\"name\":\"How to Stream Truncated Audio Using MediaSource API - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mediasource-api-stream-truncated-audio\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mediasource-api-stream-truncated-audio\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/mediasource-api-stream-truncated-audio\\\/mediasource-api.jpg\",\"datePublished\":\"2020-06-05T13:11:10+00:00\",\"dateModified\":\"2025-04-03T15:58:14+00:00\",\"description\":\"With the MediaSource API, you can generate and configure media streams right in the browser. It allows you to perform a variety of operations on media\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mediasource-api-stream-truncated-audio\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mediasource-api-stream-truncated-audio\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mediasource-api-stream-truncated-audio\\\/#primaryimage\",\"url\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/mediasource-api-stream-truncated-audio\\\/mediasource-api.jpg\",\"contentUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/mediasource-api-stream-truncated-audio\\\/mediasource-api.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mediasource-api-stream-truncated-audio\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Stream Truncated Audio Using MediaSource API\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\",\"name\":\"Preethi Ranjit\",\"description\":\"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/preethi\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Stream Truncated Audio Using MediaSource API - Hongkiat","description":"With the MediaSource API, you can generate and configure media streams right in the browser. It allows you to perform a variety of operations on media","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/","og_locale":"en_US","og_type":"article","og_title":"How to Stream Truncated Audio Using MediaSource API","og_description":"With the MediaSource API, you can generate and configure media streams right in the browser. It allows you to perform a variety of operations on media","og_url":"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2020-06-05T13:11:10+00:00","article_modified_time":"2025-04-03T15:58:14+00:00","og_image":[{"url":"https:\/\/assets.hongkiat.com\/uploads\/mediasource-api-stream-truncated-audio\/mediasource-api.jpg","type":"","width":"","height":""}],"author":"Preethi Ranjit","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Preethi Ranjit","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/"},"author":{"name":"Preethi Ranjit","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3"},"headline":"How to Stream Truncated Audio Using MediaSource API","datePublished":"2020-06-05T13:11:10+00:00","dateModified":"2025-04-03T15:58:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/"},"wordCount":636,"commentCount":2,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/mediasource-api-stream-truncated-audio\/mediasource-api.jpg","keywords":["Audio Editors"],"articleSection":["Coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/","url":"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/","name":"How to Stream Truncated Audio Using MediaSource API - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/#primaryimage"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/mediasource-api-stream-truncated-audio\/mediasource-api.jpg","datePublished":"2020-06-05T13:11:10+00:00","dateModified":"2025-04-03T15:58:14+00:00","description":"With the MediaSource API, you can generate and configure media streams right in the browser. It allows you to perform a variety of operations on media","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/#primaryimage","url":"https:\/\/assets.hongkiat.com\/uploads\/mediasource-api-stream-truncated-audio\/mediasource-api.jpg","contentUrl":"https:\/\/assets.hongkiat.com\/uploads\/mediasource-api-stream-truncated-audio\/mediasource-api.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/mediasource-api-stream-truncated-audio\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Stream Truncated Audio Using MediaSource API"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3","name":"Preethi Ranjit","description":"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.","url":"https:\/\/www.hongkiat.com\/blog\/author\/preethi\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-7H7","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/29581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=29581"}],"version-history":[{"count":4,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/29581\/revisions"}],"predecessor-version":[{"id":73475,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/29581\/revisions\/73475"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=29581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=29581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=29581"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=29581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}