{"id":2276,"date":"2023-04-23T16:56:48","date_gmt":"2023-04-23T14:56:48","guid":{"rendered":"https:\/\/alax.info\/blog\/?p=2276"},"modified":"2023-04-23T16:56:48","modified_gmt":"2023-04-23T14:56:48","slug":"play-an-mp3-with-media-foundation","status":"publish","type":"post","link":"https:\/\/alax.info\/blog\/2276","title":{"rendered":"Play an MP3 with Media Foundation"},"content":{"rendered":"\n<p>An under 100 lines of C++ code sample that plays an MP3 file with Microsoft Media Foundation.<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/roman380\/MediaFoundationMinimalAudioPlayback\">https:\/\/github.com\/roman380\/MediaFoundationMinimalAudioPlayback<\/a><\/p>\n\n\n\n<p>Comes from <a href=\"https:\/\/stackoverflow.com\/q\/76056442\/868014\">this StackOverflow question<\/a>, however unlike <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows\/win32\/medfound\/media-session-playback-example\">Microsoft&#8217;s sample<\/a> it does not spread simple code among multiple source code files, gives a quick way to build &amp; run, shows modern C++ practices (including Microsoft <a href=\"https:\/\/github.com\/microsoft\/wil\">Windows Implementation Library<\/a> in <a href=\"https:\/\/github.com\/roman380\/MediaFoundationMinimalAudioPlayback\/tree\/wil\"><code>wil<\/code> branch<\/a> &#8211; the library I recommend to use).<\/p>\n\n\n\n<p>The sample quickly goes over main concepts: topology, nodes, session, events, asynchronous processing. Media Foundation&#8217;s &#8220;Intelligent Connect&#8221; automatically &amp; transparently supplying two Media Foundation Transforms to decode MP3 audio and to fit audio format to acceptable by audio playback device.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Event: MESessionTopologySet\r\n  Node Count: 4\r\n  Node 0: MF_TOPOLOGY_SOURCESTREAM_NODE\r\n    MF_TOPONODE_MEDIASTART\r\n    MF_TOPONODE_SOURCE\r\n    MF_TOPONODE_PRESENTATION_DESCRIPTOR\r\n    MF_TOPONODE_STREAM_DESCRIPTOR\r\n  Node 1: MF_TOPOLOGY_OUTPUT_NODE\r\n    MF_TOPONODE_STREAMID\r\n    MF_TOPONODE_TRANSFORM_OBJECTID {D23E6476-B104-4707-81CB-E1CA19A07016}\r\n  Node 2: MF_TOPOLOGY_TRANSFORM_NODE\r\n    MF_TOPONODE_MARKIN_HERE\r\n    MF_TOPONODE_MARKOUT_HERE\r\n    MF_TOPONODE_DECODER\r\n    MF_TOPONODE_TRANSFORM_OBJECTID {BBEEA841-0A63-4F52-A7AB-A9B3A84ED38A}\r\n  Node 3: MF_TOPOLOGY_TRANSFORM_NODE\r\n    MF_TOPONODE_TRANSFORM_OBJECTID {F447B69E-1884-4A7E-8055-346F74D6EDB3}\r\nEvent: MESessionNotifyPresentationTime\r\nEvent: MESessionCapabilitiesChanged\r\n  Capabilities: MFSESSIONCAP_START MFSESSIONCAP_SEEK MFSESSIONCAP_RATE_FORWARD MFSESSIONCAP_DOES_NOT_USE_NETWORK\r\n  Change: MFSESSIONCAP_START MFSESSIONCAP_SEEK MFSESSIONCAP_RATE_FORWARD MFSESSIONCAP_DOES_NOT_USE_NETWORK\r\nEvent: MESessionTopologyStatus MF_TOPOSTATUS_READY\r\nEvent: MESessionTopologyStatus MF_TOPOSTATUS_STARTED_SOURCE\r\nEvent: MEExtendedType\r\nEvent: MESessionCapabilitiesChanged\r\n  Capabilities: MFSESSIONCAP_START MFSESSIONCAP_SEEK MFSESSIONCAP_PAUSE MFSESSIONCAP_RATE_FORWARD MFSESSIONCAP_DOES_NOT_USE_NETWORK\r\n  Change: MFSESSIONCAP_PAUSE\r\nEvent: MESessionStarted\r\n...\r\nEvent: MEEndOfPresentation\r\nEvent: MESessionTopologyStatus MF_TOPOSTATUS_ENDED\r\nEvent: MESessionCapabilitiesChanged\r\n  Capabilities: MFSESSIONCAP_START MFSESSIONCAP_SEEK MFSESSIONCAP_RATE_FORWARD MFSESSIONCAP_DOES_NOT_USE_NETWORK\r\n  Change: MFSESSIONCAP_PAUSE\r\nEvent: MESessionEnded<\/code><\/pre>\n\n\n\n<p>Have fun!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An under 100 lines of C++ code sample that plays an MP3 file with Microsoft Media Foundation. https:\/\/github.com\/roman380\/MediaFoundationMinimalAudioPlayback Comes from this StackOverflow question, however unlike Microsoft&#8217;s sample it does not spread simple code among multiple source code files, gives a quick way to build &amp; run, shows modern C++ practices (including Microsoft Windows Implementation Library&hellip; <\/p>\n<p><a class=\"moretag\" href=\"https:\/\/alax.info\/blog\/2276\">Read the full article<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[485,38,424],"class_list":["post-2276","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-audio","tag-c","tag-media-foundation"],"_links":{"self":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/2276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/comments?post=2276"}],"version-history":[{"count":0,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/2276\/revisions"}],"wp:attachment":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/media?parent=2276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/categories?post=2276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/tags?post=2276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}