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

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

问题描述

早安,

我的应用是一个音乐播放应用.我用 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/
  • 项目导航器 > 定位我的应用程序 > 常规
  • 滚动到底部以查找链接的框架和库

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

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