這個播放器只有2個按鈕,播放/暫停和全屏,它看起來像一個system player
,如此快速和簡單。Objc - 如何在這樣的UIView內添加視頻播放器?
我在UIView裏面使用了UIWebView and HTML5
,但是UIWebView太慢了,不能沒有全屏播放。
如果此播放器是UIObject
或某些UIView的東西,我該如何添加它?
感謝〜
這個播放器只有2個按鈕,播放/暫停和全屏,它看起來像一個system player
,如此快速和簡單。Objc - 如何在這樣的UIView內添加視頻播放器?
我在UIView裏面使用了UIWebView and HTML5
,但是UIWebView太慢了,不能沒有全屏播放。
如果此播放器是UIObject
或某些UIView的東西,我該如何添加它?
感謝〜
您可以添加一個視頻播放器的使用UIView
爲MPMoviePlayerController
。
這是蘋果示例代碼,它就是這樣。 它可以從互聯網上傳輸視頻以及播放本地電影文件。
您可以通過更改moviePlayer controlStyle
屬性來更改播放控件。
您可以在下面找到
http://developer.apple.com/library/ios/#samplecode/MoviePlayer_iPhone/Introduction/Intro.html
從鏈接的示例代碼的MPMoviePlayerController從iOS版9 棄用現在,您可以插入視頻與AVPlayerViewController iOS的項目。
例如:
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
...
@property (nonatomic, retain) AVPlayerViewController *playerViewController;
...
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *stringVideoName = @"video.m4v";
NSString *stringVideoPath = [[NSBundle mainBundle] pathForResource:stringVideoName ofType:nil];
NSAssert(stringVideoPath, @"Expected not nil video file");
NSURL *urlVideoFile = [NSURL fileURLWithPath:stringVideoPath];
NSAssert(urlVideoFile, @"Expected not nil video url");
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.player = [AVPlayer playerWithURL:urlVideoFile];
_playerViewController.view.frame = self.view.bounds;
_playerViewController.showsPlaybackControls = YES;
[self.view addSubview:_playerViewController.view];
self.view.autoresizesSubviews = YES;
}
只是複製和粘貼,而且效果很好。非常感謝 – smoothumut
我需要播放YouTube網址,所以我在urlVideoFile中寫入了我的nsurl。但注意到我的觀點顯示。 請提出建議。 – Chandni
謝謝尤,我用'MPMoviePlayerController'得到它,但還是有問題,當我用視頻觀看另一個視圖時,視頻無法停下來並播放。 – yellow
@yellow,在你推動另一個視圖之前,停止'MPMoviePlayerController', '[myMoviePlayer stop]'將會做到這一點。 或者如果你只是想暫停視頻,你可以使用'[myMoviePlayer pause]'。 通過'MPMoviePlayerController'類參考[here](http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html) – sunhsiv
thx,明白了。 @ viktoR,你真是個好人。 – yellow