iphone之MPMoviePlayer 视频播放(转载 3.2之前与3.3之前)
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface View24 : UIViewController
{
//sdk3.2以后
MPMoviePlayerViewController *_moviePlayViewController;
//sdk3.2以前
MPMoviePlayerController *_moviePlayerController;
}
//远程视频事件
- (IBAction) playURLVideo;
//本地视频事件
- (IBAction) playFileVideo;
@property (nonatomic,retain) MPMoviePlayerViewController *_moviePlayViewController;
@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];
MPMoviePlayerViewController* tmpMoviePlayViewController=[[MPMoviePlayerViewController alloc]initWithContentURL:url];
if (tmpMoviePlayViewController)
{
self._moviePlayViewController=tmpMoviePlayViewController;
[self presentMoviePlayerViewControllerAnimated:_moviePlayViewController];
_moviePlayViewController.moviePlayer.movieSourceType =MPMovieSourceTypeFile;
[_moviePlayViewController.moviePlayerplay];
}
[tmpMoviePlayViewController release];
[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(playbackDidFinish)name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
//远程视频事件
- (IBAction) playURLVideo
{
NSString*videoPath =@"http://www.subfurther.com/video/running-start-iphone.m4v";
if (videoPath == NULL)
return;
[selfinitAndPlay:videoPath];
}
-(void) initAndPlay:(NSString *)videoURL
{
if([videoURL rangeOfString:@"http://"].location!=NSNotFound||[videoURL rangeOfString:@"https://"].location!=NSNotFound)
{
NSURL *URL = [[NSURLalloc] initWithString:videoURL];
if (URL) {
if([[[UIDevice currentDevice] systemVersion] doubleValue] >=3.2)
{//3。2以后
MPMoviePlayerViewController* tmpMoviePlayViewController=[[MPMoviePlayerViewController alloc]initWithContentURL:URL];
if (tmpMoviePlayViewController)
{
self._moviePlayViewController=tmpMoviePlayViewController;
[self presentMoviePlayerViewControllerAnimated:_moviePlayViewController];
_moviePlayViewController.moviePlayer.movieSourceType =MPMovieSourceTypeFile;
[_moviePlayViewController.moviePlayerplay];
}
[tmpMoviePlayViewController release];
}
elseif([[[UIDevice currentDevice] systemVersion] doubleValue] <3.2)
{//3。2以前
MPMoviePlayerController* tmpMoviePlayController=[[MPMoviePlayerController alloc]initWithContentURL:URL];
if (tmpMoviePlayController)
{
self._moviePlayerController=tmpMoviePlayController;
[_moviePlayerController play];
}
[tmpMoviePlayController release];
}
//视频播放完成通知
[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(playbackDidFinish)name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
[URL release];
}
}
//视频播放完成后事件
- (void) playbackDidFinish
{
[[NSNotificationCenter defaultCenter]removeObserver:selfname:MPMoviePlayerPlaybackDidFinishNotification object:nil];
if(_moviePlayViewController)
{
[self dismissMoviePlayerViewControllerAnimated];
[self._moviePlayViewController.moviePlayerstop];
_moviePlayViewController.moviePlayer.initialPlaybackTime=-1.0;
[_moviePlayViewController release];
_moviePlayViewController=nil;
}
if(_moviePlayerController)
{
[self._moviePlayerController stop];
_moviePlayerController.initialPlaybackTime= -1.0;
[_moviePlayerController release];
_moviePlayerController =nil;
}
}