10

ヘッドセットの再生/一時停止ボタンのクリックを検出する方法はありますか?

以下を使用して、音量ボタンのクリックを検出することができました。

AudioSessionAddPropertyListener( kAudioSessionProperty_CurrentHardwareOutputVolume , audioVolumeChangeListenerCallback, self );

しかし、中央のボタンのAudioSessionPropertyが見つかりません。それを行う方法は何ですか?

4

3 に答える 3

8

アプリの外部から行われることはすべて「リモートイベント」と見なされます。ホームボタンをダブルタップしてそこで再生/一時停止を押すと、ヘッドセットの再生/一時停止ボタンを押すのと同じです(次はダブルタップ、前はトリプルタップと同じです)。

iOSのリモートイベントのイベント処理に関するガイドは次のとおりです。

個人的には、MainWindow(UIWindow)をサブクラス化し、メソッドをオーバーライドするのが好きなsendEvent:ので、より直接管理できます。

- (void)sendEvent:(UIEvent *)event
{
    if (event.type == UIEventTypeRemoteControl)
    {
        // Do stuff here
    }
    else
    {
        // Not my problem.
        [super sendEvent:event];
    }
}

お役に立てば幸いです。中央のボタンのイベントの列挙型はですUIEventSubtypeRemoteControlTogglePlayPause

于 2011-09-18T05:48:07.203 に答える
2

上記のすべてを試しましたが、残念ながら今はどれも機能していないようです。それから私は覗いてbeginReceivingRemoteControlEvents、これを見つけました

iOS 7.1以降では、共有MPRemoteCommandCenterオブジェクトを使用してリモートコントロールイベントに登録します。共有コマンドセンターオブジェクトを使用する場合は、このメソッドを呼び出す必要はありません。

その後、チェックアウトしMPRemoteCommandCenter、最終的にMPRemoteCommandドキュメントページに行き着きました。

良いことは、この例があることです:

let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.addTarget(handler: { (event) in    

    // Begin playing the current track    
    self.myMusicPlayer.play()
    return MPRemoteCommandHandlerStatus.success
})

真ん中のボタンを読みたい場合は、次のことができます。

MPRemoteCommandCenter.shared().togglePlayPauseCommand.addTarget { (event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus in

 // middle button (toggle/pause) is clicked
 print("event:", event.command)

 return .success
}

これは機能し、ヘッドフォンの中央のボタンを検出することができました。

注:上記のコードをどこに配置するかによって、動作が異なることに気付きました。つまり、View Controllerに配置した場合、報告されたイベントは同一であり、AppDelegateのdidFinishLaunchingに配置した場合、報告されたイベントは異なります。いずれにせよ、イベントが検出されます。

于 2019-10-05T14:59:13.320 に答える
1

缶の答えは良かったのですが、時代遅れだと思います。

次に、UIApplicationをサブクラス化する必要があります。

のコードmain.m

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "MyUIApplication.h"

int main(int argc, char * argv[]) {
  @autoreleasepool {
    return UIApplicationMain(
      argc,
      argv,
      NSStringFromClass([MyUIApplication class]),
      NSStringFromClass([AppDelegate class]));
  }
}

のコードMyUIApplication.m

@implementation MyUIApplication
- (void)sendEvent:(UIEvent *)event {
  if (event.type == UIEventTypeRemoteControl) {
    // Check event.subtype to see if it's a single click, double click, etc.
  } else {
    // Not my problem.
    [super sendEvent:event];
  }
}
@end

のコードAppDelegate.m

の中に- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

電話[application beginReceivingRemoteControlEvents];

于 2017-10-18T04:58:42.760 に答える