簡體   English   中英

AudioQueueStart返回561015905(AVAudioSessionErrorCodeCannotStartPlaying)

[英]AudioQueueStart returns 561015905 (AVAudioSessionErrorCodeCannotStartPlaying)

我的應用使用音頻隊列服務播放聲音。 在應用程序啟動時,我將音頻會話類別設置為solo ambient:

`[[AVAudioSession sharedInstance] setCategory:AVAudioSEssionCategorySoloAmbient error:&error];`

當我的app委托獲得applicationWillResignActive通知時,我調用AudioQueuePause(queue); 所有播放的聲音。 當app委托獲得applicationDidBecomeActive ,我會打電話

OSStatus status = AudioQueueStart(queue, 0);

有時(並且很難重現) status等於561015905.此值不屬於音頻隊列結果代碼 這是AVAudioSessionErrorCodeCannotStartPlaying ,文檔說

“該應用程序不允許開始錄制和/或播放,通常是因為其Info.plist中缺少音頻密鑰。如果應用程序具有此密鑰但使用無法記錄的類別,也可能發生這種情況。
和/或在后台播放(AVAudioSessionCategoryAmbient,AVAudioSessionCategorySoloAmbient等)。“

我絕對不想在后台播放聲音(這就是為什么我在app重新激活時暫停音頻隊列的原因)。 那么,我做錯了什么?

我有一個類似的問題,但我以不同的方式做了一點......有音樂應用程序需要背景音樂,但在某些情況下,她必須在背景模式下嚎叫。 對我來說,我使用這段代碼:

#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVAudioPlayer.h>
#import <AVFoundation/AVAudioSession.h>

@interface ViewController ()
{
  AVAudioPlayer *audioPlayer;
}

@property (assign) SystemSoundID pewPewSound;

@end

@implementation ViewController

- (void)viewDidLoad 
{
  [super viewDidLoad];

  NSURL * pewPewURL = [[NSBundle mainBundle]URLForResource:@"Calypso" withExtension:@"caf"];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:pewPewURL error:nil];

     if (audioPlayer)
     {
          [audioPlayer setNumberOfLoops:100];
          [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
          [[AVAudioSession sharedInstance] setActive: YES error: nil];
          [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
          [audioPlayer prepareToPlay];
          [audioPlayer play];
     }

}

@end

如果你沒有觸摸任何Xcode設置,這段代碼只能在前台播放循環旋律,如果你去后台,播放器就會停止。

如果你這樣做:

在此輸入圖像描述

旋律將繼續在后台播放,在我的情況下,我可以停止並在我想要的時候播放背景和前景中的旋律。 然后我添加系統聲音然后停止播放器,系統聲音停止然后U繼續播放音樂。

-(void)stopPlaying
{
    [audioPlayer pause];

    NSURL * pewPewURL;
    pewPewURL = [[NSBundle mainBundle]URLForResource:@"Calypso" withExtension:@"caf"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &_pewPewSound);
    AudioServicesPlaySystemSound(self.pewPewSound);
}

-(void)continueToPlay
{
    if ([audioPlayer prepareToPlay])
    {
        [audioPlayer play];
        AudioServicesRemoveSystemSoundCompletion(self.pewPewSound);
        AudioServicesDisposeSystemSoundID(self.pewPewSound);
    }
}

我希望它有助於解決你的問題,如果有問題,我會回答所有問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM