Tôi thấy bạn đang sử dụng MPMediaPlayer Framework tuy nhiên việc xử lý micrô được thực hiện bằng cách sử dụng khung công tác AVAudioPlayer, mà bạn sẽ cần phải thêm vào dự án của mình.
Trang web của Apple có mã từ khung AVAudioPlayer mà tôi sử dụng để xử lý các gián đoạn từ người dùng cắm vào hoặc tháo tai nghe micrô của Apple.
Kiểm tra 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];
}
}
Mã của tôi là một chút khác nhau và một số những điều này có thể giúp bạn:
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];
}
}
Nguồn
2009-12-02 10:56:23
erm tôi đang gặp 2 lỗi khi biên dịch 1) inPropertyValue của wat? Nó không bị suy giảm hoặc trong tham số phương thức 2) CFDictionaryGetValue trả về một con trỏ void không tương thích với CFNumberRef. Tôi có cần làm bất kỳ việc đúc nào trước khi trả lại giá trị không? –
hmm tôi quản lý để biên dịch mã của tôi và tất cả mọi thứ chạy tốt nhưng khi tôi cắm vào hoặc rút phích cắm tai nghe của tôi không có gì xảy ra. Hàm audioRouteChangeListenerCallback không được gọi. Có điều gì khác tôi đang thiếu bên cạnh các chức năng trên không? –
Bạn nên đăng ký cho chức năng nghe sau khi cuộc gọi của bạn để khởi tạo AudioSession .. Bạn có làm theo cách đó không? – prakash