what is the best way to play and set up controls for video (like play, pause, stop, volume and seek bar) on xamarin.ios.
Actually , there are lots of solutions which can implement it . Like MPMoviePlayerController,MPMoviePlayerViewController,AVPlayer,AVPlayerViewController...
Note: MPMoviePlayerController and MPMoviePlayerViewController are obsolete after iOS 9.0 .
The following code are the basic usage of AVPlayer
in the method ViewDidLoad
//Set the local movie file path
//string moviePath = NSBundle.MainBundle.PathForResource("xxx", "mp4");
//NSUrl movieUrl = NSUrl.FromFilename(moviePath);
//set remote url path
NSUrl movieUrl = new NSUrl("https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4");
//Using AVPlayer(using AVFoundation)
AVPlayer avPlayer;
AVPlayerLayer playerLayer;
AVAsset asset;
AVPlayerItem playerItem;
asset = AVAsset.FromUrl(movieUrl);
playerItem = new AVPlayerItem(asset);
avPlayer = new AVPlayer(playerItem);
playerLayer = AVPlayerLayer.FromPlayer(avPlayer);
playerLayer.Frame = new CGRect(50, 300, 200, 200);
View.Layer.AddSublayer(playerLayer);
avPlayer.Play();
// you can add button and slider to control the play, pause , seek and volume
avPlayer.Pause();
avPlayer.Seek();
avPlayer.Volume = xxx;
In this way , you need to define the UI of Control Element by yourself.
If the url is always remote , we can also open the url in WKWebView . It had implemented pause and seek function in default .
//set remote url path
NSUrl movieUrl = new NSUrl("https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4");
var webView = new WKWebView(View.Frame, new WKWebViewConfiguration());
Add(webView);
using (var request = NSUrlRequest.FromUrl(movieUrl))
{
webView.LoadRequest(request);
}