你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios播放視頻demo

ios播放視頻demo

編輯:IOS開發綜合

今天要用到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


 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved