كيفية كشف برمجيا سماعة الأذن في اي فون؟
-
11-09-2019 - |
سؤال
أنا أعمل حاليا على مشروع ينطوي على تشغيل الموسيقى من مكتبة الموسيقى iPhone داخل التطبيق في الداخل. أنا أستخدم MPMEDIAPICKERCONTROLLER للسماح للمستخدم بتحديد موسيقاهم وتشغيله باستخدام مشغل iPod Music داخل iPhone.
ومع ذلك، فقد ركضت مشكلة عندما يقوم المستخدم بإدراج سماعة الأذن له ويزيله. ستتوقف الموسيقى فجأة عن اللعب دون سبب. بعد بعض الاختبارات، اكتشفت أن مشغل iPod سيقابل اللعب مؤقتا عند فصل المستخدم عن جهاز سماته من الجهاز. فهل هناك أي طريقة للكشف عنها برمجيا إذا تم إلغاء توصيل الأذن بحيث يمكنني استئناف تشغيل الموسيقى؟ أو هل هناك أي طريقة لمنع لاعب iPod من التوقف عند فصل المستخدم سماعة الأذن الخاصة به؟
المحلول
يجب عليك التسجيل للحصول على إعلام 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..
}
نصائح أخرى
إذا كنت ترغب فقط في التحقق مما إذا كان يتم توصيل سماعات الرأس في أي وقت محدد، دون الاستماع إلى تغييرات توجيه، يمكنك ببساطة القيام بما يلي:
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);
}
هتافات، raffaello colasante
أراك تستخدم إطار MPMEDIAPLEYER ولكن يتم تشغيل معالجة الميكروفون باستخدام إطار Avaudioplayer، والتي ستحتاج إلى إضافتها إلى مشروعك.
يحتوي موقع Apple على رمز من إطار Avaudioplayer الذي أستخدمه للتعامل مع الانقطاعات من مستخدم يقوم بتوصيل أو إزالة سماعات Apple Microphone Headphones.
تحقق من أبل دليل برمجة الصوتيات مركز ديف.
- (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];
}
}
يا شباب فقط تحقق من تطبيق عينة AddMusic. سوف يحل جميع المشكلات الخاصة بك المتعلقة بود
أول تسجيل iPod Player للإشعار مع التعليمات البرمجية التالية
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];
}
}