2016-10-15 4 views
0

私のイヤホンの再生/一時停止ボタンを押したときに逃げようとしています。このために、私は最初にテキストラベルを変更しようとしています...すぐにイヤホンを使用する

さまざまなウェブサイトを見て、彼らはこれを実行したが動作しません。私の欠けているものはありますか?

私はあなたがオーディオの再生を持っている必要があります任意のRemoteControlEventsについてはviewController.swift

override func viewDidLoad() { 
    super.viewDidLoad() 
    canBecomeFirstResponder() 
    UIApplication.sharedApplication().beginReceivingRemoteControlEvents() 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

@IBOutlet weak var Cosa: UILabel! 

func play() { 
    Cosa.text = "Play" 
} 

override func canBecomeFirstResponder() -> Bool { 
    return true 
} 

override func remoteControlReceivedWithEvent(event: UIEvent?) { 
    guard let event = event else { 
     print("no event\n") 
     return 
    } 
    guard event.type == UIEventType.RemoteControl else { 
     print("received other event type\n") 
     return 
    } 
    switch event.subtype { 
    case UIEventSubtype.RemoteControlPlay: 
     print("received remote play\n") 
     play() 
    case UIEventSubtype.RemoteControlPause: 
     print("received remote pause\n") 
    case UIEventSubtype.RemoteControlTogglePlayPause: 
     print("received toggle\n") 
    default: 
     print("received \(event.subtype) which we did not process\n") 
    } 
} 
+0

については、以下のリンクを参照することができますが、コードのコメント:あなたのIBOutlet「コーザは」で始まる必要があります小文字。大文字で始まるObj-cとSwiftの名前は、型であると予想されます。メンバーの値、プロパティ、メソッドはすべて小文字で始まる必要があります。 – alexkent

答えて

0

に次のコードを持っています。

次の操作を行い、リモートコントロールイベントを受信するには、リモートコントロールイベント

のためのアプリケーションを準備

:あなたはをサポートする各アクションの

  1. 登録ハンドラを。 演奏オーディオを開始し、異なる種類のイベント
  2. の のハンドラを登録するには、共有 MPRemoteCommandCenterオブジェクトを使用します。あなたのアプリは「今遊んでいる」アプリでなければなりません。アプリケーションでは、オーディオ再生を開始するまでリモコンイベントを受信しません。

https://developer.apple.com/library/content/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Remote-ControlEvents/Remote-ControlEvents.html

また、あなたは、より明確化し、デモプロジェクトあなたの質問に関係のない Receive remote control events without audio

関連する問題