I am using a videoview from https://github.com/Baseflow/XamarinMediaManager ,it works perfectly fine. I want audio to be continued played when the app goes in background or screen is locked, that can be achieved using the method given in this link 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
So I followed it as you can see my codes. I am getting currently PlayerViewController and AVPlayer and connecting and disconnecting them as done in the given class.
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;
}
}
In my AppDelege Class, I create an instance of the this class and use it as given in the code.
[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);
}
}
I have done all the background playing setting in the info.plist, but it still doesn't work. It stops when the app is gone in the background. Help will be appreciated to sort this out.
Thanks