原文:cocoa自学 - 人人小站
首先需要引用framework:MediaPlayer.framework
然后在头文件中
1#import
2@property (nonatomic,retain) MPMoviePlayerController *player;
在M文件中开始实现方法
1/**
2@method 播放视频
3*/
4- (void)playMovie:(NSString *)fileURL{
5//本地视频文件路径
6// NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp4"];
7//网络视频URL
8NSURL *url = [NSURL URLWithString:fileURL];
9// NSLog(@"%@", url);
10//视频播放对象
11self.player = [[MPMoviePlayerController alloc] init];
12//设置为流媒体模式
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// 注册一个播放结束的通知
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 -------------------视频播放结束委托--------------------
30/*
31@method 当视频播放完毕释放对象
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}
通过调用[self playMovie:videoURL];就可以播放视频文件了