当前位置:首页 > swift 播放网络音频

swift 播放网络音频

点击次数:1364  更新日期:2016-12-14

var audioPlayer:AVAudioPlayer!


func playsong(musicdata:NSData){

    do{

        try audioPlayer = AVAudioPlayer(data: musicdata)

        audioPlayer.prepareToPlay()

        audioPlayer.play()

    }catch {

        print(error)

    }

}



let str = "http://xxx/xxx.mp3"

let url = NSURL(string: str)

let configDefault = NSURLSessionConfiguration.defaultSessionConfiguration()

configDefault.timeoutIntervalForRequest = 15

let session1 = NSURLSession(configuration: configDefault)

let dataTask = session1.dataTaskWithURL(url!, completionHandler: {(data,response,error)->Void in

        print(error)

        playsong(data!)

    })

dataTask.resume()