如何使应用程序在后台播放音频(iOS上为phonegap 1.5.0) [英] How to make app playing audio in the background (phonegap 1.5.0 on iOS)

查看:169
本文介绍了如何使应用程序在后台播放音频(iOS上为phonegap 1.5.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序可以播放一些音乐,可以使用PhoneGap和html5音频标签在iOS上播放音频.以前它可以在后台完美播放音乐(使用适用于iOS的PhoneGap 1.0.0),但是当我将PhoneGap升级到1.5.0(Cordova)之后,我再也无法在后台播放音乐了.

I have an app to play some music, using PhoneGap and html5 audio tag to play audio on iOS. previously it could play music in the background perfectly (with PhoneGap 1.0.0 for iOS), but after I upgraded PhoneGap to 1.5.0(Cordova), I can't get it play on the background anymore.

当我按HOME按钮时,音频会暂停.当我切换回应用程序时,它将恢复.

When I press HOME button, the audio pauses. When I switch back to app, it resumes.

我已经在Info.plist中将Required background modes设置为值audio.

I have already set the Required background modes in the Info.plist with value audio.

那么我有什么想念的吗?谢谢.

So is there something I missed? Thank you.

推荐答案

在MainController.h文件中,更改以下内容:

In the MainController.h file change the following:

#import "MainViewController.h"
#import <AVFoundation/AVFoundation.h>

@implementation MainViewController

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

        NSError *setCategoryError = nil;
        [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];

    }
    return self;
}

我不确定是否需要执行以下操作,但是我清理了我的项目(CMD + ALT + K),重新编译了PhoneGap/Cordova Lib(更改了目标并让其运行到模拟器)和(再次更改了目标之后)再次编译到应用程序中,现在可以使用了!

I'm not sure if the following is necessary but I cleaned my project (CMD+ALT+K), recompiled the PhoneGap / Cordova Lib (change target and let it run to simulator) and (after changing the target again) compiled to the application once more and now it works!

当心:它仅适用于设备,不适用于模拟器

Beware: it only works on Device, not on the simulator

这篇关于如何使应用程序在后台播放音频(iOS上为phonegap 1.5.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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