ios 視頻播放代碼Demo

來源:互聯網
上載者:User

標籤:nsstring   etc   mpm   tle   callback   hup   type   res   class   

方法一:- (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]];}


ios 視頻播放代碼Demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.