3

ヘッドフォン ボタンなどのリモート コントロール イベントを受信するための実際の例を示す、モノタッチの実際のサンプルがあるかどうかを知りたいです。

私は単一のビューのiPhoneアプリを実装し、CanBecomeFirstResponderを実装し、BecomeFirstResponderとUIApplication.SharedApplication.BeginRecomingRemoteControlEvents()を呼び出しましたが、イベントを取得しません。

これが私の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);
    }
}
4

1 に答える 1

1

私はこれに少し時間を費やしました、そして私はあなたのために答えがあるかもしれないと思います。私の最初の誤った仮定は、リモート(ヘッドフォン)の音量アップとダウンコントロールが登録されるが、登録されないというものでした。

試行錯誤を除いて、次のことを確認することはできませんでしたが、AVAudioSessionを開始するときに、AVAudioPlayerで何かを再生するか、少なくとも何かを再生する必要があるようです。何かを再生せずに、再生/停止イベントはそれを処理するミュージックアプリに渡されます。

あなたのコードでは、baseの呼び出し後の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でも機能させることができませんでした。

お役に立てれば。

于 2013-02-10T02:11:41.983 に答える