2017-06-27 15 views
0

私はオーディオ録音/再生アプリケーションを持っています。しかし、私は定期的に有線のiPhoneヘッドセットで再生/一時停止ボタンを使用すると、再生を一時停止したい。 は、だから私は、リモートイベントの処理を実装:remoteControlReceived(イベント:UIEvent?)トリガーされません

// MARK: Overrides 
internal extension AppDelegate { 
    override func remoteControlReceived(with event: UIEvent?) { 
     super.remoteControlReceived(with: event) 
     /* some other logic */ 
    } 
} 

それから私はapplication: didFinishLaunchingWithOptions:で、リモートのイベント受信を開始:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { 
    application.beginReceivingRemoteControlEvents() 
    becomeFirstResponder() 
    /* some other logic */ 
    return true 
} 

しかし、いずれにせよremoteControlReceived(with event: UIEvent?)をトリガーされることはありません。

はまた、私はMPRemoteCommandCenterを試してみました:

MPRemoteCommandCenter.shared().togglePlayPauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in 
    return .success 
} 

が起動しません。

スイフトまたは客観的-c回答が受け付けられました。

どうしたのですか?または、私は何かを.plistに追加する必要がありますか?

答えて

0

[OK]を見つけることができますので、私はmixWithOthersオプションでオーディオセッションをアクティブにしています:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [.mixWithOthers]) 

ただし、私がmixWithOthersを使用している場合、アプリケーションはリモートイベントの受信を停止します。それは私がヘッドセットからtoggle play/pauseイベントを受信したい場合は、私は、このオプションを使用することができないことを、意味:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 

これは奇妙ですが、これは我々が持っているものです。だから私の場合、私はヘッドセットのボタンを使用することはできません:(

1

このコードを使用して仕事をしてください。アプリケーションのドキュメントは、私はそれが他のアプリとのサウンドのミックスする私のアプリを使用しています....理由を見つけ、here

MPRemoteCommandCenter.sharedCommandCenter().togglePlayPauseCommand.addTarget(self, action: #selector(togglePlayStop)) 
func togglePlayStop(){ 
    //isPlaying = !isPlaying 
} 
+0

はい、私はこれを試してみましたが、それもトリガしません。 –

関連する問題