ios播放視頻demo

來源:互聯網
上載者:User

ios播放視頻demo

今天要用到ios原生態播放一段網路視頻,在此整理共用出來

 

首先需要給工程添加架構MediaPlayer.Framework

 

#import ViewController.h#import @interface ViewController (){    MPMoviePlayerViewController *playerViewController;    MPMoviePlayerController *player;        UIButton                *_playBtn; //播放按鈕    UIImageView             *_image;   //播放按鈕圖片    UIImageView             *_thumbImgView; //抓取視頻的圖片}@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    NSInteger VideoWidth = 320;    NSInteger VideoHeight = 200;    NSInteger VideoY = 44;    //重新初始化MPMoviePlayerController,否則擷取視頻的第一幀圖片,會引起無法播放的bug。    MPMoviePlayerController *pc = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@http://sm.domob.cn/ugc/151397.mp4]];        //擷取視頻的第一幀圖片    UIImage *videoThumbImg = [pc thumbnailImageAtTime:0 timeOption:MPMovieTimeOptionNearestKeyFrame];    _thumbImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, VideoY, VideoWidth, VideoHeight)];    [_thumbImgView setImage:videoThumbImg];    [self.view addSubview:_thumbImgView];        //設定播放按鈕    _playBtn = [UIButton buttonWithType:UIButtonTypeCustom];    _playBtn.backgroundColor = [UIColor clearColor];    [_playBtn setFrame:CGRectMake(0.0f, VideoY, VideoWidth, VideoHeight)];    [_playBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];    [_playBtn addTarget:self action:@selector(playVideo) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:_playBtn];        //設定播放按鈕的圖片    _image = [[UIImageView alloc]init];    _image.frame = CGRectMake(140,120, 50, 50);    [_image setImage:[UIImage imageNamed:@yss_ios_hy_huodong_touxiang.png]];    [self.view addSubview:_image];    }//播放視頻- (void) playVideo{    //根據視頻播放狀態,點擊視頻,出現播放按鈕圖片或者隱藏    if (player && player.playbackState == MPMoviePlaybackStatePlaying ) {        [player pause];        _image.hidden = NO;        return;    }else if (player && player.playbackState == MPMoviePlaybackStatePaused) {        _image.hidden = YES;        [player play];        return;    }        //介面剛顯示播放按鈕應顯示,所以調用時播放圖片應為隱藏    _image.hidden = YES;    NSInteger VideoWidth = 320;    NSInteger VideoHeight = 200;    NSInteger VideoY = 44;    //播放視頻    player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@http://sm.domob.cn/ugc/151397.mp4]];    player.view.frame = CGRectMake(0, VideoY, VideoWidth,VideoHeight);    player.controlStyle = MPMovieControlStyleNone;    player.repeatMode = MPMovieRepeatModeNone;    [player setFullscreen:YES animated:YES];    player.scalingMode = MPMovieScalingModeAspectFit;    [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(myMovieFinishedCallback:)                                                 name:MPMoviePlayerPlaybackDidFinishNotification                                               object:player];    [self.view insertSubview:player.view belowSubview:_playBtn];    [player play];}//播放視頻結束的回調-(void)myMovieFinishedCallback:(NSNotification*)notify{    //視頻播放對象    MPMoviePlayerController* theMovie = [notify object];    //銷毀播放通知    [[NSNotificationCenter defaultCenter] removeObserver:self                                                    name:MPMoviePlayerPlaybackDidFinishNotification                                                  object:theMovie];    [theMovie stop];    [theMovie.view removeFromSuperview];    //如果視頻播放停止了,顯示播放按鈕圖片    if (player && player.playbackState == MPMoviePlaybackStateStopped){        _image.hidden = NO;        [player stop];        return;    }}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{    return toInterfaceOrientation == UIInterfaceOrientationPortrait;}- (BOOL)shouldAutorotate{    return YES;}@end


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.