ios - 单点触控远程控制事件不起作用

标签 ios events xamarin.ios controls

我想知道是否有 monotuch 的工作示例显示了接收远程控制事件(例如来自耳机按钮的事件)的工作示例。

我已经实现了一个单 View 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);
    }
}

最佳答案

我在这方面花了一点时间,我想我可能会给你答案。我的第一个错误假设是 Remote (耳机)上的音量增大和减小控件会注册,但它们不会。

除了通过反复试验,我还没有设法确认以下内容,但看起来您需要让 AVAudioPlayer 播放一些东西,或者至少在您启动 AVAudioSession 时播放一些东西。在不播放任何内容的情况下,播放/停止事件将传递给处理它的音乐应用程序。

在您的代码中,在调用 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 中使其正常工作。

希望这对您有所帮助。

关于ios - 单点触控远程控制事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12328002/

相关文章:

ios - 运行 Storyboard 项目时,符合 Monotouch 键值编码标准的崩溃

ios - Swift 返回错误 "method ' beginTrackingWithTouch(_ :withEvent: )' with Objective-C selector ' beginTrackingWithTouch:withEvent :' conflicts"

javascript - 显示单击了哪个列表项

c# - 从正在等待的事件返回反馈

c# - 单色触摸 : Memory leak when drawing PDF on a custom graphics context

uibutton - 具有 NavigationBar 后退按钮样式的自定义按钮

ios - 如何在 UitableviewCell 中填充 UICollectionViewCells

ios - 我可以在 iOS 6 中直接使用 Paypal SDK 吗?

ios - iphone "landscape"上的字体大小属性似乎更大?

c++ - 在 C++ main() 执行之前处理 Mac OS X 文件打开事件