iOSでの音声の再生と録音についてまとめる
iOSでの音声の再生と録音についてまとめる。
wavなどの音声ファイルを再生するだけなら AVFoundation#AVAudioPlayer で簡単に再生できる。
録音する場合には、 AudioUnit#AudioUnit や AudioToolbox#AudioQueue を使う必要がある。
いづれの場合も 再生方法は AudioToolbox#AudioSession で設定する必要がある。
AVAudioPlayer 以外はC言語の関数なのでC言語がわからないと取り組むのは難しい。
また、コンピュータでサウンドをどう扱うのか(PCM、サンプリング周波数、量子化ビット数)の知識を持っている必要がある。
AudioUnit#AudioUnit
・Cフレームワーク
・サンプルレート、サウンドフォーマットを意識する必要がある低レベル関数群
// AudioUnitを取得して設定する
AudioComponentFindNext();
AudioComponentInstanceNew();
AudioUnitSetProperty();
AudioUnitInitialize(InputUnit);
// 開始
AudioUnitRender();
AudioToolbox#AudioQueue
・Cフレームワーク
// オーディオキュー作成
AudioQueueNewOutput();
// バッファ確保
for (int cnt = 0; i < bufferCount; cnt++) {
AudioQueueAllocateBuffer();
HandleOutputBuffer();
}
// パラメータ設定
AudioQueueSetParameter();
// 再生開始
AudioQueueStart();
AVFoundation#AVAudioPlayer
・Objective-Cクラスフレームワーク
・wav caf などのオーディオファイルをObjectiveCクラスを使って再生する
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sample" ofType:@"caf"]] error:&playerError];
[player prepareToPlay];
AudioToolbox#AudioSession
・Cフレームワーク
・オーディオ再生方法を指定する
OSStatus error = AudioSessionInitialize(NULL, NULL, interruptListener, self);
// オーディオ再生方法を指定
UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
OSStatus error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), & sessionCategory);
// オーディオルート変更デリゲートを指定
OSStatus error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, routeChangeListener, self);
// オーディオ出力先スピーカー設定
UInt32 audioSpeaker = kAudioSessionOverrideAudioRoute_Speaker;
error = AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof (audioSpeaker), & audioSpeaker);
AudioSessionSetActive(true);
2023年12月 | ||||||
日 | 月 | 火 | 水 | 木 | 金 | 土 |
  |   |   |   |   | 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |   |   |   |   |   |   |
iOS
web
アプリの著作権
ブロックチェーン/暗号技術
新しい社会
禅・大乗仏教
日本のなりたち