Pergunta

Atualmente estou trabalhando em um projeto que envolve a reprodução de música da biblioteca de músicas do iPhone dentro de dentro do aplicativo. Estou usando MPMediaPickerController para permitir que o usuário selecione suas músicas e reproduzi-lo usando o leitor de música iPod dentro do iPhone.

No entanto, eu corri para problema quando o usuário inserir seu fone de ouvido e remove-lo. A música, de repente, parar de jogar por nenhuma razão. Depois de alguns testes, eu descobri que o iPod jogador irá pausar a reprodução quando o usuário desligue seu fone de ouvido do dispositivo. Então, há alguma maneira de programaticamente detectar se o fone de ouvido tem sido desligue para que eu possa continuar a reprodução da música? Ou há alguma maneira de evitar que o iPod jogador de pausa quando o usuário desligue seu fone de ouvido?

Foi útil?

Solução

Você deve se registrar para AudioRoute mudou notificação e implementar como você deseja lidar com as mudanças Rout

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

e dentro do callback, você pode obter a razão para a mudança de rota

  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..                   
  }

Outras dicas

Se você quiser apenas para verificar se os auscultadores estão ligados a qualquer momento, sem ouvir mudanças de rota, você pode simplesmente fazer o seguinte:

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

Cheers, Raffaello Colasante

Eu vejo que você está usando o Framework MPMediaPlayer no entanto, o manuseio do microfone é feito usando o framework AVAudioPlayer, o que você precisa adicionar ao seu projeto.

site da Apple tem o código do framework AVAudioPlayer que eu uso para interrupções do punho de um usuário ligar ou a remover os fones de ouvido microfone Apple.

Confira da Apple iPhone Dev Center sessão de áudio Guia de Programação .

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

Meu código é um pouco diferente e algumas delas podem ajudá-lo:

    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];
    }
}

Ei basta verificar aplicativo de exemplo AddMusic. Vai resolver todos os seus problemas relacionados iPod

Primeiro, registre iPod player para notificação com seguinte código

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

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

    [musicPlayer beginGeneratingPlaybackNotifications];

E implementar o seguinte código na notificação

- (void) handle_PlaybackStateChanged: (id) notification 
{

    MPMusicPlaybackState playbackState = [musicPlayer playbackState];

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

    } 
    else if (playbackState == MPMusicPlaybackStateStopped) 
    {
        [musicPlayer stop];
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top