iOS 7 SDK不遵守背景音频 [英] iOS 7 SDK not abiding background audio

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

问题描述

我在Google和StackOverflow上做了很多研究。我发现的所有答案都不适用于iOS 7.我开始在iOS 7 SDK中使用Xcode 5编写新的应用程序。

I have done a lot of research, both on Google and StackOverflow. All the answers I found do not work in iOS 7. I started writing fresh app in iOS 7 SDK with Xcode 5.

我所要做的就是播放音频在应用程序中,来自存储在应用程序包中的文件(而不是来自音乐库)。我希望音频在后台播放并在屏幕锁定时进行控制(除控制中心外)。

All I'm trying to do is play audio in the app from a file stored in the app bundle (not from the Music library). I want to have audio played in background and controlled when screen is locked (in addition to Control Center).

我设置 APPNAME-Info.plist key, UIBackgroundModes audio 。它不处理app委托中的内容;一切都在ViewController中完成

I set the APPNAME-Info.plist key, UIBackgroundModes, to audio. It is not handling things in the app delegate; everything is done inside the ViewController

@interface ViewController : UIViewController <AVAudioPlayerDelegate>

在实现的 viewDidAppear:方法中我调用超级,然后是以下代码:

Within the implementation's viewDidAppear: method I call super and then the following code:

// Once the view has loaded then we can register to begin receiving controls and we can become the first responder
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

在我的实现的 viewWillDisappear:方法中,我具有以下代码:

In my implementation's viewWillDisappear: method, I have the following code:

// End receiving events
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];

我还实现了 canBecomeFirstResponder 方法,返回。接下来,我实现了 remoteControlReceivedWithEvent:方法:

I have also implemented the canBecomeFirstResponder method, which returns YES. Next, I implemented the remoteControlReceivedWithEvent: method:

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    // If it is a remote control event handle it correctly
    if (event.type == UIEventTypeRemoteControl) {
        if (event.subtype == UIEventSubtypeRemoteControlPlay) {
            [self playPauseAudio:self];
        } else if (event.subtype == UIEventSubtypeRemoteControlPause) {
            [self playPauseAudio:self];
        } else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
            [self playPauseAudio:self];
        }
    }
}

令我困惑的是,这个完全相同的设置在iOS 6上运行正常。在iOS 7上,它不起作用。它曾经在iOS 6中如此简单。在iOS 7 SDK中有些根本改变了。我错过了什么?

What is confusing me is that this exact same setup was working fine on iOS 6. On iOS 7, it doesn't work. It used to be so easy in iOS 6. Something fundamentally changed in iOS 7 SDK. What am I missing?

推荐答案

我设法解决了这个问题,并且为了挽救另一个可怜的灵魂的头发而去:

I managed to solve this, and to save hair pulling by another poor soul here goes:

首先确保你的Info.plist正确地将音频列为背景模式。

Firstly make sure your Info.plist correctly lists audio as a background mode.

(如果你不知道我是什么在谈论选择YOURAPPNAME-Info.plist选择它。单击加号并添加一个名为 UIBackgroundModes 的新密钥并展开它。添加一个名为<$ c $的值c> audio 。)

(If you dont know what i'm talking about select YOURAPPNAME-Info.plist select that. Click oin the plus sign and add a new key called UIBackgroundModes and expand it. Add a value called audio.)

您需要引用任何正在创建音频的播放对象。由于我只播放音频并且AV播放器不遵守背景音频,请在视图控制器的标题中使用它:

You'll need a reference to whatever playback object is creating the audio. Since I'm only playing audio and AVplayer was not abiding by the background audio, use this in your view controller's header:

@property (nonatomic, retain) MPMoviePlayerController *audioPlayer;

在实施中,执行以下操作:

In the implementation, do the following:

 [super viewDidAppear:animated];

    //Once the view has loaded then we can register to begin recieving controls and we can become the first responder
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    //End recieving events
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];

添加两种方法

//Make sure we can recieve remote control events
- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void) registerForAudioObjectNotifications {

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

    [notificationCenter addObserver: self
                           selector: @selector (handlePlaybackStateChanged:)
                               name: MixerHostAudioObjectPlaybackStateDidChangeNotification
                             object: audioObject];
}

现在所有重要代码 - 这使您的应用程序可以控制来自控制中心的音频并从锁定屏幕:

now ALL important code - this enables your app to control audio from "control center" and from lock screen:

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {

    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:
                [self playOrStop: nil];
                break;

            default:
                break;
        }
    }
}

你可以添加很多种类型这里的事件类型并调用任何方法。

you can add many many types of Event types here and call any method.

典型事件是:

 UIEventSubtypeRemoteControlPlay                 = 100,  //Parent EVENT

// All below are sub events and you can catch them using switch or If /else.
    UIEventSubtypeRemoteControlPause                = 101,
    UIEventSubtypeRemoteControlStop                 = 102,
    UIEventSubtypeRemoteControlTogglePlayPause      = 103,
    UIEventSubtypeRemoteControlNextTrack            = 104,
    UIEventSubtypeRemoteControlPreviousTrack        = 105,
    UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
    UIEventSubtypeRemoteControlEndSeekingBackward   = 107,
    UIEventSubtypeRemoteControlBeginSeekingForward  = 108,
    UIEventSubtypeRemoteControlEndSeekingForward    = 109,

要调试帮助,您可以使用:

To Debug help you can use:

 MPMoviePlayerController *mp1= (MPMoviePlayerController *)[notification object];
    NSLog(@"Movie State is: %d",[mp1 playbackState]);

    switch ([mp1 playbackState]) {
        case 0:
            NSLog(@"******* video has stopped");
            break;
        case 1:
            NSLog(@"******* video is playing after being paused or moved.");
                       break;
        case 2:
            NSLog(@"******* video is paused");
                break;
        case 3:
            NSLog(@"******* video was interrupted");
            break;
        case 4:
            NSLog(@"******* video is seeking forward");
                     break;
        case 5:
            NSLog(@"******* video is seeking Backwards");
            break;
        default:
            break;

就是这样 - 希望它有助于那里的一些人! - 这在带有Storyboard应用程序的iOS 7和iOS 6以及使用耳机和所有新控制中心的控制方面都非常完美。

and thats it - hope it helps some one out there! - this is working perfect on iOS 7 and iOS 6 with Storyboard app as well as control using Headphone and all new control centre too.

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

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