iOS 4:背景音频的遥控器 [英] iOS 4: Remote controls for background audio

查看:29
本文介绍了iOS 4:背景音频的遥控器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试为我正在为 iOS 4 开发的应用程序设置背景音频.但是,该应用程序没有专用的音乐播放器 viewController,但与其他背景音频应用程序不同比如潘多拉,让任务有点混乱.

I'm currently attempting to set up background audio for an app I'm developing for iOS 4. The app doesn't have a dedicated music player viewController, however, unlike other background audio apps such as Pandora, which makes the task a bit more confusing.

我已经正确设置了适当的 Info.plist 设置,并且在我的应用程序委托中有一个 AVAudioPlayer 对象,它可以从任何地方访问.当用户播放歌曲时,我将 AVAudioPlayer 替换为用歌曲初始化的新歌曲并播放.这一切都很好,除了现在我不知道如何支持远程控制事件.

I've set the appropriate Info.plist settings correctly and have an AVAudioPlayer object in my app delegate which is accessible from everywhere. When the user plays a song, I replace the AVAudioPlayer with a new one initialized with the song and play it. This all works great, except now I have no idea how to go about supporting remote control events.

根据 Apple 的文档,我有这个:

Based on Apple's documentation, I have this:

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

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

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    switch(event.subtype) {
        case UIEventSubtypeRemoteControlTogglePlayPause:
            if([iPhoneAppDelegate backgroundAudioPlayer].playing)
                [iPhoneAppDelegate pauseBackgroundAudioPlayer];
            else
                [iPhoneAppDelegate playBackgroundAudioPlayer];
            break;
    }
}

问题是,我把这个放在哪里?Apple 的文档似乎建议这应该放在某个视图控制器中,但我的应用程序有很多视图控制器和导航控制器.无论我想把它放在哪里,出于某种原因,点击多任务托盘遥控器中的切换播放/暂停按钮要么导致歌曲暂停片刻然后取消暂停,要么以某种方式导致歌曲播放两次.

The thing is, where do I put this? Apple's documentation seems to suggest this should go in some view controller somewhere, but my app has lots of view controllers and navigation controllers. Wherever I try to put this, for some reason tapping the Toggle Play/Pause button in the multitasking tray remote controls either causes the song to just pause for a moment and then unpause, or somehow causes the song to play twice.

推荐答案

经过一番搜索,我在 Apple 开发者论坛上找到了几个接收全局远程控制事件的解决方案.

I found a couple of solutions to receiving global remote control events on the Apple Developer Forums after a bit of searching.

一种方法是子类化 UIWindow 并覆盖它的 remoteControlReceivedWithEvent:.

One way is to subclass UIWindow and override its remoteControlReceivedWithEvent:.

第二种可能更好的方法是子类化 UIApplication 并覆盖 sendEvent:.这样,您就可以拦截所有远程控制事件并在那里全局处理它们,而不会让任何其他响应者在响应者链中稍后处理它们.

The second, perhaps nicer way is to subclass UIApplication and override sendEvent:. That way, you can intercept all the remote control events and handle them there globally, and not have any other responders handle them later in the responder chain.

- (void)sendEvent:(UIEvent *)event {
     if (event.type == UIEventTypeRemoteControl) {
          // Handle event
     }
     else
          [super sendEvent:event];
}

这篇关于iOS 4:背景音频的遥控器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆