gpt4 book ai didi

ios - AudioQueueStart 返回 561015905 (AVAudioSessionErrorCodeCannotStartPlaying)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:59 29 4
gpt4 key购买 nike

我的应用程序使用音频队列服务来播放声音。在应用程序启动时,我将 Audio Session 类别设置为独奏环境:

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

当我的应用委托(delegate)收到 applicationWillResignActive 通知时,我调用 AudioQueuePause(queue); 来播放所有声音。当应用委托(delegate)获得 applicationDidBecomeActive 时,我调用

OSStatus status = AudioQueueStart(queue, 0);

有时(而且很难重现)status 等于 561015905。这个值不属于 Audio Queue Result Codes。 .它是 AVAudioSessionErrorCodeCannotStartPlaying,文档说

"The app is not allowed to start recording and/or playing, usually because of a lack of audio key in its Info.plist. This could also happen if the app has this key but uses a category that can't record
and/or play in the background (AVAudioSessionCategoryAmbient, AVAudioSessionCategorySoloAmbient, etc.)."

我绝对不想在后台播放声音(这就是为什么我在应用退出事件时暂停音频队列)。那么,我做错了什么?

最佳答案

我有一个类似的问题,但我用不同的方式做了一点......有一个音乐应用程序,其中背景音乐是​​必要的,但在某些情况下,她必须嚎叫才能在背景模式下播放。对我来说,我使用这段代码:

#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 设置,此代码仅在前台播放循环旋律,如果您转到后台,播放器将停止。

如果你这样做:

enter image description here

旋律将继续在背景中播放,在我的情况下,我可以在需要时停止并在背景和前景中播放旋律。然后我添加系统声音来调用然后你停止播放器,系统声音停止然后你继续播放音乐。

-(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);
}
}

希望对解决您的问题有所帮助,有问题,我会一一解答。

关于ios - AudioQueueStart 返回 561015905 (AVAudioSessionErrorCodeCannotStartPlaying),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34789414/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com