Swift.Streaming来自URL的音频,错误代码为-999“已取消”

时间:2019-05-29 07:54:19

标签: swift streaming avaudioplayer

这是我的代码:

Internet Explorer

然后像这样在viewDidLoad()方法中调用streamAudio:

var playering = AVPlayer()


private func streamAudio(url: String) {
    guard let url = URL(string: url) else {return}
    let playerItems = AVPlayerItem(url: url)
    ReportVC.playering = AVPlayer(playerItem: playerItems)
    ReportVC.playering.play()
}

但我在控制台中收到此错误:

  

2019-05-29 12:19:32.940076 + 0430 CC [13305:463451]任务<314067AD-725A-4C8E-820A-18D0A9351624>。<1>加载失败,错误为Error Domain = NSURLErrorDomain Code = -999“已取消” UserInfo = {NSErrorFailingURLStringKey = https://stage.careandcarers.com/file/get/0b710400-4454-4a46-9ed5-8a542f046ed8.m4a,NSErrorFailingURLKey = https://stage.careandcarers.com/file/get/0b710400-4454-4a46-9ed5-8a542f046ed8.m4a,_ NSURLErrorRelatedURLSessionTaskErrorKey =(       “ LocalDataTask <314067AD-725A-4C8E-820A-18D0A9351624>。<1>”   ),_NSURLErrorFailingURLSessionTaskErrorKey = LocalDataTask <314067AD-725A-4C8E-820A-18D0A9351624>。<1>,NSLocalizedDescription = cancelled} [-999]   2019-05-29 12:19:32.943557 + 0430 CC [13305:460502]任务<314067AD-725A-4C8E-820A-18D0A9351624>。<1>已完成,错误-代码:-999   2019-05-29 12:19:32.943604 + 0430 CC [13305:461457] TIC读取状态[5:0x0]:1:57

出什么问题了?

1 个答案:

答案 0 :(得分:1)

尝试:

func streamPlay(streamUrl : string){
let playerItem = AVPlayerItem( url:NSURL( string: streamUrl )! as URL )
        player = AVPlayer(playerItem:playerItem)
        player!.rate = 1.0;
        //isplay is a bool variable that indicate your stream playing is in process or not(default value = false)
        if isPlay{

            player!.pause()
            //playDemo is button that you use to stream play
            playDemo!.setImage(UIImage(named: "playBtn"), for: UIControl.State.normal)
            playDemo!.setTitle("Pause", for: UIControl.State.normal)
            isPlay = false

        }else{

            player!.play()
            playDemo!.setImage(UIImage(named: "media_pause"), for: UIControl.State.normal)
            playDemo!.setTitle("play", for: UIControl.State.normal)
            isPlay = true
        }
}

在您的viewDidLoad中:

 let url = "https://stage.careandcarers.com/file/get/0b710400-4454-4a46-9ed5-8a542f046ed8.m4a"
 streamPlay(streamUrl : url)