どのようにプログラムでiphoneにイヤーピースを検出するには?
-
11-09-2019 - |
質問
私は現在、内部のアプリ内でiPhoneの音楽ライブラリから音楽を演奏関係のプロジェクトに取り組んでいます。私は、ユーザーが自分の音楽を選択して、iPhone内のiPodの音楽プレーヤーを使用して、それを再生できるようにMPMediaPickerControllerを使用しています。
しかし、私は、ユーザーが自分のイヤホンを挿入したときに問題に遭遇し、それを削除します。音楽が突然、理由もなく再生を停止します。いくつかのテストの後、私は、ユーザーがデバイスから彼のイヤホンを抜いたときに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);
}
乾杯、 ラファエロColasante
私はあなたがしかし、MPMediaPlayerフレームワークを使用しているマイクの取り扱いは、あなたのプロジェクトに追加する必要がありますAVAudioPlayerフレームワークを使用して行われている参照してください。
Appleのウェブサイトは、私は、ユーザーに差し込むか、アップルのマイク、ヘッドホンを取り外すから中断を処理するために使用AVAudioPlayerフレームワークからのコードを持っています。
Appleの<のhref = "http://developer.apple.com/iphone/library/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/HandlingAudioInterruptions/HandlingAudioInterruptions.html#//apple_ref/doc/uid/TP40007875-CH11をチェック-SW1" のrel = "nofollowをnoreferrer"> iPhoneデベロッパーセンターオーディオセッションプログラミングガイドでます。
- (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関連の問題を解決します。
まず、コード
以下に通知のための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];
}
}