我目前正在对包括播放音乐,从iPhone音乐库中的应用程序内内的一个项目。我使用MPMediaPickerController允许用户选择自己的音乐,并使用iPhone中的iPod音乐播放器播放。

然而,我跑成问题,当用户将他的耳机和删除它。音乐就会突然停止播放没有任何理由。经过一些测试,我发现了iPod播放器将暂停播放,当用户从设备中拔出自己的耳机。那么,有没有办法以编程方式检测是否听筒已拔掉,这样我可以继续播放音乐?或者是有什么办法可以防止iPod播放器的暂停,当用户拔下耳机了?

有帮助吗?

解决方案

您应该注册AudioRoute更改通知和实施要如何处理狂胜变化

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

和回调中,你可以得到路线变更的原因

  CFDictionaryRef   routeChangeDictionary = inPropertyValue;

  CFNumberRef routeChangeReasonRef =
  CFDictionaryGetValue (routeChangeDictionary,
            CFSTR (kAudioSession_AudioRouteChangeKey_Reason));

  SInt32 routeChangeReason;

      CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);

  if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) 
  {
       // Headset is unplugged..

  }
  if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable)
  {
       // Headset is plugged in..                   
  }

其他提示

如果你只是要检查耳机是否在任何给定的时间插上,不听路由变化,你可以简单地做到以下几点:

OSStatus error = AudioSessionInitialize(NULL, NULL, NULL, NULL);
if (error) 
    NSLog("Error %d while initializing session", error);

UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;

error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
                                 &routeSize,
                                 &route);

if (error) 
    NSLog("Error %d while retrieving audio property", error);
else if (route == NULL) {
    NSLog(@"Silent switch is currently on");
} else  if([route isEqual:@"Headset"]) {
    NSLog(@"Using headphones");
} else {
    NSLog(@"Using %@", route);
}

干杯, 拉斐尔Colasante

我看到你正在使用的MPMediaPlayer框架然而麦克风处理使用AVAudioPlayer框架,您将需要添加到您的项目中完成的。

苹果的网站具有代码从AVAudioPlayer框架,我使用于来自用户的中断处理在插入或移除所述苹果麦克风的耳机。

查看苹果的 iPhone开发中心音频会话编程指南

- (void) beginInterruption {
    if (playing) {
        playing = NO;
        interruptedWhilePlaying = YES;
        [self updateUserInterface];
    }
}

NSError *activationError = nil;
- (void) endInterruption {
    if (interruptedWhilePlaying) {
        [[AVAudioSession sharedInstance] setActive: YES error: &activationError];
        [player play];
        playing = YES;
        interruptedWhilePlaying = NO;
        [self updateUserInterface];
    }
}

我的代码是有点不同的,一些本可以帮助你:

    void interruptionListenerCallback (
                                   void *inUserData,
                                   UInt32   interruptionState
) {
    // This callback, being outside the implementation block, needs a reference
    //  to the AudioViewController object
    RecordingListViewController *controller = (RecordingListViewController *) inUserData;

    if (interruptionState == kAudioSessionBeginInterruption) {

        //NSLog (@"Interrupted. Stopping playback or recording.");

        if (controller.audioRecorder) {
            // if currently recording, stop
            [controller recordOrStop: (id) controller];
        } else if (controller.audioPlayer) {
            // if currently playing, pause
            [controller pausePlayback];
            controller.interruptedOnPlayback = YES;
        }

    } else if ((interruptionState == kAudioSessionEndInterruption) && controller.interruptedOnPlayback) {
        // if the interruption was removed, and the app had been playing, resume playback
        [controller resumePlayback];
        controller.interruptedOnPlayback = NO;
    }
}

void recordingListViewMicrophoneListener (
                         void                      *inUserData,
                         AudioSessionPropertyID    inPropertyID,
                         UInt32                    inPropertyValueSize,
                         const void                *isMicConnected
                         ) {

    // ensure that this callback was invoked for a change to microphone connection
    if (inPropertyID != kAudioSessionProperty_AudioInputAvailable) {
        return;
    }

    RecordingListViewController *controller = (RecordingListViewController *) inUserData;

    // kAudioSessionProperty_AudioInputAvailable is a UInt32 (see Apple Audio Session Services Reference documentation)
    // to read isMicConnected, convert the const void pointer to a UInt32 pointer
    // then dereference the memory address contained in that pointer
    UInt32 connected = * (UInt32 *) isMicConnected;

    if (connected){
        [controller setMicrophoneConnected : YES];
    }
    else{
        [controller setMicrophoneConnected: NO];    
    }

    // check to see if microphone disconnected while recording
    // cancel the recording if it was
    if(controller.isRecording && !connected){
        [controller cancelDueToMicrophoneError];
    }
}

嗨只是检查AddMusic示例应用程序。将解决所有的iPod相关的问题

第一寄存器iPod播放器的通知用下面的代码

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

    [notificationCenter
     addObserver: self
     selector:    @selector (handle_PlaybackStateChanged:)
     name:        MPMusicPlayerControllerPlaybackStateDidChangeNotification
     object:      musicPlayer];

    [musicPlayer beginGeneratingPlaybackNotifications];

和实施在通知中的下面的代码

- (void) handle_PlaybackStateChanged: (id) notification 
{

    MPMusicPlaybackState playbackState = [musicPlayer playbackState];

    if (playbackState == MPMusicPlaybackStatePaused) 
    {
           [self playiPodMusic];
    } 
    else if (playbackState == MPMusicPlaybackStatePlaying) 
    {

    } 
    else if (playbackState == MPMusicPlaybackStateStopped) 
    {
        [musicPlayer stop];
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top