UIWebView:当应用程序进入后台时,HTML5 音频在 iOS 6 中暂停

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11616001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:24:04  来源:igfitidea点击:

UIWebView: HTML5 audio pauses in iOS 6 when app enters background

iosuiwebviewios6avfoundationhtml5-audio

提问by heysamhey

Good day,

再会,

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.

我的应用程序是一个音乐播放应用程序。我<audio>用 Javascript控制-Tag。到目前为止没有问题,播放、暂停、下一个和上一个按钮都在工作。当我在 iOS 5 中待机时,音乐一直播放,但自动下一首歌曲不起作用。当它不处于待机状态时,它可以工作。而在 iOS 6 中,只要按下按钮,音乐就会淡出。

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

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

回答by Chris Lundie

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.

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

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.

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

#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);
}

Ensure that your target links to the AVFoundation framework.

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



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

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

#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

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

此外,如评论中所述,您需要链接 AVFoundation 框架,如本答案所述

  • Open your project with xcode open ./platforms/ios/MyApp.xcworkspace/
  • Project navigator > target My App > General
  • Scroll to the bottom to find Linked Frameworks and Libraries
  • 用 xcode 打开你的项目 open ./platforms/ios/MyApp.xcworkspace/
  • 项目导航器 > 目标我的应用程序 > 常规
  • 滚动到底部以查找链接的框架和库

回答by Dev01

This plugin will make your app ignore the mute switch. It's basically the same code that's in the other answers but it's nicely wrapped into a plugin so that you don't have to do any manual objective c edits.

这个插件会让你的应用忽略静音开关。它基本上与其他答案中的代码相同,但它很好地包装在插件中,因此您无需进行任何手动目标 c 编辑。

https://github.com/EddyVerbruggen/cordova-plugin-backgroundaudio

https://github.com/EddyVerbruggen/cordova-plugin-backgroundaudio

Run this command to add it to your project:

运行此命令将其添加到您的项目中:

cordova plugin add https://github.com/EddyVerbruggen/cordova-plugin-backgroundaudio.git

回答by Weles

Swift Syntax:

快速语法:

in AppDelegate:

在 AppDelegate 中:

import AVFoundation

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  do{
      let audio = AVAudioSession.sharedInstance()
      try audio.setCategory(AVAudioSessionCategoryPlayback)
  }catch let error as NSError{
     print(error)
  }
}

回答by Aaleks

Here the SWIFT 2.0 version to set the audio session category to 'playback' before creating the UIWebView.

这里 SWIFT 2.0 版本在创建 UIWebView 之前将音频会话类别设置为“播放”。

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}
catch let error as NSError {
    print(error)  
}

do {
    try AVAudioSession.sharedInstance().setActive(true)
}
catch let error as NSError {
    print(error)  
}