(笔记)音频播放

来源:互联网 发布:linux 重启jenkins 编辑:程序博客网 时间:2024/06/18 12:03
(一)概念
  • 音频数据格式:本质是使用了不同的压缩算法
    • aac / mp3 / caf ...
  • 音频文件格式:音频文件相当于一个容器,可以装音频数据
    • mp3 / wav caff ...


(二)Linux命令
  • 查看MAC支持的所有的 音频格式/文件格式
    • afconvert -hf
  • 查看某个音频文件的详细信息
    • afinfo <音频文件名>
  • 转换音频格式(任意格式)
    • afconvert -f <文件格式> -d<数据格式> <源文件格式> <目标文件名字>
    • 例:afconvert -f adts -d aac xxx.mp3 xxx.aac

(三)iOS支持的音频数据格式
  • 推荐使用音频数据格式:aac/mp3/alac/cdf
  • 原因:硬件/软件解码

(四)本地音频播放
1.本地音效
  • 又叫短音频,时长<=30s的音频
  • 使用底层类播放SystemService(速度快)
  • 一次播放,不能暂停

1.1 引入头文件 <AVFoundation/AVFoundation.h>

1.2 步骤
  • ①创建音效文件的本地URL路径
  • ②创建systemID
    • 创建systemID属性
    • AudioServicesCreateSystemSoundID((需要桥接转换url)(url),&_systemID);
  • ③播放:
    • AudioServicesPlaySystemSound(_systemID);
//记录systemSoundID@property (nonatomic, assign) SystemSoundID systemID;//播放音效(<=30s)- (IBAction)playShortAudio:(id)sender {    //播放本地音效文件(创建systemID+播放)    NSString *shortAudioPath = [[NSBundle mainBundle] pathForResource:@"audio.wav" ofType:nil];    NSURL *url = [NSURL fileURLWithPath:shortAudioPath];    AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &_systemID);    //播放    AudioServicesPlaySystemSound(_systemID);   }

2.本地音频
  • 使用Audio Toolbox Framework(功能多)

2.1
//记录播放器对象@property (nonatomic, strong) AVAudioPlayer *audioPlayer;//加载音频播放对象- (AVAudioPlayer *)audioPlayer {    if (!_audioPlayer) {        NSURL *audioFilePath = [[NSBundle mainBundle] URLForResource:@"AllOfMe.mp3" withExtension:nil];        _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFilePath error:nil];    }    return _audioPlayer;}



2.2 播放、暂停、停止
  • [self.audioPlayerprepareToPlay]:音频播放对象是否已经从文件读入到内存中。返回BOOL值
//播放本地音频文件- (IBAction)playLocalAudioFile:(id)sender {    if ([self.audioPlayer prepareToPlay]) {        //可以将音频文件文件的数据读到内存(快)        [self.audioPlayer play];    }}//暂停- (IBAction)pauseAudioFile:(id)sender {    if (self.audioPlayer.playing) {        //正在播放        [self.audioPlayer pause];    }}//停止(下次播放,从头开始)- (IBAction)stopAudioFile:(id)sender {    //设置当前播放的时间偏移量offset为0    [self.audioPlayer stop];    self.audioPlayer.currentTime = 0;}




(五)播放在线音频/视频
1.声明属性
//segment选项卡,区分播放的是视频还是音频@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;//播放对象,可以播放音频或视频@property (nonatomic, strong) AVPlayer *player;//视频需要,声明层属性。@property (nonatomic, strong) AVPlayerLayer *videoLayer;


2.创建 播放对象
- (IBAction)play:(id)sender {    //音频地址    NSString *audioPath = @"远程音频链接";    //视频地址    NSString *videoPath = @“远程视频链接";       if (self.segmentControl.selectedSegmentIndex == 0) {        //视频播放        //1.创建播放对象,并赋值        self.player = [AVPlayer playerWithURL:[NSURL URLWithString:videoPath]];        //2.创建layer对象,指定layer的frame        self.videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];        self.videoLayer.frame = CGRectMake(0, 150, 300, 250);        //3.把layer添加到view.layer        [self.view.layer addSublayer:self.videoLayer];    } else {        //音频        self.player = [AVPlayer playerWithURL:[NSURL URLWithString:audioPath]];    }    //执行播放动作(音频或者视频)    [self.player play];}


0 0
原创粉丝点击