ios 視頻播放代碼Demo

jopen 10年前發布 | 81K 次閱讀 IOS iOS開發 移動開發

    方法一:

- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    // Do any additional setup after loading the view.    
    //利用自帶MPMoviePlayerController來實現視頻播放,首先要在 項目中導入MediaPlayer.Framework框架包.  
    //在試圖控制器中導入#import "MediaPlayer/MPMoviePlayerController.h"  
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];  
    imageView.image = [UIImage imageNamed:@"00013"];  
    [self.view addSubview:imageView];  
    [imageView release];  

    // 播放視頻按鈕  
    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeSystem];  
    playButton.frame = CGRectMake(200, 30, 100, 30);  
    [playButton addTarget:self action:@selector(playClick:) forControlEvents:UIControlEventTouchUpInside];  
    [playButton setTitle:@"播放視頻" forState:UIControlStateNormal];  

    playButton.backgroundColor = [UIColor greenColor];  
    playButton.layer.cornerRadius = 5;  
    playButton.layer.masksToBounds = YES;  
    [self.view addSubview:playButton];  
}  

- (void)playClick:(UIButton *)btn  
{  
    //視頻文件路徑,此視頻已經存入項目包中.屬于本地播放  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];  
    //視頻URL  
    NSURL *url = [NSURL fileURLWithPath:path];  
    //視頻播放對象  
    MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:url];  
    movie.controlStyle = MPMovieControlStyleFullscreen;  
    [movie.view setFrame:self.view.bounds];  
    movie.initialPlaybackTime = -1;  
    [self.view addSubview:movie.view];  
    //注冊一個播放結束的通知, 當播放結束時, 監聽到并且做一些處理  
    //播放器自帶有播放通知的功能, 在此僅僅只需要注冊觀察者監聽通知的即可  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification  
      object:movie];  
    [movie play];  
}  

- (void)myMovieFinishedCallback:(NSNotification *)notify  
{  
    //視頻播放對象  
    MPMoviePlayerController *theMovie = [notify object];  
    //銷毀播放通知  
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:(theMovie)];  
    //釋放視頻對象  
    [theMovie.view release];  
}  

方法二:  
- (void)viewDidLoad  
{  
    //首先要在 項目中導入MediaPlayer.Framework框架包.  
    //在試圖控制器中導入#import <MediaPlayer/MediaPlayer.h>  
    [super viewDidLoad];  
    // Do any additional setup after loading the view.  
    NSURL *videoURL;  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];  
    //判斷是網絡地址還是本地播放地址  
    if ([path hasPrefix:@"http://"]) {  
        videoURL = [NSURL URLWithString:path];   
    }else{  
        videoURL = [NSURL fileURLWithPath:path];  
    }  
    MPMoviePlayerViewController *_moviePlayerController= [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];  
    [_moviePlayerController.view setFrame:CGRectMake(0,100,320,200)];  
    _moviePlayerController.moviePlayer.movieSourceType=MPMovieSourceTypeFile;  
    [_moviePlayerController.moviePlayer setScalingMode:MPMovieScalingModeNone];  
    [_moviePlayerController.moviePlayer setRepeatMode:MPMovieRepeatModeNone];  
    [_moviePlayerController.moviePlayer setControlStyle:MPMovieControlModeVolumeOnly];  
    [_moviePlayerController.moviePlayer setFullscreen:NO animated:YES];  
    [_moviePlayerController.moviePlayer play];  
    //視頻播放組件的容器,加這個容器是為了兼容iOS6,如果不加容器在iOS7下面沒有任何問題,如果在iOS6下面視頻的播放畫面會自動鋪滿self.view;  
    UIView *moviePlayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];  
    [self.view addSubview:moviePlayView];  
    [moviePlayView addSubview:[_moviePlayerController.moviePlayer view]];  
}  </pre><br />
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!