1MediaPlayer內嵌的方式播放視頻
1.1 導入頭文件
#import "MediaPlayer/MediaPlayer.h"
1.2 定義MoviePlayerController類
創建MoviePlayerController類
player = [[MPMoviePlayerControlleralloc] init];
設置視頻路徑
NSString *path = [[NSBundlemainBundle] pathForResource:self.questiondata.titleimgofType:@"mp4"inDirectory:nil];
NSURL *movieURL = [NSURL fileURLWithPath:path];
player.contentURL = movieURL
配置 控件樣式
player.controlStyle = MPMovieControlStyleEmbedded;//內嵌的方式
player.controlStyle =MPMovieControlStyleFullscreen; //全屏的方式
1.3 將視頻控件添加到指定的地方
[player.viewsetFrame:CGRectMake(30,_viewHeight+10,270,97)];
[selfaddSubview:player.view];
[player play];
1.4 釋放控件
if (player!=nil) {
[ player stop ];
[player.viewremoveFromSuperview];
[playerrelease];
}
2 MediaPlayer播放過程中的事件捕獲和處理
本次舉例偵聽
// 注冊一個播放結束的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:movie];
//處理事件
/*
@method 當視頻播放完畢釋放對象
*/
-(void)myMovieFinishedCallback:(NSNotification*)notify
{
//視頻播放對象
MPMoviePlayerController* theMovie = [notify object];
//銷毀播放通知
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie.view removeFromSuperview];
// 釋放視頻對象
[theMovie release];
}
