HomeWindowsOxygene and WP8 – Play Background Audio in Windows Phone App

Oxygene and WP8 – Play Background Audio in Windows Phone App

The Windows Phone developers can build apps targeting windows phone 8 which can play songs or music in the background. The background audio app can either be a local media or streaming media.

How to Play Background Audio in Windows Phone App?

1. The Windows Phone 8 SDK includes the “Windows Phone Audio Playback Agent” project which is available in Microsoft Visual Studio 2012. A new project with this template needs to be created and be referenced from your main windows phone project.

  • Open the Existing Windows Phone Project. Right click on the Windows Phone solution file in Visual Studio and select Add-> New Project.
  • Use the Windows Phone Audio Playback Agent project template, provide the project name and click OK. For example. Let’s Name the background agent project as “MobileOSGeekAudioPlaybackAgent” and select the Windows Phone OS version to Windows Phone OS 8.0.

Oxygene and WP8 - Play Background Audio in Windows Phone App

2. In the main windows phone project, add the media file and select it. In the properties dialog, set the copy local to “Copy if newer” and save the project. Also, add the reference of the “MobileOSGeekAudioPlaybackAgent” project.

3. Add the ExtendedTask in the “WMAppManifest.xml” file to support the back ground audio agent.

<Tasks>
 <ExtendedTask Name="BackgroundTask">
   <BackgroundServiceAgent Specifier="AudioPlayerAgent" Name="MobileOSGeekAudioPlaybackAgent" Source="MobileOSGeekAudioPlaybackAgent" Type="MobileOSGeekAudioPlaybackAgent.AudioPlayer" />
 </ExtendedTask>
</Tasks>

4. Add the code in the UI (Code behind) to copy the media file to the isolated storage and then play the song.

Function to Copy the music file to isolated storage

// Copy the File to Isolated Storage

method MainPage.CopyToIsolatedSorage;
begin
 using storage: IsolatedStorageFile := IsolatedStorageFile.GetUserStoreForApplication() do begin
 var fileName: System.String := 'music1.mp3';
 if not storage.FileExists(fileName) then begin
 var resource: System.Windows.Resources.StreamResourceInfo := Application.GetResourceStream(new Uri(fileName, UriKind.Relative));
 using file: IsolatedStorageFileStream := storage.CreateFile(fileName) do begin
 var chunkSize: System.Int32 := 4096;
 var bytes: array of System.Byte := new System.Byte[chunkSize];
 var byteCount: System.Int32;
 loop begin
  byteCount := resource.Stream.Read(bytes, 0, chunkSize);
  file.Write(bytes, 0, byteCount);
  if not(byteCount > 0) then break;
 end;
 end
 end
 end;
end;

Function to play the media using BackgroundAudioPlayer

// Method to Play Song
method MainPage.PlaySong;
begin
 var tracks: List<AudioTrack> := new List<AudioTrack>();
 tracks.Add(new AudioTrack(new Uri('music1.mp3', UriKind.Relative), 'MobileOSGeek Music', 'MobileOSGeek Music', 'MobileOSGeek Music', nil));
 var track := tracks.FirstOrDefault();
 BackgroundAudioPlayer.Instance.Track := track;
 BackgroundAudioPlayer.Instance.Play()
end;

Invoke the above methods to play the song.

method MainPage.btnLaunch_Click(sender: Object; e: System.Windows.RoutedEventArgs);
begin
 CopyToIsolatedSorage();
 PlaySong();
end;

5. Build the Project and run it. When you click the “Button”, the audio should start playing.

Download the source code used for the Oxygene and WP8 – Play Background Audio in Windows Phone App article here

    1 Comment

  1. mukund
    May 9, 2014
    Reply

    Hi senthl, hope you have worked on dynamic audio generation along with playing from static resources. I would like to understand how it is possible to continue generation of sound ( say binaural waves ) after the user leaves the app by pressing home key.. Any suggestions?

Leave a Reply

You May Also Like

This blog post will guide you through several effective methods to troubleshoot and resolve the issue of Microsoft Edge not...
Windows 11 offers a range of audio enhancements that can enrich your listening experience. These enhancements include features like virtual...
Windows 11 brings a fresh and visually stunning design to your desktop, and one of the standout features is the...