アプリのオーディオをバックグラウンドで実行したいのですが、このチュートリアルに従いましたが、うまくいきません。ホームボタンを押してもアプリのオーディオが停止し、「applicationDidBecomeActive」または「applicationDidEnterBackground」を呼び出さないことに気付きました(「アプリケーションはバックグラウンドで実行されません」という設定を無効にしても問題は続きました)。私は先週、これに対処していました。
これまでのところ、次の手順を実行しました。
- AVFoundation フレームワークを追加し、宣言
#import <AVFoundation/AVFoundation.h>
- AVAudioSession をオーディオに設定する
NSString *audioName = [NSString stringWithFormat:@"audio%d", (nimages)];
NSString *soundPath =[[NSBundle mainBundle]pathForResource:audioName ofType:@"mp3"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
NSError *error = nil;
AVAudioPlayer *audio = nil;
audio = [[AVAudioPlayer alloc]initWithContentsOfURL:soundURL error:&error];
audio.numberOfLoops = -1;
audio.volume = 0.9;
if (error) {
NSLog(@"%@", [error localizedDescription]);
NSLog(@"Unable to load file");
}else {
//Make sure the system follows our playback status
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
//Load the audio into memory
[audio prepareToPlay];
}
-plistに行を追加
更新しました:
私のアプリは、ユーザーがバックグラウンドに入ってもアプリの特定のサウンドトラックを再生できるオーディオ アプリです。出来ますか?