简体   繁体   中英

How to play a mp3 url using Swift?

I would like to play a mp3.url using Swift.

Is there any way to use http url in the iOS app? or any other way to do this?

Please I would like to play a mp3.url using Swift.

func initPlay(){

     let url = "http://61.80.90.148/mp3-1.mp3"
            audioFile = NSURL(string: url) as URL?
    //error
            do {
                audioPlayer = try AVAudioPlayer(contentsOf: audioFile) 
            } catch let error as NSError { 
                print("error: \(error)")  
            }
    }
 Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value 2019-09-09 10:27:40.046265+0900 Aircon[27673:3377166] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value 

I think you cannot use direct url to playing music, convert to Data using Alamofire.

    Alamofire.download(URL.init(string: "http://61.80.90.148/mp3-1.mp3")!).responseData { (response) in
        if let value = response.result.value {
            do {
                self.audioPlayer = try AVAudioPlayer.init(data: value)
            }catch {
                // Error Handling
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM