iOS学习集--网上转载很屌的项目运用iOS学习总结

iOS基础--多媒体简单总结

2016-04-20  本文已影响990人  云之君兮鹏
加油,夜来幽梦忽还乡

音频


iOS里面共有四种专门实现播放音频的方式:

1、System Sound Services (系统声音服务)
2、OpenAL(跨平台的开源的音频处理接口)
3、Audio Queue Services (播放和录制音频服务)
4、AVAudioPlayer (高级音频播放器) : 只能播放一个完整音频, 完全下载好的

  • 声音长度小于30秒

适合开发游戏的音频
官方教程: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;


视频


导入框架#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

 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];
}`
上一篇下一篇

猜你喜欢

热点阅读