Iphone中已经自定义好了视频播放类MPMoviePlayerController,我们只需调用既可,我自己封装好了一个视频播放类,下面就直接给大家上代码吧。
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h> //导入视频播放库
@interface CustomMoviePlayerViewController : UIViewController
{
MPMoviePlayerController *mp;
NSURL *movieURL; //视频地址
UIActivityIndicatorView *loadingAni; //加载动画
UILabel *label; //加载提醒
}
@property (nonatomic,retain) NSURL *movieURL;
//准备播放
- (void)readyPlayer;
@end
#import "CustomMoviePlayerViewController.h"
#pragma mark -
#pragma mark Compiler Directives & Static Variables
@implementation CustomMoviePlayerViewController
@synthesize movieURL;
- (void)viewDidLoad
{
[super viewDidLoad];
loadingAni = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 150, 37, 37)];
loadingAni.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[self.view addSubview:loadingAni];
label = [[UILabel alloc] initWithFrame:CGRectMake(130, 190, 80, 40)];
label.text = @"加载中...";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
[[self view] setBackgroundColor:[UIColor blackColor]];
[loadingAni startAnimating];
[self.view addSubview:label];
}
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification
{
[loadingAni stopAnimating];
[label removeFromSuperview];
// Unless state is unknown, start playback
if ([mp loadState] != MPMovieLoadStateUnknown)
{
// Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
// When tapping movie, status bar will appear, it shows up
// in portrait mode by default. Set orientation to landscape
//设置横屏
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
// Rotate the view for landscape playback
[[self view] setBounds:CGRectMake(0, 0, 480, 320)];
[[self view] setCenter:CGPointMake(160, 240)];
//选中当前view
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
// Set frame of movieplayer
[[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
// Add movie player as subview
[[self view] addSubview:[mp view]];
// Play the movie
[mp play];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
//[[UIApplication sharedApplication] setStatusBarHidden:YES];
//还原状态栏为默认状态
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
// Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissModalViewControllerAnimated:NO];
}
- (void) readyPlayer
{
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if ([mp respondsToSelector:@selector(loadState)])
{
// Set movie player layout
[mp setControlStyle:MPMovieControlStyleFullscreen]; //MPMovieControlStyleFullscreen //MPMovieControlStyleEmbedded
//满屏
[mp setFullscreen:YES];
// 有助于减少延迟
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
else
{
// Play the movie For 3.1.x devices
[mp play];
}
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void)dealloc
{
[mp release];
[movieURL release];
[loadingAni release];
[label release];
[super dealloc];
}
@end
上面为一个封装好的视频播放类,需要注意的是上面代码区分了3.1 和 以版本的
IOS差别,所以都可以直接去调用这个类。
调用时,例:
CustomMoviePlayerViewController *moviePlayer = [[[CustomMoviePlayerViewController alloc] init] autorelease];
//将视频地址传过去
moviePlayer.movieURL = [NSURL URLWithString:@"视频地址,网络地址和本地视频路径都可以"];
//然后播放就OK了
[moviePlayer readyPlayer];
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h> //导入视频播放库
@interface CustomMoviePlayerViewController : UIViewController
{
MPMoviePlayerController *mp;
NSURL *movieURL; //视频地址
UIActivityIndicatorView *loadingAni; //加载动画
UILabel *label; //加载提醒
}
@property (nonatomic,retain) NSURL *movieURL;
//准备播放
- (void)readyPlayer;
@end
#import "CustomMoviePlayerViewController.h"
#pragma mark -
#pragma mark Compiler Directives & Static Variables
@implementation CustomMoviePlayerViewController
@synthesize movieURL;
- (void)viewDidLoad
{
[super viewDidLoad];
loadingAni = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 150, 37, 37)];
loadingAni.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[self.view addSubview:loadingAni];
label = [[UILabel alloc] initWithFrame:CGRectMake(130, 190, 80, 40)];
label.text = @"加载中...";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
[[self view] setBackgroundColor:[UIColor blackColor]];
[loadingAni startAnimating];
[self.view addSubview:label];
}
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification
{
[loadingAni stopAnimating];
[label removeFromSuperview];
// Unless state is unknown, start playback
if ([mp loadState] != MPMovieLoadStateUnknown)
{
// Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
// When tapping movie, status bar will appear, it shows up
// in portrait mode by default. Set orientation to landscape
//设置横屏
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
// Rotate the view for landscape playback
[[self view] setBounds:CGRectMake(0, 0, 480, 320)];
[[self view] setCenter:CGPointMake(160, 240)];
//选中当前view
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
// Set frame of movieplayer
[[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
// Add movie player as subview
[[self view] addSubview:[mp view]];
// Play the movie
[mp play];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
//[[UIApplication sharedApplication] setStatusBarHidden:YES];
//还原状态栏为默认状态
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
// Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissModalViewControllerAnimated:NO];
}
- (void) readyPlayer
{
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if ([mp respondsToSelector:@selector(loadState)])
{
// Set movie player layout
[mp setControlStyle:MPMovieControlStyleFullscreen]; //MPMovieControlStyleFullscreen //MPMovieControlStyleEmbedded
//满屏
[mp setFullscreen:YES];
// 有助于减少延迟
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
else
{
// Play the movie For 3.1.x devices
[mp play];
}
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void)dealloc
{
[mp release];
[movieURL release];
[loadingAni release];
[label release];
[super dealloc];
}
@end
上面为一个封装好的视频播放类,需要注意的是上面代码区分了3.1 和 以版本的
IOS差别,所以都可以直接去调用这个类。
调用时,例:
CustomMoviePlayerViewController *moviePlayer = [[[CustomMoviePlayerViewController alloc] init] autorelease];
//将视频地址传过去
moviePlayer.movieURL = [NSURL URLWithString:@"视频地址,网络地址和本地视频路径都可以"];
//然后播放就OK了
[moviePlayer readyPlayer];
[self presentModalViewController:moviePlayer animated:NO];
转载自 http://blog.sina.com.cn/s/blog_8364f64b0100t0nn.html