方法一:
- (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的更多相关文章

  1. [iOS]一行代码集成空白页面占位图(基于runtime+MJRefresh思想)

    2018年01月03日阅读 2472   [iOS]一行代码集成空白页面占位图(基于runtime+MJRefresh思想) LYEmptyView 此框架是本人在5,6个月前,公司启动新项目的时候, ...

  2. 最全的iOS物理引擎demo

    概述 最全的iOS物理引擎demo,实现重力.碰撞.推力.摆动.碰撞+重力.重力弹跳.仿摩拜单车贴纸效果.防iMessage滚动效果.防百度外卖首页重力感应等效果! 详细 代码下载:http://ww ...

  3. iOS视频播放(AVFoundation)

    iOS视频播放(AVFoundation) 关于iOS平台的音视频处理,苹果官方提供了OC和swift接口的AVFoundation框架,可以进行各种音频播放和剪辑,底层实现使用了GPU加速,编解码效 ...

  4. iOS: ios视频播放(MPMediaPlayerController,AVPlayer,AVPlayerViewcontroller、ffmpeg-AVPlayer)

    介绍: 和音频播放一样,ios也提供个很多的API.如mediaPlayer.framework下的MPMediaPlayerController.AVFounditon.framework下的AVP ...

  5. Cordova - 与iOS原生代码交互2(使用Swift开发Cordova的自定义插件)

    在前一篇文章中我介绍了如何通过 js 与原生代码进行交互(Cordova - 与iOS原生代码交互1(通过JS调用Swift方法)),当时是直接对Cordova生成的iOS工程项目进行编辑操作的(添加 ...

  6. Web程序员开发App系列 - 调试Android和IOS手机代码(补图)

    Web程序员开发App系列 Web程序员开发App系列 - 认识HBuilder Web程序员开发App系列 - 申请苹果开发者账号 Web程序员开发App系列 - 调试Android和iOS手机代码 ...

  7. ios统计代码行数

    要统计ios开发代码,包括头文件的,终端命令进入项目目录下,命令如下 列出每个文件的行数: find . -name "*.m" -or -name "*.h" ...

  8. 嵌入式 hi3518x平台h264+g711a封装mp4代码demo

    先看代码吧,有代码有真相,具体代码的demo(下载demo的朋友请勿在网上上传我的demo,谢谢)下载连接为: http://download.csdn.net/detail/skdkjxy/8071 ...

  9. iOS: 在代码中使用Autolayout (2) – intrinsicContentSize和Content Hugging Priority【转】

    原文:http://www.mgenware.com/blog/?p=491 接上文:iOS: 在代码中使用Autolayout (1) – 按比例缩放和优先级. 我们继续来看在代码中使用Autola ...

随机推荐

  1. eclipse 安装svn插件记录

    每个人都有自己喜欢的和习惯的一套开发环境,其中对于喜欢用eclipse的同学来说.subclipse插件可以说是必不可少的插件了. 他的安装有两种方法,一种是在线安装.然而这种安装实在是太慢了,无法忍 ...

  2. Java 6中类路径ClassPath对通配符的支持

    在java 6之前,如果我们的应用依赖多个jar包,通常是将所有jar包文件枚举设置到环境变量CLASSPATH或者命令行参数-classpath(-cp)中.通常我们需要写一段shell脚本实现该功 ...

  3. 移动web——bootstrap模板

    基本概念 1.bootstrap就是在媒体查询技术出现以后才开始出现的 2.此技术使响应式开发变得十分轻松,最大特点就是栅格系统(什么设备上如何显示)以及响应式工具(是否可见) 基本模板 <!D ...

  4. html5——文本阴影

    基本结构 text-shadow: 30px 23px 31px #;/* 文字阴影: 水平位移 垂直位移 模糊程度 阴影颜色*/ 凹凸文字 <!DOCTYPE html> <htm ...

  5. pycuda installation error: command 'gcc' failed with exit status 1

    原文:python采坑之路 Setup script exited with error: command 'gcc' failed with exit status 1 伴随出现"cuda ...

  6. python 将中文转拼音后填充到url做参数并写入excel

    闲着没事写了个小工具,将中文转拼音后填充到url做参数并写如excel 一.先看下演示,是个什么东西 二.代码 代码用到一个中文转拼音的库,库是网上下的,稍微做了下修改,已经找不原来下载的地址了,然后 ...

  7. Python_多线程1(创建线程,简单线程同步)

    threading 模块除了包含 _thread 模块中的所有方法外,还提供的其他方法: threading.currentThread(): 返回当前的线程变量. threading.enumera ...

  8. vue.js---利用vue cli脚手架工具+webpack创建项目遇到的坑

    1.Eslint js代码规范报错 WARNING Compiled with 2 warnings 10:43:26 ✘ http://eslint.org/docs/rules/quotes St ...

  9. Linux下源码安装Peach-2.3.8教程

    在peach文件夹下运行 python peach.py ./samples/HelloWorld.xml 提示先安装4Suite-XML. 根据提示在dependences文件夹下安装,出现两次错误 ...

  10. python 易忘操作整理

    >>> l=[1,2,3,4,5] >>> s='$'.join(str(i) for i in l) >>> print(s,end=" ...