转自:http://blog.sina.com.cn/s/blog_4adf31ea0100r073.html
iphone的视频播放官方的例子有问题。这次是费了比较长的时间。
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface View24 : UIViewController
{
//sdk3.2以后
MPMoviePlayerViewControl
//sdk3.2以前
MPMoviePlayerController *_moviePlayerController;
}
//远程视频事件
- (IBAction) playURLVideo;
//本地视频事件
- (IBAction) playFileVideo;
@property (nonatomic,retain) MPMoviePlayerViewControl
@property (nonatomic,retain) MPMoviePlayerController *_moviePlayerController;
-(void) initAndPlay:(NSString *)videoURL;
@end
//本地视频事件
-(IBAction) playFileVideo
{//此方法只是 3。2以后的方法
NSString *path=[[NSBundle mainBundle] pathForResource:@"sophie"ofType:@"mov"];
NSURL *url=[[NSURL alloc] initFileURLWithPath:path];
MPMoviePlayerViewControl
if (tmpMoviePlayViewControll
{
self._moviePlayViewController=tmpMoviePlayViewControll
[self presentMoviePlayerViewCo
_moviePlayViewController.moviePlayer.movieSourceType =MPMovieSourceTypeFile;
[_moviePlayViewController.moviePlayer play];
}
[tmpMoviePlayViewControll
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(playbackDidFinish) name:MPMoviePlayerPlaybackDid
}
//远程视频事件
- (IBAction) playURLVideo
{
NSString *videoPath =@"http://www.subfurther.com/video/running-start-iphone.m4v";
if (videoPath == NULL)
return;
[self initAndPlay:videoPath];
}
-(void) initAndPlay:(NSString *)videoURL
{
NSURL *URL = [[NSURL alloc] initWithString:videoURL];
if (URL) {
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2)
{//3。2以后
MPMoviePlayerViewControl
if (tmpMoviePlayViewControll
{
self._moviePlayViewController=tmpMoviePlayViewControll
[self presentMoviePlayerViewCo
_moviePlayViewController.moviePlayer.movieSourceType =MPMovieSourceTypeFile;
[_moviePlayViewController.moviePlayer play];
}
[tmpMoviePlayViewControll
}
else if([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2)
{//3。2以前
MPMoviePlayerController* tmpMoviePlayController=[[MPMoviePlayerControlleralloc] initWithContentURL:URL];
if (tmpMoviePlayController)
{
self._moviePlayerController=tmpMoviePlayController;
[_moviePlayerController play];
}
[tmpMoviePlayController release];
}
//视频播放完成通知
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(playbackDidFinish) name:MPMoviePlayerPlaybackDid
}
[URL release];
}
//视频播放完成后事件
- (void) playbackDidFinish
{
}