多媒体 (系统的,用处不大)
2016-04-18 22:00
555 查看
音频
iOS里面共有四种专门实现播放音频的方式:
System Sound Services (系统声音服务)
OpenAL(跨平台的开源的音频处理接口)
Audio Queue Services (播放和录制音频服务)
AVAudioPlayer (高级音频播放器) : 只能播放一个完整音频, 完全下载好的
System Sound Services:是最底层也是最简单的声音播放服务, 通过调用AudioServicesPlaySystemSound 这个函数就可以播放一些简单的音频文件
使用场景 : 适合播放一些很小的提示或者警告音
局限性:
声音长度小于30秒
格式 : IMA4, WAV
不能控制播放的进度
调用方法后立即播放声音
没有循环播放和立体声音播放
OpenAL : 跨平台的开源音频处理借口
适合开发游戏的音频
官方教程:http://www.devdiv.com/thread-19636-1-1.html
http://www.cocoachina.com/bbs/read.php?tid-112679-page-1.html
AVAudioPlayer : 支持广泛的音频格式
AAC
AMR
ALAC
iLBC
IMA4
linearPCM
MP3
优势:
支持更多的格式
可以播放任意长度的音频
支持循环播放
可以同步播放多个音频文件
控制播放进度以及从音频的任意一点开始播放
导入框架 #import
<AVFoundation/AVFoundation.h>
//
获取到的需要播放的model
MusicModel
*model = [[MusicHandle
defaultHandle]
modelWithIndex:self.musicIndex];
//
音频的地址
NSString
*pathString = [[NSBundle
mainBundle]
pathForResource:model.musicName
ofType:model.musicType];
NSData
*data = [NSData
dataWithContentsOfFile:pathString];
//根据音频的data创建一个AVAudioPlayer对象
self.audioPlayer
= [[AVAudioPlayer
alloc]initWithData:data
error:nil];
//播放
[self.audioPlayer
play];
//暂停
[self.audioPlayer
pause];
//停止
[self.audioPlayer stop];
//音量
self.audioPlayer.volume
= 1;
//播放次数 (-1 为循环)
self.audioPlayer.numberOfLoops
= -1;
//代理协议
AVAudioPlayerDelegate
//播放完成会调用的代理方法
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer
*)player successfully:(BOOL)flag;
//播放解码失败
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer
*)player error:(NSError
*)error;
视频
iOS里面视频播放用的是AVPlayer (包含在AVFoundation框架内) 与AVAudioPlayer优点类似, 但是AVPlayer得功能更加强大, 它可以用来播放音频也可以用来播放视频. 而且在播放音频方面AVPlayer可以直接播放网络音频.
导入框架
#import
<AVFoundation/AVFoundation.h>
NSString
*playString =
@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4";
//
统一资源定位符
c9a5
NSURL
*playerURL = [NSURL
URLWithString:playString];
//初始化
self.playerView
= [[AVPlayerViewController
alloc]init];
//AVPlayerItem ,视频的一些信息.
创建AVPlayer使用的
AVPlayerItem
*item = [[AVPlayerItem
alloc]initWithURL:playerURL];
//通过AVPlayerItem创建AVPlayer
self.player
= [[AVPlayer
alloc]initWithPlayerItem:item];
AVPlayerLayer
*layer = [AVPlayerLayer
playerLayerWithPlayer:self.player];
layer.frame
= CGRectMake(0, 0,
self.view.frame.size.width,
200);
//设置AVPlayer的填充模式
layer.videoGravity
=
AVLayerVideoGravityResizeAspect;
[self.view.layer
addSublayer:layer];
//设置AVPlayerViewController内部的AVPlayer为刚创建的AVPlayer
self.playerView.player
=
self.player;
//关闭AVPlayerViewController内部的约束
self.playerView.view.translatesAutoresizingMaskIntoConstraints
=
YES;
- (void)touchesBegan:(NSSet<UITouch
*> *)touches withEvent:(UIEvent
*)event{
//开始
// [self.player play];
//暂停
//[self.player pause];
//
音量 0 ~ 1
// self.player.volume = 0;
//获取当前播放到第几秒
// CGFloat time = self.player.currentTime.value / self.player.currentTime.timescale;
//获取当前播放的视频或者音频的总时长
// CGFloat duration = self.player.currentItem.duration.value / self.player.currentItem.duration.timescale;
//替换掉当前播放的音频或者视频
// self.player replaceCurrentItemWithPlayerItem:<#(nullable AVPlayerItem *)#>];
//跳转到某个时间的节点
//
构建一个CMTime结构体
//
参数 1 :
时间
//
参数 2 :
时间跨度
// CMTimeMakeWithSeconds(100, self.player.currentTime.timescale);
// self.player seekToTime:<#(CMTime)#> completionHandler:<#^(BOOL finished)completionHandler#>
[self
showViewController:self.playerView
sender:nil];
}
iOS里面共有四种专门实现播放音频的方式:
System Sound Services (系统声音服务)
OpenAL(跨平台的开源的音频处理接口)
Audio Queue Services (播放和录制音频服务)
AVAudioPlayer (高级音频播放器) : 只能播放一个完整音频, 完全下载好的
System Sound Services:是最底层也是最简单的声音播放服务, 通过调用AudioServicesPlaySystemSound 这个函数就可以播放一些简单的音频文件
使用场景 : 适合播放一些很小的提示或者警告音
局限性:
声音长度小于30秒
格式 : IMA4, WAV
不能控制播放的进度
调用方法后立即播放声音
没有循环播放和立体声音播放
OpenAL : 跨平台的开源音频处理借口
适合开发游戏的音频
官方教程:http://www.devdiv.com/thread-19636-1-1.html
http://www.cocoachina.com/bbs/read.php?tid-112679-page-1.html
AVAudioPlayer : 支持广泛的音频格式
AAC
AMR
ALAC
iLBC
IMA4
linearPCM
MP3
优势:
支持更多的格式
可以播放任意长度的音频
支持循环播放
可以同步播放多个音频文件
控制播放进度以及从音频的任意一点开始播放
导入框架 #import
<AVFoundation/AVFoundation.h>
//
获取到的需要播放的model
MusicModel
*model = [[MusicHandle
defaultHandle]
modelWithIndex:self.musicIndex];
//
音频的地址
NSString
*pathString = [[NSBundle
mainBundle]
pathForResource:model.musicName
ofType:model.musicType];
NSData
*data = [NSData
dataWithContentsOfFile:pathString];
//根据音频的data创建一个AVAudioPlayer对象
self.audioPlayer
= [[AVAudioPlayer
alloc]initWithData:data
error:nil];
//播放
[self.audioPlayer
play];
//暂停
[self.audioPlayer
pause];
//停止
[self.audioPlayer stop];
//音量
self.audioPlayer.volume
= 1;
//播放次数 (-1 为循环)
self.audioPlayer.numberOfLoops
= -1;
//代理协议
AVAudioPlayerDelegate
//播放完成会调用的代理方法
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer
*)player successfully:(BOOL)flag;
//播放解码失败
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer
*)player error:(NSError
*)error;
视频
iOS里面视频播放用的是AVPlayer (包含在AVFoundation框架内) 与AVAudioPlayer优点类似, 但是AVPlayer得功能更加强大, 它可以用来播放音频也可以用来播放视频. 而且在播放音频方面AVPlayer可以直接播放网络音频.
导入框架
#import
<AVFoundation/AVFoundation.h>
NSString
*playString =
@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4";
//
统一资源定位符
c9a5
NSURL
*playerURL = [NSURL
URLWithString:playString];
//初始化
self.playerView
= [[AVPlayerViewController
alloc]init];
//AVPlayerItem ,视频的一些信息.
创建AVPlayer使用的
AVPlayerItem
*item = [[AVPlayerItem
alloc]initWithURL:playerURL];
//通过AVPlayerItem创建AVPlayer
self.player
= [[AVPlayer
alloc]initWithPlayerItem:item];
AVPlayerLayer
*layer = [AVPlayerLayer
playerLayerWithPlayer:self.player];
layer.frame
= CGRectMake(0, 0,
self.view.frame.size.width,
200);
//设置AVPlayer的填充模式
layer.videoGravity
=
AVLayerVideoGravityResizeAspect;
[self.view.layer
addSublayer:layer];
//设置AVPlayerViewController内部的AVPlayer为刚创建的AVPlayer
self.playerView.player
=
self.player;
//关闭AVPlayerViewController内部的约束
self.playerView.view.translatesAutoresizingMaskIntoConstraints
=
YES;
- (void)touchesBegan:(NSSet<UITouch
*> *)touches withEvent:(UIEvent
*)event{
//开始
// [self.player play];
//暂停
//[self.player pause];
//
音量 0 ~ 1
// self.player.volume = 0;
//获取当前播放到第几秒
// CGFloat time = self.player.currentTime.value / self.player.currentTime.timescale;
//获取当前播放的视频或者音频的总时长
// CGFloat duration = self.player.currentItem.duration.value / self.player.currentItem.duration.timescale;
//替换掉当前播放的音频或者视频
// self.player replaceCurrentItemWithPlayerItem:<#(nullable AVPlayerItem *)#>];
//跳转到某个时间的节点
//
构建一个CMTime结构体
//
参数 1 :
时间
//
参数 2 :
时间跨度
// CMTimeMakeWithSeconds(100, self.player.currentTime.timescale);
// self.player seekToTime:<#(CMTime)#> completionHandler:<#^(BOOL finished)completionHandler#>
[self
showViewController:self.playerView
sender:nil];
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- Android实现多媒体录音笔
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android开发之多媒体文件获取工具类实例【音频,视频,图片等】
- Android、iOS和Windows Phone中的推送技术详解