UIWebView:当应用程序进入后台时,HTML5音频在iOS 6中暂停 [英] UIWebView: HTML5 audio pauses in iOS 6 when app enters background

查看:154
本文介绍了UIWebView:当应用程序进入后台时,HTML5音频在iOS 6中暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,

我的应用是一款音乐播放应用。我使用Javascript控制< audio> -Tag。到目前为止没有问题,播放,暂停,下一个和上一个按钮正在工作。当我在iOS 5中待机时,音乐会继续播放,但自动下一首歌不起作用。当它不处于待机状态时,它可以工作。在iOS 6中,只需按下按钮,音乐就会消失。

My app is a music playing app. I control the <audio>-Tag with Javascript. So far no problems, play, pause, next and previous buttons are working. When I stand-by the device in iOS 5, the music keeps playing, but the automatic next song doesn't work. When it isn't in stand-by, it works. And in iOS 6, just after pressing the button, the music fades out.

锁定屏幕上的播放/暂停按钮适用于iOS 5,但不适用于iOS 6。

The Play/Pause button on the lockscreen works in iOS 5, but not in iOS 6.

推荐答案

从iOS 6开始,您必须在创建UIWebView之前将音频会话类别设置为回放。这就是你所要做的。没有必要使会话处于活动状态。

Starting with iOS 6, you MUST set the audio session category to 'playback' before creating the UIWebView. This is all you have to do. It is not necessary to make the session active.

这也应该用于html视频,因为如果你没有配置会话,你的视频将被静音当振铃开关关闭时。

This should be used for html video as well, because if you don't configure the session, your video will be muted when the ringer switch is off.

#import <AVFoundation/AVFoundation.h>

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL ok;
NSError *setCategoryError = nil;
ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
                         error:&setCategoryError];
if (!ok) {
  NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
}

确保您的目标链接到AVFoundation框架。

Ensure that your target links to the AVFoundation framework.

如果使用Cordova,您需要修改的文件是 platforms / ios / MyApp / Classes / AppDelegate.m ,最终看起来像这样:

If using Cordova, the file you need to modify is platforms/ios/MyApp/Classes/AppDelegate.m, and will end up looking like this:

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

@implementation AppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    BOOL ok;
    NSError *setCategoryError = nil;
    ok = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
    if (!ok) {
        NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
    }

    self.viewController = [[MainViewController alloc] init];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end

此外,正如注释,您需要链接AVFoundation框架,如此答案

Also, as mentioned in the comments, you need to link the AVFoundation Framework, as explained in this answer:


  • 使用xcode打开您的项目 open ./platforms/ios/ MyApp.xcworkspace /

  • 项目导航器>目标我的应用>常规

  • 滚动到底部查找Linked Frameworks和图书馆

这篇关于UIWebView:当应用程序进入后台时,HTML5音频在iOS 6中暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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