[iPhone app] AVAudioPlayer で音源多重再生

1月 3, 2009   //   by rssfeeder   //   ETH  //  [iPhone app] AVAudioPlayer で音源多重再生 はコメントを受け付けていません

あけましておめでとうございます。今年もどうぞよろしくお願いします。
みんなが、聞ける音楽、使えるアプリ、楽しめる空間 を創る一年にしたいと思います。

で、相変わらずiPhoneアプリを作成しています。

avAudioPlayer

iPhone OS 2.2からAVFoundation.frameworkが使用可能になり、mp3などの音楽コンテンツの再生が簡単に出来るようになったみたいです。
で、多重音楽再生のために使用したのですが、いろいろトラブったのでメモしておきます。

最初、リファレンスを見ながら、
#import<AVFoundation/AVFoundation.h>
して

NSString *path = [[NSBundle mainBundle] pathForResource:@”sound” ofType:@”mp3″];
NSURL *url = [NSURL fileURLWithPath:path];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

;

としたがビルド失敗。

“.objc_class_name_AVAudioPlayer”, referenced from: literal-pointer@__OBJC@__cls_refs@AVAudioPlayer in AVAudioTest2AppDelegate.o
ld: symbol(s) not found collect2: ld returned 1 exit status

このエラーをコピペしてググったら、原因がわかった。

XcodeのFrameworkにAVFoundation.frameworkを追加していなかったため。
しかし、追加しようにも、System/Library/Frameworks下に見当たらない。SpotLightで”AVFoundation.framework”を探したら、iPhoneシミュレータのフォルダにあったため、それをコピペ。
しかしまだ出来ない。下のエラーが出る。

Error loading
/Library/QuickTime/LiveType.component/Contents/MacOS/LiveType:
dlopen(/Library/QuickTime/LiveType.component/Contents/MacOS/LiveType,
262): Symbol not found:

またググったら、
Numata Designed Factoryさんのページに答えが。
http://numata.designed.jp/ja/2008/12/iphone-1.html
どうやら開発環境の不具合(仕様?)っぽい。
で、記載されている通り、/Library/QuickTimeから

  • LiveType.component

を削除したら再生成功。エラー出力でググるのは強力だと、実感した。

で多重再生しようと
AVAudioPlayerのインスタンスを2つつくって両方再生させようとしたが、1曲目再生中に2曲目を再生させると、そこで1曲目の再生がストップしてしまい、交互にしか再生出来ない。。リファレンスには、
Using an audio player you can:
* Play sounds of any duration
* Play sounds from files or memory buffers
* Loop sounds
* Play multiple sounds simultaneously
* Control relative playback level for each sound you are playing
* Seek to a particular point in a sound file, which supports such application features as fast forward and rewind
* Obtain data that you can use for playback level metering

ってちゃんと書いてあるのにできない。
調べたら、どうやら多重再生したい場合、フォーマットは .cafファイルを使わなければいけないらしい。

cafファイルはmp3ファイルから、ターミナル上で以下のコマンドにより作成出来る。

/usr/bin/afconvert -f caff -d ima4 sound.mp3 sound.caf

作成したcafファイルを使用したところ、ちゃんと多重再生できました。

Comments are closed.

Archives