locked
BackgroundMediaPlayer挂起后不能后台播放 RRS feed

  • 问题

  • 我在使用在线音乐播放软件  使用到了BackgroundMediaPlayer   我在真机和模拟器上面调试 接WIN键 返回键 返回到 开始界面 音乐会一直处理播放状态 

    但是我拨掉数据线  直接运行软件  音乐就不能后台播放了   必须处理前台才能播放

    当我在模拟器上面  点击开始调试   然后点击 生命周期(lifecycle event)  挂起(suspending)   音乐也停止播放    不点挂起  而是在模拟器上面点击  开始  或者返回 将软件挂起  则可以台面播放

    我也下载了 微软官方的后台音乐播放DEMO 他们的DEMO也不能正常挂起  一旦挂机  则软件停止播放

    namespace BackgroundTasks
    {
        public sealed class BackgroundMusicPlayTask : IBackgroundTask
        {
            public SystemMediaTransportControls mediatransportcontrol { set; get; }
    
            //public string songid { set; get; }
    
            public void Run(IBackgroundTaskInstance taskInstance)
            {
                BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
                mediatransportcontrol = SystemMediaTransportControls.GetForCurrentView();
                mediatransportcontrol.ButtonPressed += mediatransportcontrol_ButtonPressed;
                mediatransportcontrol.IsPreviousEnabled = true;//privious button
                mediatransportcontrol.IsNextEnabled = true;//next button
                mediatransportcontrol.IsPauseEnabled = true;//pause button
                mediatransportcontrol.IsPlayEnabled = true; //play button
                mediatransportcontrol.IsStopEnabled = false;//stop button
    
                BackgroundMediaPlayer.Current.MediaEnded -= Current_MediaEnded;
                BackgroundMediaPlayer.Current.MediaEnded += Current_MediaEnded;
    
               // BackgroundMediaPlayer.Current.CurrentStateChanged -= Current_CurrentStateChanged;
                //BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged;
    
                BackgroundMediaPlayer.MessageReceivedFromForeground -= BackgroundMediaPlayer_MessageReceivedFromForeground;        
                BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;
    
    
                deferral.Complete();
            }
    
            void Current_MediaEnded(MediaPlayer sender, object args)
            {
                BackgroundMediaPlayer.SendMessageToForeground(new ValueSet { 
                        //{"songid",this.songid},
                        {"key","Next"}
                        });
            }
    
    
            private void mediatransportcontrol_ButtonPressed(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs args)
            {
                switch (args.Button)
                {
                    case SystemMediaTransportControlsButton.Pause:
                        BackgroundMediaPlayer.Current.Pause();
                        break;
                    case SystemMediaTransportControlsButton.Play:
                        BackgroundMediaPlayer.Current.Play();
                        break;
                    case SystemMediaTransportControlsButton.Next:
                        BackgroundMediaPlayer.SendMessageToForeground(new ValueSet { 
                        //{"songid",this.songid},
                        {"key","Next"}
                        });
                        break;
                    case SystemMediaTransportControlsButton.Previous:
                        BackgroundMediaPlayer.SendMessageToForeground(new ValueSet{
                            //{"songid",this.songid},
                            {"key","Previous"}
                        });
                        break;
    
    
                }
    
            }
    
            private void Current_CurrentStateChanged(MediaPlayer sender, object args)
            {
                MediaPlayerState playerState = sender.CurrentState;
                switch (playerState)
                {
                    case MediaPlayerState.Stopped:
                        BackgroundMediaPlayer.SendMessageToForeground(new ValueSet { 
                        //{"songid",this.songid},
                        {"key","Next"}
                        });
                        break;
                }
            }
    
            private void BackgroundMediaPlayer_MessageReceivedFromForeground(object sender, MediaPlayerDataReceivedEventArgs e)
            {
                mediatransportcontrol.DisplayUpdater.Type = MediaPlaybackType.Music;
                mediatransportcontrol.DisplayUpdater.MusicProperties.Title = e.Data["Title"].ToString();
                mediatransportcontrol.DisplayUpdater.MusicProperties.Artist = e.Data["Artist"].ToString();
                string FirstOrLast = e.Data["FirstOrLast"].ToString();
                if (FirstOrLast == "First")
                {
                    mediatransportcontrol.IsPreviousEnabled = false;
                }
                else if (FirstOrLast == "Last")
                {
                    mediatransportcontrol.IsNextEnabled = false;
                }
                else if (FirstOrLast == "Middle")
                {
                    mediatransportcontrol.IsPreviousEnabled = true;
                }
                mediatransportcontrol.DisplayUpdater.Update();
            }
    
        }
    }

    2015年10月8日 12:46

答案

  • Hello Dengguibao,

    是的,这个问题已经有几个OP反馈了,目前的话我们已经开了一个内部的discussion,请耐心等待,一旦有任何结果,我们会发一个结果到这个case上的。

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年10月9日 1:53
    版主