我在Xamarin.ios中做了一个用于播放音频和背景控制的依赖服务。
音频在后台执行得很好。但是,锁屏控制器没有显示。
我不知道是什么问题。
我在info.plist中注册了UIBackgroundMode和audio,并将AVAudioSession设置为播放。
我使用play_pause(string url)函数来播放音频和设置MPNowPlayingInfo。
[assembly: Dependency(typeof(AudioService))]
namespace BibleHymn.iOS
{
class AudioService : IAudio
{
private bool isPlaying = false;
private bool interrupted = false;
int audioNum = 0;
AVFoundation.AVPlayer player;
public AudioService()
{
var avSession = AVAudioSession.SharedInstance();
avSession.SetCategory(AVAudioSessionCategory.Playback);
NSError activationError = null;
avSession.SetActive(true, out activationError);
}
public bool Play_Pause(string url)
{
if(player == null)
{
this.player = new AVFoundation.AVPlayer();
this.player = AVFoundation.AVPlayer.FromUrl(Foundation.NSUrl.FromString(url));
this.player.Play();
UpdateNotification();
}
isPlaying !=isPlaying;
return isPlaying;
}
public void UpdateNotification()
{
var item = new MPNowPlayingInfo
{
Title = "My Title"
};
MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = item;
Device.BeginInvokeOnMainThread(() => {
UIKit.UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
});
}
}我通过button控件的命令使用了viewModel中的play_pause。
初始化,就像...
public IAudio streamService = DependencyService.Get<IAudio>();在指挥部。
streamService.Play_Pause(url);发布于 2018-12-28 14:51:22
您需要通过以下截图在info.plist中注册音频的后台支持

您还导入了Mediaplayer.framework吗
https://stackoverflow.com/questions/53953851
复制相似问题