#import <MediaPlayer/MediaPlayer.h>
@interface ViewController (){
MPMoviePlayerViewController *_moviePlayerController;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100, 100);
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)pressBtn
{
NSURL *url = [NSURL URLWithString:@"http://221.229.165.31:80/play/274CF5C996AFCE2C751D315B5D1BF131B8C08208/298088%5Fstandard.mp4"];
_moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
_moviePlayerController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[_moviePlayerController.moviePlayer prepareToPlay];
[_moviePlayerController.moviePlayer play];
_moviePlayerController.moviePlayer.backgroundView.backgroundColor=[UIColor whiteColor];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(my:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[self presentMoviePlayerViewControllerAnimated:_moviePlayerController];
}
-(void)my:(NSNotification *)noti
{
NSLog(@"%lu",(unsigned long)_moviePlayerController.moviePlayer.loadState);
}
每次都是加载一点然后就不加载了,也不是网速的问题,直接在模拟器的浏览器输入网址就可以播放。这是什么原因
我用AVPlayer很正常:
/// setup player
NSURL *url = [NSURL URLWithString:@"http://221.229.165.31:80/play/274CF5C996AFCE2C751D315B5D1BF131B8C08208/298088%5Fstandard.mp4"];
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
AVPlayer *player = [AVPlayer playerWithPlayerItem:item];
_player = player;
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
layer.frame = _playerView.bounds;
layer.videoGravity = AVLayerVideoGravityResizeAspect;
[_playerView.layer addSublayer:layer];
/// play video
[_player play];
MPMoviePlayerViewController下的self·view在arc下被提前释放了,将self.view 设置为全局的就可以播放出来了,试一下吧!可以的话点?