Audio streaming agents allow you to
customize how audio files are downloaded and decoded by your app, and
they enable you to support live streaming scenarios not natively
supported by the phone.
An audio streaming agent can be created in
the same manner as an audio player agent, by using the “Windows Phone
Audio Streaming Agent” Visual Studio project template .
The Visual Studio project template produces a class that derives from AudioStreamingAgent
.
The agent must then be registered with your main project’s
WMAppManifest.xml file, which is done automatically when linking to an
Audio Streaming Agent project. The XML definition can be seen alongside
the AudioPlayerAgent
, as shown:
<Tasks>
<DefaultTask Name="_default" NavigationPage="MainPage.xaml"/>
<ExtendedTask Name="BackgroundTask">
<BackgroundServiceAgent Specifier="AudioPlayerAgent"
Name="<Unique name within the app>"
Source="<Assembly>"
Type="<Namespace.Class>" />
<BackgroundServiceAgent Specifier="AudioStreamingAgent"
Name="<Unique name within the app>"
Source="<Assembly>"
Type="<Namespace.Class>" />
</ExtendedTask>
</Tasks>
Registering a custom AudioStreamingAgent
does not disable the built-in streaming and decoding of the phone. The
OS determines whether to use the built-in audio streaming, or your
custom AudioStreamingAgent
, based on the AudioTrack
’s Source
property, each time a new AudioTrack
is played. To have your custom AudioStreamingAgent
called for a particular track, assign the Source
property of the AudioTrack
to null, as shown in the following excerpt:
new AudioTrack(null, /* Uri is null so that it is sent
* to the AudioStreamingAgent
* called AssemblyAudioStreamingAgent. */
"Assembly Track!",
"Acquired using AudioStreamingAgent",
"Unleashed",
null,
"AudioFiles/Audio04.mp3", // Tag parameter.
EnabledPlayerControls.All),
If the Uri
is not null, the OS performs all streaming and decoding. By using this
technique, you can use a combination of both the inbuilt streaming and
decoding for some tracks, and custom streaming and decoding for others.
The AudioTrack
class contains a Tag
property, which can be used to pass information to an AudioStreamingAgent
.
Notice from the previous excerpt that this property is set to the
relative path of an audio file resource. You see, in the next section,
how this value is used to resolve an audio file resource from the
current assembly.
AudioStreamingAgent
contains a virtual method named OnBeginStreaming
, which is overridden to handle streaming and decoding of an audio track. The method has two parameters: the AudioTrack
and an AudioStreamer
. The AudioStreamer
allows you to attach your own MediaStreamSource
. MediaStreamSource
provides you with direct access to APIs for manipulating encoded
elementary audio and video streams. It enables you to implement your
own mechanism for processing the contents of a media file, enabling
support beyond the native built-in media formats.
Although this section does not cover MediaStreamSource
in detail, it does, however, look at applying a specialized MediaStreamSource
from the ManagedMediaHelpers project to enable
the playback of audio files that are located in an app’s assembly,
something that is not supported by the phone’s built-in streaming and
decoding system.