16

私は現在、アプリ内の iPhone ミュージック ライブラリから音楽を再生するプロジェクトに取り組んでいます。MPMediaPickerController を使用して、ユーザーが音楽を選択し、iPhone 内の iPod 音楽プレーヤーを使用して再生できるようにしています。

ただし、ユーザーがイヤホンを挿入して取り外すときに問題が発生しました。理由もなく突然音楽の再生が停止します。いくつかのテストの後、ユーザーがイヤホンをデバイスから外すと、iPod プレーヤーの再生が一時停止することがわかりました。それで、音楽の再生を再開できるように、イヤホンが取り外されたかどうかをプログラムで検出する方法はありますか? または、ユーザーがイヤホンのプラグを抜いたときに iPod プレーヤーが一時停止しないようにする方法はありますか?

4

4 に答える 4

9

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..                   
  }
于 2009-12-02T11:52:38.207 に答える
4

ルートの変更を聞くことなく、ヘッドフォンが接続されているかどうかをいつでも確認したい場合は、次のようにします。

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);
}

乾杯、ラファエロ・コラサンテ

于 2010-07-05T21:38:05.233 に答える
2

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];
    }
}
于 2010-10-22T06:39:45.197 に答える
2

MPMediaPlayer フレームワークを使用しているようですが、マイクの処理は、プロジェクトに追加する必要がある AVAudioPlayer フレームワークを使用して行われます。

Apple の Web サイトには、AVAudioPlayer フレームワークのコードがあり、これを使用して、ユーザーが Apple マイク ヘッドフォンを接続したり取り外したりすることによる中断を処理しています。

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];
    }
}

私のコードは少し異なり、これのいくつかはあなたを助けるかもしれません:

    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];
    }
}
于 2009-12-02T10:56:23.600 に答える