iOS - AVFoundation - 音频播放

AVFoundation框架,看到这个名字的时候小激动一下,仔细想一下应该是audio,video的意思吧,后来去翻苹果的文档,原来是** audiovisual(视听)**的意思。单词不一样,不过意思差不多吧,主要是用来处理音频视频的.

下面的是从官方文档中拿出来的


iOS - AVFoundation - 音频播放_第1张图片
frameworksBlockDiagram_2x.png

注意:在使用时,应尽量使用更高级别的控件(这些控件经过进一步的封装,使用起来可能会更容易一些)

  1. 如果只是想播放视频,应该使用AVKit框架。
  2. 在IOS中,如果只是想录制视频,可以使用UIKit框架(UIImagePickerController).

iOS中的音频

  • 音效(时间一般比较短)
  • 音乐
  • 录音

1.播放音效

主要实现下面的几个方法

  • AudioServicesCreateSystemSoundID(CFURLRef inFileURL,SystemSoundID*outSystemSoundID)
  • AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)
  • AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID);
- (void)playSoundWithFileName:(NSString *)fileName {
  //定义一个soundID用来接收系统生成的声音ID
  SystemSoundID soundID=0;
  // 获取资源文件
  NSURL *url = [[NSBundle mainBundle] URLForResource:fileName   withExtension:nil];
  if (url == nil) return;
  // 获取音频的soundID
  AudioServicesCreateSystemSoundID(CFBridgingRetain(url), &soundID);
  AudioServicesPlaySystemSound(soundID);
}

2. 播放音乐

To play sound files, you can use AVAudioPlayer.
播放音乐文件,使用AVAudioPlayer。进到头文件看一下,内容也不是很多,大概看一些应该就可以上手了。

主要的几个方法:

- (nullable instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;
- (nullable instancetype)initWithData:(NSData *)data error:(NSError **)outError;

/* transport control */
/* methods that return BOOL return YES on success and NO on failure. */
- (BOOL)prepareToPlay;  /* get ready to play the sound. happens automatically on play. */
- (BOOL)play;           /* sound is played asynchronously. */
- (void)pause;          /* pauses playback, but remains ready to play. */
- (void)stop;           /* stops playback. no longer ready to play. */

示例代码:

#import "ViewController.h"
#import 
@interface ViewController ()
@property (nonatomic,strong) AVAudioPlayer *player;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"童话.mp3" withExtension:nil];
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
    // 准备播放
    [self.player prepareToPlay];
}

- (IBAction)play {
    [self.player play];
}

- (IBAction)pause {
    [self.player pause];
}

- (IBAction)stop {
    [self.player pause];
}
@end

3. 录音

To record audio, you can use AVAudioRecorder.
记录音频信息,需要使用AVAudioRecorder。这个类和AVAudioPlayer非常类似,不过录音的时候需要设置参数。

- (nullable instancetype)initWithURL:(NSURL *)url settings:(NSDictionary *)settings error:(NSError **)outError;这个settings的参数可以参考AV Foundation Audio Settings Constants.

//    setting[AVFormatIDKey] = @(kAudioFormatAppleIMA4);
//    // 音频采样率-->采样频率是指计算机每秒钟采集多少个声音样本,是描述声音文件的音质、音调,衡量声卡、声音文件的质量标准
//    setting[AVSampleRateKey] = @(8000.0);
//    // 音频通道数-->iPhone只有一个 电脑可能多个
//    setting[AVNumberOfChannelsKey] = @(1);
//    // 线性音频的位深度(8/16/24/32 bit) 数值越高 失真越小
//    setting[AVLinearPCMBitDepthKey] = @(8);

直接传一个空字典也是可以的,使用系统默认的参数

参考:
苹果官方文档
iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
iOS 播放音频的几种方法
iOS音频播放(二):AudioSession
iOS音频播放(二):AudioSession

你可能感兴趣的:(iOS - AVFoundation - 音频播放)