简单iPhone视频播放器(1)
视频播放在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行。