1

尝试初始化音频会话,以便应用程序音频在后台播放,并在屏幕锁定时显示远程控制事件以控制音频,但使用未声明的标识符“audioRouteChangeListenerCallback”时出错

- (void) setupAudioSession {

AVAudioSession *mySession = [AVAudioSession sharedInstance];

// Specify that this object is the delegate of the audio session, so that
//    this object's endInterruption method will be invoked when needed.
[mySession setDelegate: self];

// Assign the Playback category to the audio session.
NSError *audioSessionError = nil;
[mySession setCategory: AVAudioSessionCategoryPlayback
                 error: &audioSessionError];

if (audioSessionError != nil) {

    NSLog (@"Error setting audio session category.");
    return;
}

// Activate the audio session
[mySession setActive: YES
               error: &audioSessionError];

if (audioSessionError != nil) {

    NSLog (@"Error activating audio session during initial setup.");
    return;
}

// Increase the maximum frames per slice allows the mixer unit to accommodate the
//    larger slice size used when the screen is locked.
UInt32 maximumFramesPerSlice = 4096;

 AudioUnitSetProperty (
                               mixerUnit,
                               kAudioUnitProperty_MaximumFramesPerSlice,
                               kAudioUnitScope_Global,
                               0,
                               &maximumFramesPerSlice,
                               sizeof (maximumFramesPerSlice)
                               );

// Register the audio route change listener callback function with the audio session.
AudioSessionAddPropertyListener (
kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback, self );
 }

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
 }

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}

- (BOOL)canBecomeFirstResponder {
return YES;
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
//if it is a remote control event handle it correctly
if (event.type == UIEventTypeRemoteControl) {
    if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {

        [self playAction];

    }  else if (event.subtype == UIEventSubtypeRemoteControlPreviousTrack) {
        [self rewButtonPressed];

    } else if (event.subtype == UIEventSubtypeRemoteControlNextTrack)
        [self ffwButtonPressed:nil];
}}

任何帮助将不胜感激。

谢谢

4

1 回答 1

0

您确实有一个未声明的标识符,称为 audioRouteChangeListenerCallback。这不是 CoreAudio 框架 AFAIK 中定义的符号,但是,Apple 的一些示例代码源定义了它,例如MixerHost,在MixerHostAudio.m.

该源文件中的注释具有指导意义:

// Audio session callback function for responding to audio route changes. If playing back audio and
//   the user unplugs a headset or headphones, or removes the device from a dock connector for hardware
//   that supports audio playback, this callback detects that and stops playback.
//
// Refer to AudioSessionPropertyListener in Audio Session Services Reference.
于 2013-05-14T18:31:50.637 回答