多媒体 (系统的,用处不大)


音频

iOS里面共有四种专门实现播放音频的方式:
  1. System Sound Services (系统声音服务)
  2. OpenAL(跨平台的开源的音频处理接口)
  3. Audio Queue Services (播放和录制音频服务)
  4. AVAudioPlayer (高级音频播放器)  : 只能播放一个完整音频, 完全下载好的

  • System Sound Services:是最底层也是最简单的声音播放服务, 通过调用AudioServicesPlaySystemSound 这个函数就可以播放一些简单的音频文件
  • 使用场景 : 适合播放一些很小的提示或者警告音
  • 局限性:
  1. 声音长度小于30秒
  2. 格式 : IMA4, WAV
  3. 不能控制播放的进度
  4. 调用方法后立即播放声音
  5. 没有循环播放和立体声音播放

OpenAL : 跨平台的开源音频处理借口
适合开发游戏的音频
AVAudioPlayer : 支持广泛的音频格式
  1. AAC
  2. AMR
  3. ALAC
  4. iLBC
  5. IMA4
  6. linearPCM
  7. MP3
优势:
  1. 支持更多的格式
  2. 可以播放任意长度的音频
  3. 支持循环播放
  4. 可以同步播放多个音频文件
  5. 控制播放进度以及从音频的任意一点开始播放


导入框架  #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" ;
   
// 统一资源定位符
   
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 ];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值