This sample demonstrates how to use the new MediaStreamSource API to stream a MPEG-Layer 3 (MP3) audio file. The media stream source API are in the Windows.Media.Core namespace.

Note  This sample was created using one of the universal app templates available in Visual Studio. It shows how its solution is structured so it can run on both Windows 8.1 and Windows Phone 8.1. For more info about how to build apps that target Windows and Windows Phone with Visual Studio, see Build apps that target Windows and Windows Phone 8.1 by using Visual Studio.

MediaStreamSource is a new generic media source for Windows Store apps which is introduced in Windows 8.1. MediaStreamSource allows apps to send compressed or uncompressed audio and video samples to the media pipeline for playback, transcoding, and streaming. Media samples can be dynamically generated by the app, or de-multiplexed from a stream or files. This flexibility enables apps to more easily extend platform support for new media formats or solve complex problems, such as adaptive streaming.

The MediaStreamSource API are very similar to the Microsoft Silverlight API of the same name.

MediaStreamSource can be used with audio and video objects in Windows Store apps using JavaScript, MediaElement objects in Windows Store app using C++, C#, or Visual Basic, and the MediaTranscoder.

Here are some of the main MediaStreamSource API. The order outlines the basic flow of how MediaStreamSource functions. You'll notice that the MediaStreamSource sends request objects to the app through event arguments. These request objects enable the app to interact with the MediaStreamSource and pass data back to it.

API Description
MediaStreamSource Represents a media source that delivers media samples directly to the media pipeline. MediaStreamSource consumes MediaStreamSample objects that are provided by the application.
MediaStreamSample Represents a media sample used by the MediaStreamSource.
MediaStreamSource.Starting (event)

The MediaStreamSource uses this event to notify the app that it is ready to start processing media data.

MediaStreamSourceStartingRequest

Represents a request from the MediaStreamSource that it is ready to start processing media data.

Apps should reply as soon as possible to this request by calling SetActualStartPosition on the request. If an app needs to delay the MediaStreamSource from processing data, it can get an asynchronous deferral from MediaStreamSourceStartingRequest.GetDeferral. When the app is ready for the MediaStreamSource to start, it calls Complete on the deferral object.

The starting request is accessed through the MediaStreamSourceStartingEventArgs that are passed to the MediaStreamSource.Starting event handler.

MediaStreamSource.SampleRequested (event)

The MediaStreamSource uses this event to notify the app that it is ready for a MediaStreamSample.

Apps are required to register a handler for this event.

MediaStreamSourceSampleRequest

Represents a request from the MediaStreamSource for a new media sample. Setting the Sample property to the new MediaStreamSample triggers the MediaStreamSource to retrieve the media sample and continue processing the media data.

Apps should reply as soon as possible to this request. If an app needs time before sending the MediaStreamSample, it can get an asynchronous deferral from MediaStreamSourceSampleRequest.GetDeferral. When the app is finished with the deferral, it calls Complete on the deferral object.

The sample request is accessed through the MediaStreamSourceSampleRequestedEventArgs that are passed to the MediaStreamSource.SampleRequest event handler.

The app indicates it has reached the end of the stream by responding to a MediaStreamSourceSampleRequest without providing a MediaStreamSample, or by assigning the MediaStreamSourceSampleRequest.Sample property to null.

MediaStreamSource.Closed (event)

The MediaStreamSource uses this event to notify the app that it has shut down.

MediaStreamSourceClosedRequest

Represents a request from the MediaStreamSource that it has closed.

The close request is accessed through the MediaStreamSourceClosedEventArgs that are passed to the MediaStreamSource.Closed event handler.

MediaElement.SetMediaStreamSource Sets the source of the MediaElement to a MediaStreamSource.

Note  For Windows 8 app samples, download the Windows 8 app samples pack. The samples in the Windows 8 app samples pack will build and run only on Microsoft Visual Studio 2012.

Related topics

Windows 8 app samples
Roadmaps
Adding multimedia
Designing UX for apps
Roadmap for apps using C# and Visual Basic
Roadmap for apps using C++
Roadmap for apps using JavaScript
Reference
MediaStreamSource
MediaStreamSample
MediaStreamSource.Starting
MediaStreamSourceStartingRequest
MediaStreamSource.SampleRequested
MediaStreamSourceSampleRequest
MediaStreamSource.Closed
MediaStreamSourceClosedRequest
MediaElement.SetMediaStreamSource

Operating system requirements

Client
Windows 8.1
Server
Windows Server 2012 R2
Phone
Windows Phone 8.1

Build the sample

  1. Start Microsoft Visual Studio 2013 Update 2 and select File > Open > Project/Solution.
  2. Go to the directory to which you unzipped the sample. Then go to the subdirectory named for the sample and double-click the Visual Studio 2013 Update 2 Solution (.sln) file.
  3. Follow the steps for the version of the sample you want:
    • To build the Windows version of the sample:

      1. Select MediaStreamSource.Windows in Solution Explorer.
      2. Press Ctrl+Shift+B, or use Build > Build Solution, or use Build > Build MediaStreamSource.Windows.
    • To build the Windows Phone version of the sample:

      1. Select MediaStreamSource.WindowsPhone in Solution Explorer.
      2. Press Ctrl+Shift+B or use Build > Build Solution or use Build > Build MediaStreamSource.WindowsPhone.

Run the sample

The next steps depend on whether you just want to deploy the sample or you want to both deploy and run it.

Deploying the sample

Deploying and running the sample