4. Representing Audio Files with the AudioTrack Class
The AudioTrack
class includes information about the location of an audio file, as well as track metadata, such as the track’s title. Table 3 describes each of the AudioTrack
properties.
TABLE 3. AudioTrack
Properties
AudioTrack
implements IEditableObject
to enable batching of property change requests. After an AudioTrack
object has been instantiated, it cannot be changed without putting it in edit mode first. AudioTrack
uses an IAudioTrack
backing field, which links the managed AudioTrack
object to the native audio system via Com Interop. To make changes to an AudioTrack
object, its BeginEdit
method must be called. When all changes have been made, EndEdit
should be called. This commits the property changes to the OS via the native IAudioTrack
object.
5. Creating a Custom Audio Player Agent
An audio player agent can be created by
selecting the Windows Phone Audio Playback Agent project type from the
Visual Studio New Project dialog (see Figure 1). This produces a new project that includes a class that derives from AudioPlayerAgent
.
FIGURE 1 Visual Studio Windows Phone agent projects.
When you link to an Audio Player Agent project from your main project, a BackgroundServiceAgent
task definition is added to your main project’s WMAppManifest.xml file, resembling the following:
<Tasks>
<DefaultTask Name="_default" NavigationPage="MainPage.xaml"/>
<ExtendedTask Name="BackgroundTask">
<BackgroundServiceAgent Specifier="AudioPlayerAgent"
Name="<Unique name within the app>"
Source="<Assembly>"
Type="<Namespace.Class>" />
</ExtendedTask>
</Tasks>
It is not necessary to use Visual Studio’s
Audio Player Agent project template to create a custom background audio
agent. You can create the class and add the task definition to the
WMAppManifest.xml file manually if you so choose.