Je vois que vous utilisez MPMediaPlayer Framework mais la gestion du microphone est faite en utilisant le framework AVAudioPlayer, que vous devrez ajouter à votre projet.
Le site Web d'Apple contient le code de l'infrastructure AVAudioPlayer que j'utilise pour gérer les interruptions d'un utilisateur qui branche ou enlève les écouteurs de microphone Apple.
Consultez le iPhone Dev Center Audio Session Programming Guide d'Apple.
- (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];
}
}
Mon code est un peu différent et certains de cela peut vous aider:
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];
}
}
erm je vais avoir 2 erreur lors de la compilation 1) wat's inPropertyValue? Il n'apparaît pas non plus dans le paramètre de méthode 2) CFDictionaryGetValue renvoie un pointeur vide qui est incompatible avec CFNumberRef. Ai-je besoin de faire un casting avant de retourner la valeur? –
hmm j'ai réussi à compiler mes codes et tout fonctionne bien mais quand je branche ou débranche mon casque rien ne se passe. La fonction audioRouteChangeListenerCallback n'a pas été appelée. Y a-t-il autre chose qui me manque à côté des fonctions ci-dessus? –
Vous devez vous inscrire à la fonction d'écoute APRÈS votre appel à l'initialisation d'AudioSession. Faites-vous cela? – prakash