MPMoviePlayerController和MPMoviePlayerViewController都是播放视频的原生类。有找到文章说是MPMoviePlayerController在4.0以后的被MPMoviePlayerViewController取代建议使用后者的,今天在使用时发现有个问题
#import "videoViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#define PATHSTRING @"http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8"
@interface videoViewController ()
@property(nonatomic,strong)MPMoviePlayerViewController* moviePlay;
@property(nonatomic,strong)MPMoviePlayerController* player;
@end
@implementation videoViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* filepath=[[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v" ];
// _moviePlay=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:filepath]];
// _moviePlay=[[MPMoviePlayerViewController alloc] initWithContentURL:[[NSURL alloc] initWithString:PATHSTRING]];
// _moviePlay.moviePlayer.view.hidden=NO;
// [_moviePlay.moviePlayer prepareToPlay];
// [self.view addSubview:_moviePlay.view];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movienotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
// [_moviePlay.moviePlayer play];
// _player=[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filepath]];
// _player.view.frame=CGRectMake(0, 0, 320, 460);
// [_player prepareToPlay];
// [self.view addSubview:_player.view];
// [_player play];
在将俩个对象做为全局变量时都能正常显示。
// MPMoviePlayerViewController* player=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:filepath]];
// [player.moviePlayer prepareToPlay];
// [self.view addSubview:player.view];
//// [self presentMoviePlayerViewControllerAnimated:player];
// [player.moviePlayer play];
MPMoviePlayerController* innerplay=[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filepath]];
// innerplay.view.
innerplay.view.frame=CGRectMake(0, 0, 320, 480);
[innerplay prepareToPlay];
[self.view addSubview: innerplay.view];
[innerplay play];
当作为局部变量的时候无论是addsubview还是 presentMoviePlayerViewControllerAnimated方法
上都不行。
之前问题已解决
原因:
由于项目使用的ARC内存管理方式,所以做为局部变量的MPMoviePlayerController会在方法执行结束之后作为方法的局部变量释放掉,照成了视频只能播放个几秒后面直接就黑屏的现象,而且在模拟器不能打断点调试的(详细见下面),所以会找不出是什么原因。今天又碰上了这个情况,刚好碰上有人回答。一想确实会照成这样的解决。贴上原网址:点击打开链接
关于在模拟器上加载在线视频项目中有断点存在时会crash的情况,只要去掉断掉就可以。具体原因看原链接:
音频:
- #import <AVFoundation/AVFoundation.h>
- NSError* err;
- AVAudioPlayer* player = [[AVAudioPlayer alloc]
- initWithContentsOfURL:[NSURL fileURLWithPath:
- [[NSBundle mainBundle]pathForResource:
- @"music" ofType:@"m4a"
- inDirectory:@"/"]]
- error:&err ];//使用本地URL创建