在 xamarin.ios 上播放和设置视频控件(如播放、暂停、停止、音量和搜索栏)的最佳方式是什么
what is the best way to play and setup controls for video (like play, pause, stop, volume and seek bar) on xamarin.ios
在 xamarin.ios 上播放和设置视频控件(如播放、暂停、停止、音量和搜索栏)的最佳方式是什么。
实际上,有很多解决方案可以实现它。比如MPMoviePlayerController,MPMoviePlayerViewController,AVPlayer,AVPlayerViewController.. .
注意:MPMoviePlayerController 和 MPMoviePlayerViewController 在 iOS 9.0 之后已过时。
以下代码是AVPlayer
的基本用法
方法中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;
这种方式需要自己定义控制元素UI
如果url一直是远程的,我们也可以在WKWebView中打开url。默认实现了暂停和搜索功能。
//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);
}
在 xamarin.ios 上播放和设置视频控件(如播放、暂停、停止、音量和搜索栏)的最佳方式是什么。
实际上,有很多解决方案可以实现它。比如MPMoviePlayerController,MPMoviePlayerViewController,AVPlayer,AVPlayerViewController.. .
注意:MPMoviePlayerController 和 MPMoviePlayerViewController 在 iOS 9.0 之后已过时。
以下代码是AVPlayer
的基本用法方法中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;
这种方式需要自己定义控制元素UI
如果url一直是远程的,我们也可以在WKWebView中打开url。默认实现了暂停和搜索功能。
//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);
}