Vedo che si sta utilizzando MPMediaPlayer Framework, tuttavia la gestione del microfono viene eseguita utilizzando il framework AVAudioPlayer, che sarà necessario aggiungere al progetto.
Il sito Web di Apple include il codice del framework AVAudioPlayer che utilizzo per gestire le interruzioni da un utente che collega o rimuove le cuffie del microfono Apple.
Controllare Apple iPhone Dev Center Audio Session Programming Guide.
- (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];
}
}
Il mio codice è un po 'diverso e un po' di questo può aiutare:
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];
}
}
fonte
2009-12-02 10:56:23
ehm sto avendo 2 errore durante la compilazione 1) wat's inPropertyValue? Non è stato riconosciuto nel parametro metodo 2) CFDictionaryGetValue restituisce un puntatore vuoto che è incomparabile con CFNumberRef. Devo fare un casting prima di restituire il valore? –
hmm sono riuscito a compilare i miei codici e tutto funziona bene ma quando collego o scollego la cuffia non succede nulla. La funzione audioRouteChangeListenerCallback non è stata chiamata. C'è qualcos'altro che mi manca accanto alle funzioni sopra? –
È necessario registrarsi per la funzione listener DOPO la chiamata all'inizializzazione di AudioSession. Lo fai in questo modo? – prakash