Frage

Im Moment arbeite ich an einem Projekt, die Musik aus dem iPhone Musikbibliothek innerhalb der App innerhalb beinhaltet spielen. Ich MPMediaPickerController mit dem Benutzer zu ermöglichen, ihre Musik zu wählen und spiele den iPod Musik-Player im iPhone.

Aber ich lief in Problem, wenn der Benutzer seine Hörer ein und entfernt sie. Die Musik wird plötzlich nicht mehr ohne Grund zu spielen. Nach einigen Tests, fand ich heraus, dass der iPod-Player spielen wird angehalten, wenn der Benutzer seine Hörer aus dem Gerät ziehen. So ist es eine Möglichkeit, programmatisch festzustellen, ob der Hörer Unplug war so, dass ich die Musik spielt wieder aufnehmen kann? Oder gibt es eine Möglichkeit, iPod-Player zu verhindern, dass eine Pause, wenn der Benutzer seine Hörer ziehen?

War es hilfreich?

Lösung

Sie sollten registrieren für AudioRoute Benachrichtigung geändert und implementieren, wie Sie die Router Änderungen

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

und innerhalb der Callback, können Sie den Grund für die Routenänderung

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

Andere Tipps

Wenn Sie wollen einfach nur prüfen, ob ein Kopfhörer zu einem bestimmten Zeitpunkt eingesteckt, ohne zu Routenänderungen zu hören, können Sie einfach wie folgt vor:

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

Ich sehe Sie die MPMediaPlayer Framework verwenden jedoch das Mikrofon Handhabung wird mit dem AVAudioPlayer Rahmen getan, die Sie für Ihr Projekt benötigen hinzuzufügen.

Apple-Website hat Code aus dem AVAudioPlayer Framework, das ich von einem Anwender Unterbrechungen zu behandeln verwende Einstecken oder Entfernen der Apple-Mikrofon-Kopfhörer.

Überprüfen Sie heraus Apples Führer iPhone Dev Center Audio Session Programmierung.

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

Mein Code ist ein wenig anders, und einige davon können Ihnen helfen:

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

Hey Jungs nur AddMusic Beispielanwendung überprüfen. Löst alle Ihre iPod Fragen

Zuerst registrieren iPod-Player für die Benachrichtigung mit folgendem Code

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

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

    [musicPlayer beginGeneratingPlaybackNotifications];

Und implementieren Sie den folgenden Code in der Mitteilung

- (void) handle_PlaybackStateChanged: (id) notification 
{

    MPMusicPlaybackState playbackState = [musicPlayer playbackState];

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

    } 
    else if (playbackState == MPMusicPlaybackStateStopped) 
    {
        [musicPlayer stop];
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top