Swift 3:从服务器流式传输MP3文件 [英] Swift 3: stream MP3 file from server

查看:124
本文介绍了Swift 3:从服务器流式传输MP3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从网址中流式传输示例mp3.这是一个非常小的示例文件.但是,当我按下播放按钮时,什么也没有发生.好吧,没有声音在播放.

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.

我对Swift还是相当陌生,非常菜鸟,所以这里是完整的View Controller,因为我不确定您可能需要什么.

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.

    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:

按下

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

URL =可选(文件:///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_tm0O8v.tmp)

播放文件:///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

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

URL = Optional(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

playing file:///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.

推荐答案

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

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")

        }

    }

这篇关于Swift 3:从服务器流式传输MP3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆