简体   繁体   中英

How to play background audio Windows phone 8.1

I read this article about "How to play audio in the background (XAML)" and works,i played my mp3 file well,but if i try to out from app the music just stop,i thought "background audio" was to play even if the app was not focus on screen!

XAML

<Grid>
    <MediaElement x:Name="musicPlayer" 
      Source="Assets/VIGEVANO.mp3"
      AudioCategory="BackgroundCapableMedia"
      CurrentStateChanged="MusicPlayer_CurrentStateChanged" />

</Grid>

CS

SystemMediaTransportControls systemControls;
public MainPage()
    {

        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;
        // Hook up app to system transport controls.
        systemControls = SystemMediaTransportControls.GetForCurrentView();
        systemControls.ButtonPressed += SystemControls_ButtonPressed;

        // Register to handle the following system transpot control buttons.
        systemControls.IsPlayEnabled = true;
        systemControls.IsPauseEnabled = true;

    }

private void SystemControls_ButtonPressed(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs args)
    {
        switch (args.Button)
        {
            case SystemMediaTransportControlsButton.Play:
                PlayMedia();
                break;
            case SystemMediaTransportControlsButton.Pause:
                PauseMedia();
                break;
            default:
                break;
        }
    }
    async void PlayMedia()
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            musicPlayer.Play();
        });
    }

    async void PauseMedia()
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            musicPlayer.Pause();
        });
    }

清单文件

What i miss?

Summary answer reached
Since Background Streaming was the main requirement so the solution reached was Yes Check out this sample and let me know if it works for you. Your background task first tutorial works for local mp3 files but streaming audio is a different scenario in itself
Also for streaming we used to use PhoneSM

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM