doge-man.com
import UIKit
import MediaPlayer
class ViewController: UIViewController {
var audioPlayer:AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
let url = "https://○○/○○○.wav"
self.downloadFileFromURL(url: URL(string:url)!)
}
func downloadFileFromURL(url:URL){
let downloadTask:URLSessionDownloadTask = URLSession.shared.downloadTask(with: url as URL) { (URL, response, error) in
self.play(url: URL!)
}
downloadTask.resume()
}
func play(url:URL) {
print("playing \(url)")
do {
self.audioPlayer = try AVAudioPlayer(contentsOf: url as URL)
audioPlayer.prepareToPlay()
audioPlayer.volume = 1.0
audioPlayer.play()
} catch let error as NSError {
//self.player = nil
print(error.localizedDescription)
} catch {
print("AVAudioPlayer init failed")
}
}
}