【问题标题】:AVAudioPlayer not playing mp3 soundAVAudioPlayer 不播放 mp3 声音
【发布时间】:2016-04-30 20:04:05
【问题描述】:

我正在尝试使用 AVAudioPlayer 播放 Spotify 歌曲的 30 秒预览。 mp3 文件的 url 如下所示:https://p.scdn.co/mp3-preview/4f5fa753f8e814c51c718bf34bbc594bf55ef5b2

这是我播放这首歌的代码:

trackObj.previewURL = NSURL(fileURLWithPath:"https://p.scdn.co/mp3-preview/4f5fa753f8e814c51c718bf34bbc594bf55ef5b2")
// set up the audio player
if trackObj.previewURL != nil {
    do {
        try previewPlayer = AVAudioPlayer(contentsOfURL: trackObj.previewURL)
        previewPlayer.prepareToPlay()
        previewPlayer.play()
    } catch _ {}
}

这没有任何作用。歌曲根本没有开始播放。

【问题讨论】:

    标签: swift avaudioplayer spotify


    【解决方案1】:

    AVAudioPlayer 不适用于远程 URL。改用 AVPlayer。

    【讨论】:

      【解决方案2】:

      试试这个,

         var aSongURL: String = String(format: " https://p.scdn.co/mp3-preview/4f5fa753f8e814c51c718bf34bbc594bf55ef5b2")
              // NSLog(@"Song URL : %@", aSongURL);
          var aPlayerItem: AVPlayerItem = AVPlayerItem(URL: NSURL(string: aSongURL)!)
          var anAudioStreamer: AVPlayer = AVPlayer(playerItem: aPlayerItem)
          anAudioStreamer.play()
      

      避免语法错误,因为我在这里有类型

      希望这会有所帮助:)

      【讨论】:

        猜你喜欢
        • 2015-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-22
        • 1970-01-01
        • 1970-01-01
        • 2014-08-15
        相关资源
        最近更新 更多