0

我已经尝试了一切:

-setup info.plist 所需的背景模式:应用播放音频;

- 设置播放器:

 [[AVAudioSession sharedInstance] setDelegate: self];

// allow app continue to play when the screen is locked
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                       error:nil];

UInt32 doSetProperty = 0;
AudioSessionSetProperty ( kAudioSessionProperty_OverrideCategoryMixWithOthers,
                         sizeof (doSetProperty),
                         &doSetProperty
                         );

// Registers the audio route change listener callback function
AudioSessionAddPropertyListener (
                                 kAudioSessionProperty_AudioRouteChange,
                                 audioRouteChangeListenerCallback,
                                 self
                                 );

// Activates the audio session.

NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];

[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume: 1.0];
[appSoundPlayer setDelegate: self];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

我忘记了什么?有什么不对?

4

1 回答 1

1

您需要将音频类别设置为播放:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];

如果您想允许将音频与其他应用程序混合,您可以添加此选项,只需确保在将音频会话设置为活动之前执行此操作。

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];

现在您可以将音频会话设置为活动:

[[AVAudioSession sharedInstance] setActive:YES error:&error];
于 2013-08-30T13:46:46.873 回答