我正在使用来自https://github.com/Baseflow/XamarinMediaManager的视频视图,它工作得非常好。我希望在应用程序进入后台或屏幕锁定时继续播放音频,这可以使用此链接中给出的方法来实现https://developer.apple.com/documentation/avfoundation/media_playback_and_selection/creating_a_basic_video_player_ios_and_tvos/playing_audio_from_a_video_asset_in_the_background
所以我跟着它,你可以看到我的代码。我目前正在获取 PlayerViewController 和 AVPlayer 并像在给定类中那样连接和断开它们。
public class LockedScreenManager
{
public AVQueuePlayer CurrentAVPlayer { get; set; }
public AVPlayerViewController CurrentAVPlayerController { get; set; }
public LockedScreenManager()
{
UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
CrossMediaManager.Current.PositionChanged += PositionChanged;
var audioSession = AVAudioSession.SharedInstance();
audioSession.SetCategory(AVAudioSessionCategory.Playback);
}
public void Connect()
{
CurrentAVPlayerController.Player = CurrentAVPlayer;
}
public void DisConnect()
{
CurrentAVPlayer = null;
}
private void PositionChanged(object sender, MediaManager.Playback.PositionChangedEventArgs e)
{
var apple = CrossMediaManager.Apple;
var _IosMediaPlayer = apple.AppleMediaPlayer as MediaManager.Platforms.Ios.Player.IosMediaPlayer;
CurrentAVPlayer = _IosMediaPlayer.Player;
CurrentAVPlayerController = _IosMediaPlayer.PlayerView.PlayerViewController;
var mediaItem = CrossMediaManager.Current.Queue.Current;
if (mediaItem == null)
{
MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = null;
return;
}
var nowPlayingInfo = new MPNowPlayingInfo
{
Title = mediaItem.Title,
AlbumTitle = mediaItem.Album,
AlbumTrackNumber = mediaItem.TrackNumber,
AlbumTrackCount = mediaItem.NumTracks,
Artist = mediaItem.DisplaySubtitle,
Composer = mediaItem.Composer,
DiscNumber = mediaItem.DiscNumber,
Genre = mediaItem.Genre,
ElapsedPlaybackTime = CrossMediaManager.Current.Position.TotalSeconds,
PlaybackDuration = CrossMediaManager.Current.Duration.TotalSeconds,
PlaybackQueueIndex = CrossMediaManager.Current.Queue.CurrentIndex,
PlaybackQueueCount = CrossMediaManager.Current.Queue.Count,
IsLiveStream = mediaItem.IsLive
};
MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = nowPlayingInfo;
}
}
在我的 AppDelege 类中,我创建了这个类的一个实例,并按照代码中给出的方式使用它。
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
LockedScreenManager lockedScreenManager;
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
lockedScreenManager= new LockedScreenManager();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
public override void WillEnterForeground(UIApplication uiApplication)
{
lockedScreenManager.Connect();
base.WillEnterForeground(uiApplication);
}
public override void DidEnterBackground(UIApplication uiApplication)
{
lockedScreenManager.DisConnect();
base.DidEnterBackground(uiApplication);
}
}
info.plist中的所有后台播放设置我都做了,但是还是不行。当应用程序在后台消失时它会停止。我们将不胜感激帮助解决这个问题。
谢谢