実現したいこと
WKWebViewを利用したアプリを作っています。
このViewでは全ページで検索したYoutubeの映像が流れるView(SecondViewController)です。
WKWebViewでバックグランド再生をしたいのですが、アプリをバックグラウンドにした時に停止してしまいます。
詳しい方がいらっしゃいましたらご教示いただけますと幸いです。
試したこと
2, SecondViewControllerに以下の記述を行う。
import UIKit import WebKit import AVFoundation ⬅追記 class SecondViewController: UIViewController, WKUIDelegate { var webView: WKWebView! var mySearchString = "" override func loadView() { let webConfiguration = WKWebViewConfiguration() webView = WKWebView(frame: .zero, configuration: webConfiguration) webView.uiDelegate = self view = webView } override func viewDidLoad() { super.viewDidLoad() let myURL:String = "https://www.youtube.com/results?search_query=(mySearchString)" let encURL = URL(string:myURL.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!) let myRequest = URLRequest(url: encURL!) webView.load(myRequest) } //バックグラウンド対応するための記述 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool { let audioSession = AVAudioSession.sharedInstance() do { try audioSession.setCategory(AVAudioSessionCategoryPlayback) } catch { print("Setting category to AVAudioSessionCategoryPlayback failed.") } return true }
こちらではうまくバックグラウンド再生ができません。
ご教示いただけますと幸いです。
よろしくお願い致します。
あなたの回答
tips
プレビュー