Stream Online Audio and Play in Background Windows 1



Download 47.6 Kb.
Date23.04.2018
Size47.6 Kb.
Stream Online Audio and Play in Background Windows 8.1
Hello Developers,

Today I’m going to introduce how to stream online audio and play it in the background through BackgroundTasks.

Requirements:

This blog is using to create basic windows 8.1 application, so you should be familiar with XAML and C#.

Instructions

So Friend, Let’s start, how to stream online audio and play it with you application.



  1. Define MediaElement,

      1. <MediaElement x:Name="myMedia"

AutoPlay="False"/>

  1. Assign the source file to media element

      1. string audioSourceUrl = "http://server12.mp3quran.net/maher/002.mp3";

      2. myMedia.Source = new Uri(audioSourceUrl, UriKind.RelativeOrAbsolute);

It’s working now, it’s easy to do.

Now it’s time to move, how to play audio in the background.

To play audio in the background, declare the Audio background task and handle the SystemMediaTransportControls buttons.

Important: Windows 8.1 introduces the SystemMediaTransportControls class which replace the MediaControl class. You should use SystemMediaTransportControls in your app.

Step 1: Declare the background audio task



  1. Open the project file for your app in Microsoft Visual Studio.

  2. Open the Package.appmanifest file in the Solution Explorer.

  3. Click the Declaration tab, then select Background Tasks from the Available Declaration.

  4. Click Add, then check the audio checkbox.

  5. It’s very important to specify the entry point (if your project’s name is MyApp then the entry point will be MyApp.App).

Step 2: Define the MediaElement and set the AudioCategory

The AudioCategory is a property on MediaElement, it’s using to either Communication or BackgroundCapableMedia.


  1. <MediaElement x:Name="myMedia"

AutoPlay="False" AudioCategory="BackgroundCapableMedia"/>

Define and listen to the CurrentStateChanged event,



  1. <MediaElement x:Name="myMedia"

CurrentStateChanged="myMedia_CurrentStateChanged"/>
Here the final xaml code that creats the media element,


  1. <MediaElement x:Name="myMedia"

AutoPlay="False" AudioCategory="BackgroundCapableMedia"

CurrentStateChanged="myMedia_CurrentStateChanged"/>


Now, we should initialize the SystemMediaTranspostControls, here is the method called InitializeTransportControls for that reason



void InitializeTransportControls()

{

systemControls = SystemMediaTransportControls.GetForCurrentView();

systemControls.ButtonPressed += systemControls_ButtonPressed;



systemControls.IsPlayEnabled = true;

systemControls.IsPauseEnabled = true;

}
After initializing the SystemMediaTranspostControls, here the code that handles the CurrentStateChanged event of the MediaElement

private void myMedia_CurrentStateChanged(object sender, RoutedEventArgs e)

{

switch (myMedia.CurrentState)

{

case MediaElementState.Closed:

systemControls.PlaybackStatus = MediaPlaybackStatus.Closed;

break;

case MediaElementState.Paused:

systemControls.PlaybackStatus = MediaPlaybackStatus.Paused;

break;

case MediaElementState.Playing:

systemControls.PlaybackStatus = MediaPlaybackStatus.Playing;

break;

case MediaElementState.Stopped:

systemControls.PlaybackStatus = MediaPlaybackStatus.Stopped;

break;

default:

break;

}

}

And here is the code of ButtonPressed (systemControls_ButtonPressed) event handler, also it’ll include two methods PlayMedia(), and PauseMedia()



void systemControls_ButtonPressed(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs args)

{

switch (args.Button)

{

case SystemMediaTransportControlsButton.Pause:

PlayMedia();

break;

case SystemMediaTransportControlsButton.Play:

PauseMedia();

break;

default:

break;

}

}

PlayMedia()



private async void PlayMedia()

{

await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>

{

elem.Play();

});

}
PauseMedia()

private async void PauseMedia()

{

await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>

{

elem.Pause();

});

}
Conclusion: In Window 8.1 Microsoft introduced SystemMediaTransportControls for MediaControls to make it easy to play audio in the background, you need to define the four steps that we introduced,
Hope you are enjoying

ZA


Download 47.6 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2020
send message

    Main page