首先需要引用framework:MediaPlayer.framework
然后在頭文件中
1#import
2@property (nonatomic,retain) MPMoviePlayerController *player;
在M文件中開始實(shí)現(xiàn)方法
1/**
2@method 播放視頻
3*/
4- (void)playMovie:(NSString *)fileURL{
5//本地視頻文件路徑
6//? ? NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp4"];
7//網(wǎng)絡(luò)視頻URL
8NSURL *url = [NSURL URLWithString:fileURL];
9//? ? NSLog(@"%@", url);
10//視頻播放對象
11self.player = [[MPMoviePlayerController alloc] init];
12//設(shè)置為流媒體模式
13self.player.movieSourceType = MPMovieSourceTypeStreaming;
14[self.player setControlStyle:MPMovieControlStyleFullscreen];
15[self.player.view setFrame:self.view.bounds];
16[self.player setContentURL:url];
17self.player.fullscreen = YES;//全屏播放
18self.player.initialPlaybackTime = -1;
19[self.view addSubview:self.player.view];
20// 注冊一個(gè)播放結(jié)束的通知
21[[NSNotificationCenter defaultCenter] addObserver:self
22selector:@selector(myMovieFinishedCallback:)
23name:MPMoviePlayerPlaybackDidFinishNotification
24object:self.player];
25[self.player prepareToPlay];
26//? ? [sharedData stopLoading];
27[self.player play];
28}
29#pragma mark -------------------視頻播放結(jié)束委托--------------------
30/*
31@method 當(dāng)視頻播放完畢釋放對象
32*/
33- (void)myMovieFinishedCallback:(NSNotification*)notify
34{
35//視頻播放對象
36MPMoviePlayerController* theMovie = [notify object];
37//銷毀播放通知
38[[NSNotificationCenter defaultCenter] removeObserver:self
39name:MPMoviePlayerPlaybackDidFinishNotification
40object:theMovie];
41// 釋放視頻對象
42[theMovie.view removeFromSuperview];
43}
通過調(diào)用[self playMovie:videoURL];就可以播放視頻文件了