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

查看:191
本文介绍了的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.

,我有这样的:

- (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;
    }
}

问题是,我在哪里把这个?苹果公司的文件似乎表明这应该在某些视图控制器去的地方,但我的应用程序有很多视图控制器和导航控制器。在多任务托盘中的遥控器无论我尝试把这个,出于某种原因窃听切换播放/暂停按钮或者导致这首歌只是停顿了一会儿,然后取消暂停,或某种原因导致首歌曲播放两次。

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.

推荐答案

我发现一对夫妇的解决方案来有点搜索后,收到关于苹果开发者论坛全球遥控器的事件。

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天全站免登陆