ios视频

MPMoviePlayerViewController视频播放。添加MediaPlayer.framework

示例代码如下:

//
//  ViewController.h
//  MoviePlayTest
//  视频播放
//  Created by Dwen on 13-2-28.
//  Copyright (c) 2013年 Dwen. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
//本地视频播放
- (IBAction)playMovie:(id)sender;
//远程视频播放
- (IBAction)remote:(id)sender;

@end
//
//  ViewController.m
//  MoviePlayTest
//  视频播放:支持的文件类型包括MOV、MP4、MPV、M4V、3GP、MP3、AIFF和M4A.
//  Created by Dwen on 13-2-28.
//  Copyright (c) 2013年 Dwen. All rights reserved.
//

#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>

#define PATHSTRING @"http://www.archive.org/download/bb_poor_cinderella/bb_poor_cinderella_512kb.mp4"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"系统名称:%@ ,系统版本: %@",[[UIDevice currentDevice] systemName],[[UIDevice currentDevice] systemVersion]);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//本地播放
- (IBAction)playMovie:(id)sender {    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp4"];
    NSURL *URL = [NSURL fileURLWithPath:path];
    if (URL) {
        MPMoviePlayerViewController* tmpMoviePlayViewController=[[MPMoviePlayerViewController alloc] initWithContentURL:URL];
        //视频资源类型
        tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
        //播放结束后通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:tmpMoviePlayViewController];
        //播放视频
        [tmpMoviePlayViewController.moviePlayer play];
        //动画跳转界面
        [self presentMoviePlayerViewControllerAnimated:tmpMoviePlayViewController];
        NSLog(@"play...");
    }
}

//远程播放
- (IBAction)remote:(id)sender {
//    NSURL *URL = [[NSURL alloc] initFileURLWithPath:PATHSTRING];
    NSURL *URL = [NSURL URLWithString:PATHSTRING];
    if (URL) {
        MPMoviePlayerViewController *tmpMoviePlayViewController=[[MPMoviePlayerViewController alloc] initWithContentURL:URL];
        [self presentMoviePlayerViewControllerAnimated:tmpMoviePlayViewController];
        tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:tmpMoviePlayViewController];
        [tmpMoviePlayViewController.moviePlayer play];
        NSLog(@"play...");
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"文件不存在" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

//播完后关闭视频
-(void)playbackDidFinish:(NSNotification *)noti
{
    MPMoviePlayerViewController * mpv = [noti object];
    [mpv dismissMoviePlayerViewControllerAnimated];
}
@end

实现过程中的问题:

1、

//保存当前视频播放时间通知(**为解决当正在播放时,用户按home键使程序进入后台并暂停工作,当用户再次点击程序时,需回到开始退出视频的播放点继续播放)

- (void)viewDidLoad{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveCurrentPalyTime) name:@"SaveCurrentPalyBackTime" object:nil];
    //继续播放通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(continuePlay) name:@"ContinuePlayBack" object:nil];

}

- (void)viewDidUnload{
    //清除通知,不然会报异常
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

应用程序Delegate.m文件中

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    //通知方式保存当前视频播放时间
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SaveCurrentPalyBackTime" object:self];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    //继续播放视频通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ContinuePlayBack" object:self];
}

猜你喜欢

转载自wenxin2009.iteye.com/blog/1817820