MyMoviePlayerController.h 播放器

 MoviePlayerController的初始化可以接受一个NSURL的参数,指定播放的视频资源文件,既可以是本地的,也可以是远程的。在旧的SDK中,MoviePlayerController只要实例化后调用play方法就可以自动全屏播放了,但在新SDK中你会发现同样的代码不会有任何效果,必须另外作些Hack处理(这意味着,如果你开发所用的SDK是3.2之前的,则编译后的程序只能发布到iPhone OS 3的设备上运行,所以赶紧升级你的SDK到4.0或更高版本吧)。我们把这些处理与MoviePlayerController组合在一起,放到一个新的类MyMoviePlayerController中(读者先想一下为什么不直接放到StreamMoviePlayerViewController类中)。在实现之前要先将MadiaPlayer.framework添加到项目中,方法是在Group& Files窗格中的Framework文件夹右击选择Add菜单下的Existing Frameworks命令,在弹出的framework列表中选择MadiaPlayer.framework后点击Add按钮。如果你在framework列表中找不到MadiaPlayer.framework,则需要点击Add other…,展开到 /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/System/Library/Frameworks目录,就可以看到MadiaPlayer.framework了。

 

 

  接下来,我们来实现这个类:

 

MyMoviePlayerController.h

 

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface MyMoivePlayerController : UIViewController {
MPMoviePlayerController *moviePlayer;
}

- (void)playMovieWithURL:(NSURL *)movieURL;

@end

 

 

 

 

 

MyMoviePlayerController.m

 

 

 


#import "MyMoivePlayerController.h"


@implementation MyMoivePlayerController

-(void) playMovieWithURL:(NSURL *)movieURL
{
// initialize a new MPMoviePlayerController object with the specified URL
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

//for 4.0
if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)])
{
// Set movie player layout
[moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[moviePlayer setScalingMode:MPMovieScalingModeFill];

// May help to reduce latency
[moviePlayer prepareToPlay];

// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
else
{
moviePlayer.scalingMode = MPMovieScalingModeFill;

// Register to receive a notification when the movie is in memory and ready to play.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
}

// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}

- (void) moviePlayerLoadStateChanged:(NSNotification*)notification
{
// Unless state is unknown, start playback
if ([moviePlayer 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)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];

// Set frame of movieplayer
[[moviePlayer view] setFrame:CGRectMake(0, 0, 480, 320)];

// Add movie player as subview
[[self view] addSubview:[moviePlayer view]];

// Play the movie
[moviePlayer play];
}
}

- (void) moviePreloadDidFinish:(NSNotification*)notification
{
// Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];

// Play the movie
[moviePlayer play];
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
// Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];

[self dismissModalViewControllerAnimated:YES];

[moviePlayer release];
}


- (void)didReceiveMemoryWarning {
// Releases the view if it doesn"t have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren"t in use.
}

- (void)viewDidUnload {
moviePlayer = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}


@end
http://www.byywee.com/page/M0/S464/464389.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值