Swift 3:从服务器流式传输MP3文件

时间:2023-01-24 11:55:34

I am trying to stream a sample mp3 from a url. It is a very small sample file. However, when I press the play button nothing happens. Well, no sound plays.

我正在尝试从网址流式传输示例mp3。这是一个非常小的示例文件。但是,当我按下播放按钮时没有任何反应。好吧,没有声音播放。

I am rather new to Swift and very much a noob so here is the full View Controller as I am unsure what you might need.

我对Swift很新,而且非常像菜鸟,所以这里是完整的View Controller,因为我不确定你可能需要什么。

    import UIKit
    import AVKit
    import AVFoundation
    import Foundation

    class ViewController: UIViewController {

        @IBAction func playButton(_ sender: Any) {

            print("pressed")
            let urlstring = "http://cdn.mos.musicradar.com/audio/samples/80s-heat-demos/AM_Eighties08_85-01.mp3"
            let url = NSURL(string: urlstring)
            print("the url = \(url!)")
            downloadFileFromURL(url: url!)

        }



        override func viewDidLoad() {
            // Do any additional setup after loading the view
        }



        func downloadFileFromURL(url:NSURL){
            weak var weakSelf = self
            var downloadTask:URLSessionDownloadTask
            downloadTask = URLSession.shared.downloadTask(with: url as URL, completionHandler: { (URL, response, error) -> Void in
            print("URL = \(URL)")
            weakSelf!.plays(url: URL! as URL)

            })

            downloadTask.resume()

        }



        func plays(url:URL) {

            print("playing \(url)")

            do {

                var playerItem = AVPlayerItem(url: url as URL)

                player = AVPlayer(url : url)

                player.volume = 1.0

                player.play()

            } catch let error as NSError {



                print(error.localizedDescription)

            } catch {

                print("AVAudioPlayer init failed")

            }

        }

    }

in my console I am getting this:

在我的控制台中,我得到了这个:

pressed

压制

the url = http://cdn.mos.musicradar.com/audio/samples/80s-heat-demos/AM_Eighties08_85-01.mp3

url = http://cdn.mos.musicradar.com/audio/samples/80s-heat-demos/AM_Eighties08_85-01.mp3

URL = Optional(file:///private/var/mobile/Containers/Data/Application/C255E8F4-DDBA-43BB-BC33-AF71C08BBDFD/tmp/CFNetworkDownload_tm0O8v.tmp)

URL =可选(文件:///private/var/mobile/Containers/Data/Application/C255E8F4-DDBA-43BB-BC33-AF71C08BBDFD/tmp/CFNetworkDownload_tm0O8v.tmp)

playing file:///private/var/mobile/Containers/Data/Application/C255E8F4-DDBA-43BB-BC33-AF71C08BBDFD/tmp/CFNetworkDownload_tm0O8v.tmp

播放文件:///private/var/mobile/Containers/Data/Application/C255E8F4-DDBA-43BB-BC33-AF71C08BBDFD/tmp/CFNetworkDownload_tm0O8v.tmp

URL = Optional(file:///private/var/mobile/Containers/Data/Application/C255E8F4-DDBA-43BB-BC33-AF71C08BBDFD/tmp/CFNetworkDownload_J1f2LF.tmp)

URL =可选(file:///private/var/mobile/Containers/Data/Application/C255E8F4-DDBA-43BB-BC33-AF71C08BBDFD/tmp/CFNetworkDownload_J1f2LF.tmp)

playing file:///private/var/mobile/Containers/Data/Application/C255E8F4-DDBA-43BB-BC33-AF71C08BBDFD/tmp/CFNetworkDownload_J1f2LF.tmp

播放文件:///private/var/mobile/Containers/Data/Application/C255E8F4-DDBA-43BB-BC33-AF71C08BBDFD/tmp/CFNetworkDownload_J1f2LF.tmp

Can anyone help me solve this? I just want to be able to play this sound on click of the button from the server not locally.

谁能帮我解决这个问题?我只是希望能够在服务器上点击按钮而不是在本地播放此声音。

1 个解决方案

#1


1  

Please try to use this code for you player function.

请尝试将此代码用于播放器功能。

func plays(url:URL) {

        print("playing \(url)")

        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)

            player = try AVAudioPlayer(contentsOf: url)
            guard let player = player else { return }

            player.play()
        } catch let error as NSError {



            print(error.localizedDescription)

        } catch {

            print("AVAudioPlayer init failed")

        }

    }

#1


1  

Please try to use this code for you player function.

请尝试将此代码用于播放器功能。

func plays(url:URL) {

        print("playing \(url)")

        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)

            player = try AVAudioPlayer(contentsOf: url)
            guard let player = player else { return }

            player.play()
        } catch let error as NSError {



            print(error.localizedDescription)

        } catch {

            print("AVAudioPlayer init failed")

        }

    }