2009年08月23日, 1:12 下午
在使用MP库之前是需要将其导入的,上次忘记了,这回将截图发上来
首先在左侧找到Targets
然后打开,双击这个工程名
你就可以看到下面的图了
点击左下角的+号
找到MediaPlayer.framework
然后点Add,完成!!
之后就只需要在需要使用库的地方添加下面的代码拉~
#import "MediaPlayer/MediaPlayer.h"
简单iPhone视频播放器(1)
2009年08月20日, 6:11 下午
本程序源代码,不包含视频文件
视频播放在iPhone中是再重要不过了,今天要在30行内解决iPhone视频播放的问题!
1,建立工程MPtest1
2, 建立一个UIViewController类 MyMPViewController
3, 设置MPtest1AppDelegate初始化并添加MyMPViewController的view到window
@interface MPtest1AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MyMPViewController *mpViewController;
}
@end
- (void)applicationDidFinishLaunching:(UIApplication *)application {
mpViewController = [[MyMPViewController alloc] init];
[window addSubview:mpViewController.view];
[window makeKeyAndVisible];
}
4, 设置MyMPViewController在一个按钮点击后播放视频
@interface MyMPViewController : UIViewController {
UIButton *playButton;
}
@end
@interface MyMPViewController()
- (void)playVideo;
@end
@implementation MyMPViewController
- (id)init
{
if (self = [super init]) {
playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
//在这里初始化那个按键
playButton.frame = CGRectMake(100, 100, 100, 30);
[self.view addSubview:playButton];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[playButton setTitle:@"播放" forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(playVideo) forControlEvents:UIControlEventTouchUpInside];
//设置按键属性,然后添加点击后触发的方法函数
}
- (void)playVideo
{
MPMoviePlayerController *moviePlayer;
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"]]];
//初始化视频播放器对象,并传入被播放文件的地址
moviePlayer.movieControlMode = MPMovieControlModeDefault;
[moviePlayer play];
//此处有内存溢出,简单程序就先算了...
}
- (void)dealloc {
[playButton release];
[super dealloc];
}
@end
5, 编译运行
————————————————
整个程序非常简单,排除掉没有用的,必须用的,真正MP播放的部分也不到10行。
简单iPhone视频播放器(1)补
2009年07月13日, 12:41 下午
N 以下是一个非常简单的视频播放代码,并且可以简单的理解Cocoa中的通告使用方法。
- (void)playMovieAtURL:(NSURL*)theURL //简单的方法,传入一个视频地址
{
MPMoviePlayerController *thePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:theURL]; //初始化播放器
thePlayer.scalingMode = MPMovieScalingModeAspectFill; //下面有详细的缩放解释
//thePlayer.userCanShowTransportConstrols = NO; 这个是苹果文档上的原始代码,是错的...
thePlayer.movieControlMode = MPMovieControlModeDefault; //缺省视频控制
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification thePlayer];
//注册本地类为thePlayer的通告对象,通告方法为“视频播放结束”,
//回应方法为myMovieFinishedCallback
//也就是说,当视频播放结束以后,运行该方法
[thePlayer play]; //播放!
}
- (void)myMovieFinishedCallback:(NSNotification*)aNotification
{
MPMoviePlayerController *thePlayer = [aNotification object];
//从通告中导入这个播放器对象,如果播放器是单独的类成员,那就不用这步咯。
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
thePlayer];
//这步非常非常重要,一定要将被监听对象卸载,
//否则本地对象卸载后,监听对象为nil,软件会崩溃的
[thePlayer release]; //释放视频对象
}
scalingMode缩放模式的四种参数:
-MPMovieScalingModeNone
-不对视频进行缩放
-MPMovieScalingModeAspectFit
-视频缩放到内框,4比3的视频会在左右留下黑框
-MPMovieScalingModeAspectFill
-视频缩放到外框,4比3的视频上下会被削掉一部分
-MPMovieScalingModeFill
-视频被拉伸,失去原始比例