我目前正在做一个项目,该项目涉及在应用程序内部播放iphone音乐库中的音乐。我使用MPMediaPickerController允许用户选择他们的音乐,并使用iPhone中的iPod音乐播放器播放它。
然而,当用户插入和移除耳机时,我遇到了问题。音乐将无缘无故地突然停止播放。经过测试,我发现当用户从设备上拔下耳机时,iPod播放器会暂停播放。那么,有没有办法通过编程来检测耳机是否已拔下,以便我可以继续播放音乐?或者,有没有办法防止iPod播放器在用户拔下耳机时暂停?
发布于 2009-12-02 19:52:38
您应该注册AudioRoute更改通知,并实现您希望如何处理路由器更改
// Registers the audio route change listener callback function
AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,
self);在回调中,您可以获得路由更改的原因
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..
}发布于 2010-07-06 05:38:05
如果您只想检查耳机是否已在任何给定时间插入,而不想监听路由更改,您可以简单地执行以下操作:
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);
}干杯,拉斐洛·科拉桑特
发布于 2009-12-02 18:56:24
我看到您正在使用MPMediaPlayer框架,但是麦克风处理是使用AVAudioPlayer框架完成的,您需要将其添加到您的项目中。
苹果的网站上有来自AVAudioPlayer框架的代码,我用它来处理用户插入或拔出苹果麦克风耳机时的中断。
看看苹果的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];
}
}我的代码稍有不同,其中一些可能会对您有所帮助:
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];
}
}https://stackoverflow.com/questions/1832041
复制相似问题