【问题标题】:Remote Control for audio doesn't work音频遥控器不起作用
【发布时间】:2013-09-04 19:31:38
【问题描述】:

在我设法使用 MPMoviePlayerController 在后台播放音频并尝试使其接收远程控制之后。但是当我点击播放/暂停按钮时,没有反应,音频继续播放。 然后我尝试显示是否有日志输出但没有输出。

这是我的代码:

   -(void)viewDidAppear:(BOOL)animated{
...
       [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
    }

    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
        [self resignFirstResponder];
    }

    - (void)remoteControlReceivedWithEvent:(UIEvent *)event {
        NSLog(@"remoteControlReceivedWithEvent: %@", event);
        if (event.type==UIEventTypeRemoteControl) {
            if (event.subtype==UIEventSubtypeRemoteControlPlay) {
                NSLog(@"Play");
            } else if (event.subtype==UIEventSubtypeRemoteControlPause) {
                NSLog(@"Pause");
            } else if (event.subtype==UIEventSubtypeRemoteControlTogglePlayPause) {
                NSLog(@"Play Pause");
            }
        }
    }

感谢您的努力。

【问题讨论】:

    标签: ios events audio controls mpmovieplayercontroller


    【解决方案1】:

    您的视图控制器似乎不在响应者链中。
    这可能是由于 viewWillDisappear 中的 [self resignFirstResponder] 和/或另一个 ViewController 在前面。

    为了确保您接收到这些事件,您可以实现一个 UIApplication 子类,该子类将能够始终接收这些事件。所以哪个 ViewController 可见并不重要。 UIApplication 始终位于响应者链的末尾。

    为 UIApplication (MyUIApplication) 创建一个子类并实现以下 UIResponder 方法:

    - (void)remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
        ...
    }
    
    - (BOOL)canBecomeFirstResponder {
      return YES;
    }
    

    在 main.m 中使用以下命令启动应用程序(将 MyUIApplication 和 AppDelegate 替换为您的类名):

    @autoreleasepool {
          return UIApplicationMain(argc, argv, NSStringFromClass([MyUIApplication class]), NSStringFromClass([AppDelegate class]));
    }
    

    【讨论】:

      猜你喜欢
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多