Ich sehe, Sie verwenden das MPMediaPlayer Framework, aber die Mikrofon-Handhabung erfolgt über das AVAudioPlayer-Framework, das Sie Ihrem Projekt hinzufügen müssen.
Die Apple-Website enthält Code aus dem AVAudioPlayer-Framework, mit dem ich Unterbrechungen durch einen Benutzer beheben kann, der die Kopfhörer des Apple-Mikrofons einsteckt oder entfernt.
Schauen Sie sich Apples 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];
}
}
Mein Code ist ein wenig anders, und einige von diesen können Sie 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];
}
}
erm Benachrichtigung implementieren Ich habe 2 Fehler beim Kompilieren 1) wat's inPropertyValue? Es wird entweder im Methodenparameter nicht deklariert. 2) CFDictionaryGetValue gibt einen void-Zeiger zurück, der mit CFNumberRef inkompatibel ist. Muss ich ein Casting machen, bevor ich den Wert zurückgebe? –
hmm es ist mir gelungen, meine codes zu kompilieren und alles läuft gut, aber wenn ich meinen kopfhörer einstecke oder ziehe passiert nichts. Die Funktion audioRouteChangeListenerCallback wurde nicht aufgerufen. Gibt es noch etwas, das mir neben den oben genannten Funktionen fehlt? –
Sie sollten sich für die Listener-Funktion NACH Ihrem Anruf beim Initialisieren von AudioSession registrieren. Machst du das? – prakash