#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.moviePlayer play];
}
[tmpMoviePlayViewController release];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
//远程视频事件
- (IBAction) playURLVideo
{
NSString *videoPath =@"http://www.subfurther.com/video/running-start-iphone.m4v";
if (videoPath == NULL)
return;
[self initAndPlay:videoPath];
}
-(void) initAndPlay:(NSString *)videoURL
{
if ([videoURL rangeOfString:@"http://"].location!=NSNotFound||[videoURL rangeOfString:@"https://"].location!=NSNotFound)
{
NSURL *URL = [[NSURL alloc] 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.moviePlayer play];
}
[tmpMoviePlayViewController release];
}
else if([[[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:self selector:@selector(playbackDidFinish) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
[URL release];
}
}
//视频播放完成后事件
- (void) playbackDidFinish
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
if (_moviePlayViewController)
{
[self dismissMoviePlayerViewControllerAnimated];
[self._moviePlayViewController.moviePlayer stop];
_moviePlayViewController.moviePlayer.initialPlaybackTime=-1.0;
[_moviePlayViewController release];
_moviePlayViewController=nil;
}
if (_moviePlayerController)
{
[self._moviePlayerController stop];
_moviePlayerController.initialPlaybackTime = -1.0;
[_moviePlayerController release];
_moviePlayerController = nil;
}
}