2016-09-27 66 views
0

在Swift中使用AVPlayer。 iPhone上没有错误,但没有声音播放。在Swift中使用AVPlayer。 iPhone上没有错误但没有声音播放

我是新来的斯威夫特。我会很感激回应。

这里是我的代码:

import UIKit 
import AVFoundation 

class PlayerViewController: UIViewController { 
    // This time, we'll declare avPlayer as an instance variable, 
    // which means it exists as long as our view controller exists. 
    var avPlayer: AVPlayer! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     print("Connecting...") 

     // If you'd rather not use this sound file, 
     // replace the string below with the URL of some other MP3. 
     let urlString = "http://live.canstream.co.uk:8000/bangradio.mp3" 
     let url = NSURL(string: urlString)! 
     avPlayer = AVPlayer(URL: url) 

     print("Connected to One Radio!") 
     avPlayer.play() 
     print("Now playing...") 
    } 

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

嗨,欢迎来到Stack Overflow。在调试方面到目前为止您尝试过了什么?例如,你确定你没有静音吗?你是否试图用Xcode模拟器等。 – tech4242

+0

我敢打赌,iOS 9应用传输安全功能阻止了你的http请求。看到这里的解决方案:http://stackoverflow.com/q/31254725/22147 –

+0

这不是我的问题,我仍然不能听音乐。任何解决方案 – Pavlos

回答

0

你会尝试使用AVPlayerItem(URL: “你的网址”),并设置成avplayer。

+0

他们为什么要这样做? – Moritz