2016-07-18 61 views
1

當我在iOS模擬器中運行以下代碼時,我從嵌入的YouTube視頻獲取聲音。當我在iPhone 6上運行這個功能時,我聽不到來自揚聲器的聲音,但是我確實從耳機聽到了聲音。這是硬件問題還是軟件?沒有聲音來自嵌入的YouTube視頻

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

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