Как программно обнаружить динамик в iphone?
В настоящее время я работаю над проектом, который включает в себя воспроизведение музыки из музыкальной библиотеки iphone внутри приложения. Я использую MPMediaPickerController, чтобы пользователь мог выбирать свою музыку и воспроизводить ее с помощью музыкального плеера iPod в iPhone.
Однако, я столкнулся с проблемой, когда пользователь вставляет свой динамик и удаляет его. Музыка внезапно перестанет играть без причины. После некоторого тестирования я узнал, что проигрыватель iPod приостанавливает воспроизведение, когда пользователь отключает наушник от устройства. Итак, есть ли способ программно определить, отключен ли наушник, чтобы я мог возобновить воспроизведение музыки? Или есть ли способ предотвратить переключение iPod-плеера, когда пользователь отключает наушник?
Ответы
Ответ 1
Вам необходимо зарегистрироваться для изменения уведомлений 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..
}
Ответ 2
Если вы просто хотите проверить, подключены ли наушники в любой момент времени, не прослушивая изменения маршрута, вы можете просто сделать следующее:
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);
}
Cheers,
Raffaello Colasante
Ответ 3
Я вижу, что вы используете MPMediaPlayer Framework, однако обработка микрофона выполняется с использованием среды AVAudioPlayer, которую вам нужно будет добавить в свой проект.
На веб-сайте Apple есть код из структуры AVAudioPlayer, который я использую для обработки прерываний от пользователя, подключающего или удаляющего наушники микрофона Apple.
Отъезд Apple Руководство по программированию аудиозаписей iPhone Dev Center.
- (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];
}
}
Ответ 4
Эй, ребята, просто проверьте пример приложения AddMusic. Решение всех проблем, связанных с iPod,
Сначала зарегистрируйте плеер iPod для уведомления со следующим кодом
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter
addObserver: self
selector: @selector (handle_PlaybackStateChanged:)
name: MPMusicPlayerControllerPlaybackStateDidChangeNotification
object: musicPlayer];
[musicPlayer beginGeneratingPlaybackNotifications];
И внедрите следующий код в уведомлении
- (void) handle_PlaybackStateChanged: (id) notification
{
MPMusicPlaybackState playbackState = [musicPlayer playbackState];
if (playbackState == MPMusicPlaybackStatePaused)
{
[self playiPodMusic];
}
else if (playbackState == MPMusicPlaybackStatePlaying)
{
}
else if (playbackState == MPMusicPlaybackStateStopped)
{
[musicPlayer stop];
}
}