播放视频时不显示MPNowPlayingInfoCenter,而在播放音频时不显示MPNowPlayingInfoCenter

时间:2020-01-24 10:01:52

标签: ios xamarin media-player

我想制作一个播放音频和视频的应用程序。我希望音频可以在后台模式下播放,所以我添加了MediaPlayerInfo。虽然我不希望视频在后台模式下播放。因此,我在播放视频时不需要MediaPlayerInfo。

但是,尽管我没有设置它,但是在播放视频时,MediaPlayerInfo仍然以某种方式显示在通知栏中。 我已经尝试将其设置为“ null”,但是它没有改变行为。

任何人都可以帮助我使MediaPlayerInfo在后台模式下播放音频时显示,但是在播放视频时(背景和前景模式)却没有显示吗?

(更新)这是我的代码的片段:

    private MPRemoteCommandCenter m_commandCenter = MPRemoteCommandCenter.Shared;
    private string m_currentAudioTitle = "";
    private MPNowPlayingInfo _currentFileInfo;
    NSObject _playCommandTarget;
    NSObject _pauseCommandTarget;
    NSObject _skipBackwardCommandTarget;
    NSObject _skipForwardCommandTarget;

public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);

        m_observerList.Add(NSNotificationCenter.DefaultCenter.AddObserver(aName: UIApplication.DidEnterBackgroundNotification, notify: HandleDidEnterBackgroundNotification));
        m_observerList.Add(NSNotificationCenter.DefaultCenter.AddObserver(aName: UIApplication.WillEnterForegroundNotification, notify: HandleWillEnterForegroundNotification));
    }


public override void ViewWillDisappear(bool animated)
    {
        base.ViewWillDisappear(animated);

        foreach (NSObject _observer in m_observerList)
        {
            NSNotificationCenter.DefaultCenter.RemoveObserver(_observer);
        }
    }

private void HandleWillEnterForegroundNotification(NSNotification obj)
    {
        RemoveRemoteTransportControl();
        RemoveNowPlaying();
    }

private void HandleDidEnterBackgroundNotification(NSNotification obj)
    {
        SetupRemoteTransportControl();
        SetupNowPlaying("Test Asset Name");
    }

 public void SetupRemoteTransportControl()
    {
        m_commandCenter.SkipBackwardCommand.Enabled = true;
        m_commandCenter.SkipForwardCommand.Enabled = true;

        _playCommandTarget = m_commandCenter.PlayCommand.AddTarget(PlayAudioBackground);
        _pauseCommandTarget = m_commandCenter.PauseCommand.AddTarget(PauseAudioBackground);
        _skipBackwardCommandTarget = m_commandCenter.SkipBackwardCommand.AddTarget(SkipBackwardBackground);
        _skipForwardCommandTarget = m_commandCenter.SkipForwardCommand.AddTarget(SkipForwardBackground);
    }

    public void RemoveRemoteTransportControl()
    {
        m_commandCenter.SkipBackwardCommand.Enabled = false;
        m_commandCenter.SkipForwardCommand.Enabled = false;
        m_commandCenter.PlayCommand.RemoveTarget(_playCommandTarget);
        m_commandCenter.PauseCommand.RemoveTarget(_pauseCommandTarget);
        m_commandCenter.SkipBackwardCommand.RemoveTarget(_skipBackwardCommandTarget);
        m_commandCenter.SkipForwardCommand.RemoveTarget(_skipForwardCommandTarget);
        m_commandCenter.Dispose();
    }

 public void SetupNowPlaying(string aAssetTitle = null)
    {
        _currentFileInfo = new MPNowPlayingInfo();
        if (!string.IsNullOrEmpty(aAssetTitle))
        {
            m_currentAudioTitle = aAssetTitle;

        }
        Console.WriteLine("SetupNowPlaying: "+ m_currentAudioTitle);
        _currentFileInfo.Title = m_currentAudioTitle;
        _currentFileInfo.ElapsedPlaybackTime = apPlayer.CurrentTime.Seconds;
        _currentFileInfo.PlaybackDuration = m_audioAsset.Duration.Seconds;
        MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = _currentFileInfo;
    }

public void RemoveNowPlaying()
    {
        MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = _currentFileInfo;
    }

0 个答案:

没有答案