ios播放视频demo

今天要用到ios原生态播放一段网络视频,在此整理共享出来

白白手游专栏http://blog.csdn.net/u010229677

首先需要给工程添加框架MediaPlayer.Framework

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


@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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值