ios - 嵌入的 YouTube 视频没有声音

标签 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.
}

最佳答案

SWIFT 2.0 版本 我遇到了同样的问题,解决方案是添加 AppDelegate。感谢达莱克斯。一定不要忘记导入 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

关于ios - 嵌入的 YouTube 视频没有声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38446205/

相关文章:

ios - 我的 TableViewCell 自定义类中的 IbOutlet 从未被初始化

ios - 如何有效检测UI文本输入以调用Google Places API以获得建议?

iphone - UIGestureRecognizer 的子类仅在一个 UIImageView 上工作

ios - 我怎样才能避免重新加载整个表格,扰乱滚动

ios - 在 iOS 上的 Safari 中嵌入视频

api - YouTube 数据 API 视频插入/更新 - 设置 'Game title'

iphone - 通过 Cocoa Touch 更改 iPhone 应用程序图标

c++ - OpenCV (C++) 无法获取 XVID 视频文件帧率

swift - CFDictionary 不会桥接到 NSDictionary (Swift 2.0/iOS9)

ios - 类型不符合协议(protocol) - 类型别名到另一个协议(protocol)