2012-09-08 44 views
3

我想知道是否有單點觸摸的工作示例,它顯示了一個接收遠程控制事件(例如來自耳機按鈕的事件)的工作示例。單點觸控遠程控制事件不起作用

我已經實現了一個單一的視圖iPhone應用程序,實現了CanBecomeFirstResponder,名爲BecomeFirstResponder和UIApplication.SharedApplication.BeginReceivingRemoteControlEvents()但我沒有得到任何事件。

這是我的SingleViewController的代碼。

public partial class SingleViewViewController : UIViewController 
{ 
    public SingleViewViewController() : base ("SingleViewViewController", null) 
    { 
    } 

    public override void DidReceiveMemoryWarning() 
    { 
     // Releases the view if it doesn't have a superview. 
     base.DidReceiveMemoryWarning(); 

     // Release any cached data, images, etc that aren't in use. 
    } 

    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 

     // Perform any additional setup after loading the view, typically from a nib. 
     AVAudioSession audioSession = AVAudioSession.SharedInstance(); 
     NSError error; 
     audioSession.SetCategory(AVAudioSession.CategoryPlayback, out error); 
     audioSession.SetActive(true,out error); 

     this.BecomeFirstResponder(); 
     UIApplication.SharedApplication.BeginReceivingRemoteControlEvents(); 
    } 

    public override void ViewDidUnload() 
    { 
     base.ViewDidUnload(); 

     // Clear any references to subviews of the main view in order to 
     // allow the Garbage Collector to collect them sooner. 
     // 
     // e.g. myOutlet.Dispose(); myOutlet = null; 

     ReleaseDesignerOutlets(); 
    } 

    public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation) 
    { 
     // Return true for supported orientations 
     return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown); 
    } 

    public override bool CanBecomeFirstResponder { 
     get { 
      return true; 
     } 
    } 
    public override bool CanResignFirstResponder { 
     get { 
      return false; 
     } 
    } 

    public override void RemoteControlReceived (UIEvent theEvent) 
    { 
     base.RemoteControlReceived (theEvent); 
    } 
} 

回答

1

我花了一點時間在這個上,我想我可能會爲你解答。我的第一個錯誤的假設是,遙控器(耳機)上的音量調節控制器會進行註冊,但不會。

除了通過試驗和錯誤之外,我還沒有設法確認以下內容,但似乎需要讓AVAudioPlayer播放某些內容,或者至少在啓動AVAudioSession時播放某些內容。不玩東西播放/停止事件傳遞到音樂應用程序處理它。

在你的代碼,在呼叫基地後viewDidLoad方法,我加

AVAudioPlayer player = new AVAudioPlayer(new NSUrl("Music/test.m4a", false), null); 
player.PrepareToPlay(); 
player.Play(); 

如果你看一下在GitHub上這些樣品的第27章,你會看到播放音頻和手柄的例子遙控器事件。

https://github.com/mattneub/Programming-iOS-Book-Examples

我是不是能夠得到無播放器中播放工作遙控器的事件,你的例子匹配大量的OBJ-C樣品,但我不能使它在Xcode工作,要麼。

希望這會有所幫助。