iOS视频——视频文件、播放视频
视频文件
视频文件格式很多:
1、AVI文件,是将音频与视频同步组合在一起的文件格式。有损压缩方式,压缩比较高,画面质量不是太好。
2、WMV文件,同等质量下,体积非常小,适合网上传输与播放。视频传输有延迟。必须在Windows上。
3、RMVB文件,最大特点是在保证文件清晰度的同时具有体积小巧的特点。
4、3GP文件,新的移动设备标准格式,体积小,移动性强,在PC上兼容性差。
5、MOV文件,苹果开发的一种音频、视频文件格式,用于存储常用数字媒体类型。包含很多轨道,画面效果比AVI要稍微好一些。
6、MP4文件。
7、M4V文件,苹果公司创造,为iOS设备所使用。
播放视频
有很多种方法,主要使用MediaPlayer框架中的MPMoviePlayerController或MPMoviePlayerViewController
使用AVFoundation框架中的AVPlayer和AVQueuePlayer。
使用MediaPlayer框架
一种高层次API,控制不是特别的好。
下面通过一个实例,展示该如何用:
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>@interface ViewController ()@property (nonatomic, strong) MPMoviePlayerViewController *moviePlayerView;
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;- (IBAction)useMPMoviePlayerController:(id)sender;
- (IBAction)useMPMoviePlayerViewController:(id)sender;-(NSURL *)p_movieURL;
-(void)p_playbackFinished4MoviePlayerController:(NSNotification *)notification;
-(void)p_doneButtonClick:(NSNotification *)notification;@end@implementation ViewController- (void)viewDidLoad
{[super viewDidLoad];
}-(NSURL *)p_movieURL
{NSBundle *bundle = [NSBundle mainBundle];NSString *moviePath = [bundle pathForResource:@"YY" ofType:@"mp4"];if (moviePath){return [NSURL fileURLWithPath:moviePath];}else{return nil;}
}- (IBAction)useMPMoviePlayerViewController:(id)sender
{if (_moviePlayerView == nil){_moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:[self p_movieURL]];_moviePlayerView.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;_moviePlayerView.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(p_playbackFinished4MoviePlayerController:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];}[self presentMoviePlayerViewControllerAnimated:_moviePlayerView];
}- (IBAction)useMPMoviePlayerController:(id)sender
{if (_moviePlayer == nil){[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(p_playbackFinished4MoviePlayerController:)name:MPMoviePlayerPlaybackDidFinishNotificationobject:nil];[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(p_doneButtonClick:)name:MPMoviePlayerWillExitFullscreenNotificationobject:nil];_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self p_movieURL]];_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;_moviePlayer.controlStyle = MPMovieControlStyleFullscreen;[self.view addSubview:_moviePlayer.view];}[_moviePlayer play];[_moviePlayer setFullscreen:YES animated:YES];
}-(void)p_playbackFinished4MoviePlayerController:(NSNotification *)notification
{NSLog(@"使用MPMoviePlayerController播放完成。");[[NSNotificationCenter defaultCenter] removeObserver:self];[_moviePlayer stop];[_moviePlayer.view removeFromSuperview];_moviePlayer = nil;
}-(void)p_doneButtonClick:(NSNotification *)notification
{NSLog(@"退出全屏");if (_moviePlayer.playbackState == MPMoviePlaybackStateStopped){[_moviePlayer.view removeFromSuperview];_moviePlayer = nil;}
}@end
使用AVFoundation框架
提供更加细粒度的控制。可以添加自己设计的播放控制控件、同时播放多个视频、在动画中夹杂一些视频片段。
一个实例:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>@interface ViewController (){id timeObserver;BOOL isPlaying;
}@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UIToolbar *toolBar;@property (nonatomic, weak) AVPlayer *avPlayer;
@property (nonatomic, weak) AVPlayerLayer *layer;
@property (nonatomic, strong) AVPlayerItem *playerItem;- (IBAction)play:(id)sender;
- (IBAction)seek:(id)sender;
-(void)p_addObserver;
-(void)p_playerItemDidReachEnd:(NSNotification *)notification;@end@implementation ViewController- (void)viewDidLoad
{[super viewDidLoad];NSString *filePath = [[NSBundle mainBundle] pathForResource:@"YY" ofType:@"mp4"];NSURL *fileUrl = [NSURL fileURLWithPath:filePath];AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileUrl options:nil];self.playerItem = [AVPlayerItem playerItemWithAsset:asset];_avPlayer = [AVPlayer playerWithPlayerItem:_playerItem];_layer = [AVPlayerLayer playerLayerWithPlayer:_avPlayer];float scale = 1.776;_layer.frame = CGRectMake(0, -290, self.view.frame.size.width * scale, self.view.frame.size.height *scale);[self.view.layer insertSublayer:_layer atIndex:0];double duration = CMTimeGetSeconds(asset.duration);_slider.maximumValue = duration;_slider.minimumValue = 0;isPlaying = NO;
}- (IBAction)seek:(id)sender
{float value = _slider.value;[_avPlayer seekToTime:CMTimeMakeWithSeconds(value, 10)];
}- (IBAction)play:(id)sender
{UIBarButtonItem *item1;if (!isPlaying){[self p_addObserver];[_avPlayer seekToTime:kCMTimeZero];[_avPlayer play];isPlaying = YES;item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(play:)];}else{isPlaying = NO;item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(play:)];[_avPlayer pause];}NSMutableArray *items = [[NSMutableArray alloc] initWithArray:[_toolBar items]];[items replaceObjectAtIndex:0 withObject:item1];[_toolBar setItems:items];
}-(void)p_addObserver
{if (timeObserver == nil){[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(p_playerItemDidReachEnd:)name:AVPlayerItemDidPlayToEndTimeNotificationobject:_playerItem];timeObserver = [_avPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 10)queue:dispatch_get_main_queue()usingBlock:^(CMTime time){float duration = CMTimeGetSeconds(self->_avPlayer.currentTime);self->_slider.value = duration;}];}
}-(void)p_playerItemDidReachEnd:(NSNotification *)notification
{NSLog(@"播放完成");if (timeObserver){[_avPlayer removeTimeObserver:timeObserver];timeObserver = nil;_slider.value = 0;isPlaying = NO;[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(play:)];NSMutableArray *items = [NSMutableArray arrayWithArray:[_toolBar items]];[items replaceObjectAtIndex:0 withObject:item1];[_toolBar setItems:items];}
}@end
iOS视频——视频文件、播放视频相关推荐
- h5 ios Safair下载文件自动添加.html导致文件乱码问题,ios不能使用接口播放视频的问题
需求:h5 页面 下载pdf,doc,docx,xlsx,xls 文件的功能 在安卓手机和 ios上的uc浏览器是可以正常的 下载或者预览的 但是在ios的Safair 下载时会被自动添加html尾缀 ...
- h5点播播放mp4视频遇到的坑,ios的h5不能播放视频等
背景 h5的出现对多媒体在网页上的视频播放提供了支持,以前网页播放视频基本依赖于flash等插件.而h5的video标签实现了网页播放视频无插件化.因此,h5的出现给网页视频播放带来极大的便捷性,目前 ...
- SpringBoot + thymeleaf + mysql + html<video> 实现读取视频列表并播放视频
SpringBoot + thymeleaf + mysql + html 实现读取视频列表并播放视频 通过读取数据库video表获取当前视频的视频名.视频地址,展示至前端页面videorecord. ...
- Springboot+Minio通过分片下载解决IOS下H5无法播放视频问题
一.环境说明 JDK 1.8 Springboot 2.7.5 Minio 8.4.5 Vue3实现的微信公众号网页 二.问题描述 当前项目是基于springboot和vue3的前后端分离架构,前端目 ...
- iOS 音视频录制之播放视频,AVPlayer可播放本地视频和在线视频
文章目录 在开发中,单纯使用AVPlayer类是无法显示视频的,要将视频层添加至AVPlayerLayer中,这样才能将视频显示出来,所以先在ViewController的@interface中添加以 ...
- iOS开发-停止WebView播放视频/音频 1
很多时候在WebView播放视频的时候,会有一些通知或者其他语音播报的内容,这个时候就要暂停WebView正在播放的视频了. 通过JS直接控制网页中的 video/media标签 // 停止视频播放 ...
- android打开视频噔_android: 播放视频
播放视频文件其实并不比播放音频文件复杂,主要是使用 VideoView 类来实现的.这个 类将视频的显示和控制集于一身,使得我们仅仅借助它就可以完成一个简易的视频播放器. VideoView 的用法和 ...
- ffmpeg解码H.264视频数据,MFC播放视频
ffmpeg 是一个完整的视频流解决方案,开源且有良好的跨平台性,ffmpeg具有强大的多媒体数据处理能力,能够实现视频的采集,多种视频格式间转换,给视频添加水印等多种功能,已被 VLC.Mplaye ...
- 多媒体——视频——利用视频视图VideoView播放视频
==================================================================================================== ...
- php视频提取音频,怎么提取视频中音频文件?视频文件如何分离提取出音频文件?视频转换成音频的方法...
快要过年啦,小编在这里提前祝大家新年快乐,万事如意,嘻嘻(#^.^#).今天就能回家啦,好激动٩(๑>◡ 首先呢,我们需要先通过上方的连接诶下载我们所要用到的软件,下载完成之后解压缩包,在打开的 ...
最新文章
- java为啥要捕捉异常_java – 为什么在捕获时使用IOexception而不是Exception?
- hdu 2897 巴什博弈变形
- 阿德莱德计算机专业排名,阿德莱德大学专业排名第几?2019年榜单揭晓!
- C++结构名、联合名、枚举名都是类型名
- Debug Assert Failed 怎么办?
- MySQL 优化器之Index merge Multi-Range Read MRR与Batched Key Access使用案例详解
- mysql数据库内置函数大全_MySQL数据库——内置函数
- python expected an indented block什么意思
- WPF 中的设备无关单位
- iPhone上编辑html,在iphone上重新格式化一个简单的html页面
- 美国Hack the Army 3.0 漏洞奖励计划启动
- 常见的Mule Esb下载地址
- WebAPI2使用Autofac实现IOC属性注入完美解决方案
- Mysql数据库内对查询结果去重复指令【重点】
- python 月初 月末
- linux防火墙(firewall、iptable)
- MATLAB—colormap设置颜色图
- 最新holer使用方法 如何使用外网访问自己主机的web应用
- 安笙机器人_演员动态周报 | 李晨王晓晨《北京西城故事》、张翰徐璐《若你安好便是晴天》、包贝尔辛芷蕾《我的机器人女友》...
- 华三交换机模拟器搭建和使用
热门文章
- MATLAB中nargin函数的用法nargin是用来判断输入变量个数的函数,这样就可以针对不同的情况执行不同的功能。通常可以用它来设定一些默认值。如下例所示: 函数文件 examp.m
- C++屏蔽map自动排序
- 电脑卡在系统logo处
- 基于ZYNQ 7000的1553B总线控制器测试系统的设计与实现
- hx1838 红外遥控(1):接收时序的解码
- NOI 1797:金银岛(C++)
- Rasa 3.x 学习系列-Rasa [3.4.1] - 2023-01-19新版本发布
- package import
- 学习Python的建议
- Android 模拟返回键、菜单键、主页键