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
?スA?スv?ス?ス?スフ抵ソス?ス??
?スu?ス?ス?スb?スN?ス`?スF?ス[?ス?ス?ス^?ステ搾ソス?スZ?スp
?スV?ス?ス?ス?ス?スミ会ソス
?スT?スE?ス?ス謨ァ?ス?ス
?ス?ス?ス{?スフなりた?ス?ス