Не показывать MPNowPlayingInfoCenter во время воспроизведения видео и показывать его при воспроизведении аудио - PullRequest
0 голосов
/ 24 января 2020

Я хочу сделать приложения, которые воспроизводят аудио и видео. Я хочу, чтобы звук мог воспроизводиться в фоновом режиме, поэтому я добавляю MediaPlayerInfo. Пока я не хочу, чтобы видео воспроизводилось в фоновом режиме. Поэтому мне не нужен MediaPlayerInfo во время воспроизведения видео.

Но почему-то MediaPlayerInfo все еще отображается в панели уведомлений во время воспроизведения видео, хотя я его не установил. Я уже пытался установить значение «ноль», но это не изменило его поведение.

Любой может помочь мне отобразить 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;
    }
...