如何以编程方式控制 MPNowPlayingInfoCenter 中的播放/暂停按钮 (Xamarin.iOS)

huangapple go评论49阅读模式
英文:

How to programmatically control play/pause button in MPNowPlayingInfoCenter (Xamarin.iOS)

问题

我正在开发一个使用Xamarin.iOS/Forms编写的播放音乐的应用程序。我们希望能够在应用程序内部或现在播放信息中心控制歌曲/歌单的播放。除了在应用程序内部按下播放/暂停按钮时,我无法通过编程方式控制信息中心的播放/暂停按钮状态之外,一切都按预期进行。我从几个Swift示例中了解到,您应该能够通过将MPNowPlayingInfo.PlaybackRate设置为1.0d来播放,或者0.0d来暂停。以下是应用程序的一些代码。

当新歌曲开始播放时,我使用以下代码添加歌曲:

_nowPlayingInfo = new MPNowPlayingInfo();
_nowPlayingInfo.Artist = songDetails.Artist;
_nowPlayingInfo.Title = songDetails.Title;
_nowPlayingInfo.PlaybackDuration = durationSeconds;
_nowPlayingInfo.PlaybackRate = 0.0d;
        			
var data = NSData.FromUrl(new NSUrl(songDetails.Album2xUrl));
var image = new UIImage(data:data);
_nowPlayingInfo.Artwork = new MPMediaItemArtwork(image);

MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = _nowPlayingInfo;

除了播放速率设置为0.0d时,一切都按预期进行,初始播放/暂停按钮的状态显示为暂停按钮,而我预期它会显示播放按钮。

当歌曲播放时,我使用以下代码进行更新:

_nowPlayingInfo.ElapsedPlaybackTime = elapsedSeconds;
MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = _nowPlayingInfo;

这也按预期进行。

当应用程序内的播放/暂停状态更新时,我尝试使用以下代码更新信息中心中的播放/暂停按钮,但这对播放/暂停按钮没有影响:

if (isPlaying)
    _nowPlayingInfo.PlaybackRate = 1.0d;
else
    _nowPlayingInfo.PlaybackRate = 0.0d;

MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = _nowPlayingInfo;
Console.WriteLine($"---- {MPNowPlayingInfoCenter.DefaultCenter.NowPlaying.PlaybackRate}");

我可以从控制台看到PlaybackRate设置为预期值,但更改该值对信息中心中的播放/暂停状态没有影响。

在Xamarin.iOS中,还有其他人遇到过这个问题吗?有没有其他更新播放/暂停按钮状态的方法?

我使用的是Xamarin.iOS版本:16.1.1.27,Xcode 14.2,并在iOS 16.3.1设备上进行测试。

英文:

I'm working on an app written in Xamarin.iOS/Forms that plays music. We want to be able to control the playback of the song/songlist from within the app or from the now playing info center. Everything is working as expected except I have not been able to programmatically control the state of the play/pause button in the info center when the user presses play/pause from within the app. My understanding from several swift examples is that you should be able to set the state by setting MPNowPlayingInfo.PlaybackRate to 1.0d for playing or 0.0d for paused. Here is some code from the app.

When a new song starts I add the song using the following code:

_nowPlayingInfo = new MPNowPlayingInfo();
_nowPlayingInfo.Artist = songDetails.Artist;
_nowPlayingInfo.Title = songDetails.Title;
_nowPlayingInfo.PlaybackDuration = durationSeconds;
_nowPlayingInfo.PlaybackRate = 0.0d;
    			
var data = NSData.FromUrl(new NSUrl(songDetails.Album2xUrl));
var image = new UIImage(data:data);
_nowPlayingInfo.Artwork = new MPMediaItemArtwork(image);

MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = _nowPlayingInfo;

Everything works as expected except the initial state of the play/pause button shows the pause button and with PlaybackRate set to 0.0d I expected it to show the play button.

As the song plays I update the using the following code:

_nowPlayingInfo.ElapsedPlaybackTime = elapsedSeconds;
MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = _nowPlayingInfo;

This works as expected.

When the play/pause state updates from within the app I try to update the play/pause button in info center using the following code: which I have found in numerous swift examples but this has not affect on the play/pause button:

if (isPlaying)
	_nowPlayingInfo.PlaybackRate = 1.0d;
else
	_nowPlayingInfo.PlaybackRate = 0.0d;

MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = _nowPlayingInfo;
Console.WriteLine($"---- {MPNowPlayingInfoCenter.DefaultCenter.NowPlaying.PlaybackRate}");

I can see from the console that PlaybackRate is set to the expected value but changing the value has not effect on the play/pause state in info center.

Has anyone else had this issue in Xamarin.iOS? Is there a different way to update the state of the play/pause button?

I'm using Xamarin.iOS Version: 16.1.1.27, Xcode 14.2, and testing on an iOS 16.3.1 device.

答案1

得分: 1

为我的应用程序的解决方案是调用 AVAudioEngine.Pause() 和 AVAudioEngine.StartAndReturnError() 来暂停和开始播放音频。这会自动更新“正在播放”中的播放/暂停按钮状态。之前我在使用 AVAudioPlayerNode.Play() 和 AVAudioPlayerNode.Pause()。

英文:

The solution for my app was to call AVAudioEngine.Pause() and AVAudioEngine.StartAndReturnError() to pause and start playing of audio. This automatically updates the play/pause button state in Now Playing. Previously I was using AVAudioPlayerNode.Play() and AVAudioPlayerNode.Pause().

huangapple
  • 本文由 发表于 2023年2月26日 20:53:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定