嵌入YouTube视频没有声音

时间:2016-07-18 21:21:55

标签: ios video swift2

当我在iOS模拟器中运行以下代码时,我会从嵌入的YouTube视频中获得声音。当我在iPhone 6上运行时,扬声器没有发出声音,但我从耳机中听到了声音。这是硬件问题还是软件?

import UIKit

class ThreeKeyViewController: UIViewController {

@IBOutlet weak var videoView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()
    let youTubeUrl = "https://www.youtube.com/embed/hP-vuMaoHjU"
    videoView.allowsInlineMediaPlayback = true

    dispatch_async(dispatch_get_main_queue(), { () -> Void in


        self.videoView.loadHTMLString("<iframe width=\"\(self.videoView.frame.width)\" height=\"\(self.videoView.frame.height)\" src=\"\(youTubeUrl)\" frameborder=\"0\" allowfullscreen></iframe>", baseURL: nil)

        print("This is run on the main queue, after the previous code in outer block")
    })
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

1 个答案:

答案 0 :(得分:1)

SWIFT 2.0版本我遇到了同样的问题,解决方案是在AppDelegate中添加。感谢Daleks。一定不要忘记导入AVFoundation。

Import UIKit
import AVFoundation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    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)  
    }
    return true
}enter code here