Play audio , save in document directory.
Some time we need to play a songs in application by using URL or pre save audio file. for this we need to follow some steps:
Steps are :
1: Avaudio player provides this facility;
Declare AVaudio player
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import UIKit import AVFoundation var audioPlayer:AVAudioPlayer! class ViewController: UIViewController{ override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let url = Bundle.main.url(forResource: "music", withExtension: "mp3")! do { audioPlayer = try AVAudioPlayer(contentsOf: url) guard let player = audioPlayer else { return } player.prepareToPlay() } catch let error { print(error.localizedDescription) } } |
Note: here “music ” is local store mp3 file .
2: by using URL:
for this we need to first download and play.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
@IBAction func downloadFile(_ sender: Any) { if let audioUrl = URL(string: "http://freetone.org/ring/stan/iPhone_5-Alarm.mp3") { // then lets create your document folder url let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! // lets create your destination file url let destinationUrl = documentsDirectoryURL.appendingPathComponent(audioUrl.lastPathComponent) print(destinationUrl) // to check if it exists before downloading it if FileManager.default.fileExists(atPath: destinationUrl.path) { print("The file already exists at path") // if the file doesn't exist } else { // you can use NSURLSession.sharedSession to download the data asynchronously URLSession.shared.downloadTask(with: audioUrl, completionHandler: { (location, response, error) -> Void in guard let location = location, error == nil else { return } do { // after downloading your file you need to move it to your destination url try FileManager.default.moveItem(at: location, to: destinationUrl) print("File moved to documents folder") } catch let error as NSError { print(error.localizedDescription) } }).resume() } } } |
3: For access the downloaded file and again play.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
@IBAction func playdownload(_ sender: Any) { if let audioUrl = URL(string: "http://freetone.org/ring/stan/iPhone_5-Alarm.mp3") { // then lets create your document folder url let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! // lets create your destination file url let destinationUrl = documentsDirectoryURL.appendingPathComponent(audioUrl.lastPathComponent) //let url = Bundle.main.url(forResource: destinationUrl, withExtension: "mp3")! do { audioPlayer = try AVAudioPlayer(contentsOf: destinationUrl) guard let player = audioPlayer else { return } player.prepareToPlay() player.play() } catch let error { print(error.localizedDescription) } } } |
4: you can also perform play and pause command .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@IBAction func play(_ sender: Any) { if audioPlayer.isPlaying{ audioPlayer.pause() }else{ audioPlayer.play() } } |