9

ロケーションコントローラーからのイベントで電話でサウンドファイルを再生する小さなアプリに取り組んでいます。問題は、アプリがバックグラウンド モードの場合、AVAudioPlayer がオーディオ ファイルを開始せず、コードがスケジュールされ、NSLog 出力が取得されることです。もう1つのことは、シミュレーターでは機能しますが、電話では機能しないことです.

なぜですか?

これはファイルを再生するための私のコードです:

- (void)tryPlayTaleSound:(NSString*)fileName {

    NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
    NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
    NSError *error;

    backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];

    // Check to see if iPod music is already playing
    //UInt32 propertySize = sizeof(otherMusicIsPlaying);
    //AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &otherMusicIsPlaying);

    // Play the music if no other music is playing and we aren't playing already
    //if (otherMusicIsPlaying != 1 && !backgroundMusicPlaying) {
    //[backgroundMusicPlayer prepareToPlay];
    [backgroundMusicPlayer play];
    backgroundMusicPlaying = YES;
    //}
    NSLog(@"%s - ", __FUNCTION__);
}

ログでこれを取得しています。

Apr 26 13:57:48 iPhone CommCenter[58] <Notice>: Client [com.apple.persistentconnection[dataaccessd,85]] is telling PDP context 0 to go active.
Apr 26 13:57:50 iPhone Herning100[1467] <Warning>: Playing audiofile - filename is Fil03
Apr 26 13:57:50 iPhone mediaserverd[39] <Warning>: 13:57:50.653 <AudioControl> WARNING translating CMSession error: -12985 
Apr 26 13:57:50 iPhone mediaserverd[39] <Warning>: 13:57:50.656 <AudioControl> WARNING translating CMSession error: -12985 
Apr 26 13:57:50 IPhone mediaserverd[39] <Error>: 13:57:50.734 <AudioControl> AudioQueue: Error -12985 from AudioSessionSetClientPlayState(1467)
Apr 26 13:57:50 iPhone Herning100[1467] <Warning>: -[AppDelegate tryPlayTaleSound:] - 
Apr 26 13:57:50 iPhone com.apple.debugserver-199[1465] <Warning>: 9 +28.832083 sec [05b9/0303]: error: ::mach_vm_region_recurse ( task = 0x1603, address => 0xfffffffe, size => 0, nesting_depth => 1024, info => 0x2fd7fe48, infoCnt => 12) addr = 0xfffffffe  err = (os/kern) invalid address (0x00000001)
Apr 26 13:57:51 iPhone com.apple.debugserver-199[1465] <Warning>: 10 +0.187961 sec [05b9/0303]: error: ::mach_vm_region_recurse ( task = 0x1603, address => 0xfffffffe, size => 0, nesting_depth => 1024, info => 0x2fd7fe48, infoCnt => 12) addr = 0xfffffffe  err = (os/kern) invalid address (0x00000001)

AVAudioPlayer または AVAudioSession を使用する場合に違いはありますか?

誰かが助けてくれることを願っています。

/ラッセ

4

3 に答える 3

12

audioバックグラウンド モードに加えてInfo.plist、共有オーディオ セッションをアプリに適したモードに設定し、バックグラウンドでサウンドを再生する直前にアクティブ化する必要があります。

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
NSLog(@"Activating audio session");
if (![audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error]) {
    NSLog(@"Unable to set audio session category: %@", error);
}
BOOL result = [audioSession setActive:YES error:&error];
if (!result) {
    NSLog(@"Error activating audio session: %@", error);
}
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

サウンドの再生が終了したら、オーディオ セッションを無効にすることを忘れないでください。

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
NSLog(@"Deactivating audio session");
BOOL result = [audioSession setActive:NO error:&error];
if (!result) {
    NSLog(@"Error deactivating audio session: %@", error);
}
于 2014-04-18T16:31:16.827 に答える
1

バックグラウンドでサウンドの再生を開始する前に、バックグラウンド タスクをセットアップします。バックグラウンドの残り時間が 10 秒未満になると、iOS はバックグラウンドでサウンドを開始することを拒否するようです。

于 2015-05-15T06:16:45.220 に答える
0

iOS では、一部のアプリケーションのみがバックグラウンドで実行できます。それらの 1 つはオーディオ プレーヤー アプリケーションですが、Info.plist で「オーディオ」(アプリはオーディオを再生できます) で「必要なバックグラウンド モード」を設定する必要があります。

はい、バックグラウンドで場所を取得できる場合は、このキーを Plist に設定している必要があります。plist にオーディオ許可も追加します。

于 2013-04-26T09:08:09.857 に答える