应用进入后台后,启用UIWebView音频以继续播放

时间:2017-01-19 21:51:19

标签: ios swift audio uiwebview avfoundation

摘要

我目前正在开发的应用使用UIWebViewallowsInlineMediaPlayback属性的组合来播放音频。 webView内容UIWebView目前在webView内播放正常 - 甚至播放列表也可以毫不费力地播放每首歌曲。但是,当应用程序进入后台时,音频将完全停止播放。

从这里开始,我必须拉出控制中心并继续手动播放音频。完成此操作后,音频再次开始播放,没有任何此类问题。它甚至可以毫无问题地通过播放列表。

我已经研究过各种解决方案(包括AVFoundation框架,以及AudioToolbox框架),但似乎没有任何效果。我认为音频在退出时不会继续播放,因为它是来自import UIKit import AVFoundation import AudioToolbox @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? let defaults = UserDefaults.standard func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 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) } } } 的内嵌视频的音频。据说,如果我能够通过拉起控制中心并点击播放按钮继续播放整个播放列表,我会相信有一个解决方法。

AppDelegate.swift

import UIKit
import WebKit
import AVFoundation

class ViewController: UIViewController, UIWebViewDelegate, UIScrollViewDelegate {

    @IBOutlet var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set WebView Load Configurations
        let webPlayer = URL(string: "https://urlHere.com")
        let webRequest = URLRequest(url: webPlayer!)
        webView.loadRequest(webRequest)
        webView.allowsInlineMediaPlayback = true
        webView.mediaPlaybackAllowsAirPlay = true
        webView.mediaPlaybackRequiresUserAction = false
        webView.allowsPictureInPictureMediaPlayback = true

        // Set WebView Scrolling Configurations
        webView.scrollView.delegate = self
        webView.scrollView.bounces = false
        webView.scrollView.isScrollEnabled = false

        // Set WebView Background
        webView.isOpaque = false
        webView.backgroundColor = UIColor(red:0.10, green:0.10, blue:0.11, alpha:1.0)
        webView.scrollView.backgroundColor = UIColor(red:0.10, green:0.10, blue:0.11, alpha:1.0)
    }

    // Hide Status Bar
    override var prefersStatusBarHidden: Bool {
        return true
    }

    // Disable Zoom for WebView
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return nil
    }

    // Workaround for Scrolling Offset Bug
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        scrollView.bounds = webView.bounds
    }

}

ViewController.swift

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>

<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
</array>

的Info.plist

Keyboards

结论

有没有办法让这个工作没有通过控制中心手动播放暂停的歌曲的麻烦?即使是在后台进程后播放音乐的解决方法也暂停了吗?我只是不确定从这里去哪里。任何见解都是受欢迎的,非常感谢。

感谢阅读!

0 个答案:

没有答案