9

I'm testing my app in simulator.

I'm downloading file and getting it's local way like this one: file:///Users/administrator/Library/Developer/CoreSimulator/Devices/4CDF286B-543F-4137-B5E2-C312E19B992F/data/Containers/Data/Application/E5F13797-A6A8-48A1-B3C3-FBC3D7A03151/Documents/4d13e04980d3.mp3

Now I want to play this file with AVAudioPlayer but I'm always getting this error:

file:///
Error Domain=NSOSStatusErrorDomain Code=2003334207 "(null)"

Code for playing:

var alertSound = NSURL(fileURLWithPath: "file:///Users/administrator/Library/Developer/CoreSimulator/Devices/4CDF286B-543F-4137-B5E2-C312E19B992F/data/Containers/Data/Application/E5F13797-A6A8-48A1-B3C3-FBC3D7A03151/Documents/4d13e04980d3.mp3")
        print(alertSound)

        var error:NSError?
        do {
            try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound)
            audioPlayer.prepareToPlay()
            audioPlayer.play()
        } catch {
            print(error)
        }

How should I make it playing?

SwiftStudier
  • 2,272
  • 5
  • 21
  • 43
  • Are you down-loading and playing in the same run? I ask because every time you run your app in the simulator, your app folder will be different. If you want to play a sound in your app, and the sound does not change, then get the MP3 file, add it to your app in Xcode, and use NSBundle to get the URL of the file. –  Aug 30 '15 at 12:54
  • You're right about different app folders but audio can be changed and I need to download it and play immediately – SwiftStudier Aug 30 '15 at 17:51

4 Answers4

18

Under iOS8, the path that you have saved won't be valid across launches. The id you see "E5F13797-A6A8-48A1-B3C3-FBC3D7A03151" will change with each launch.

The solution is to save the filename and not the complete path, and to recreate the URL or complete path, by getting the path to the Documents (or tmp) folder and appending the filename to it.

mahboudz
  • 39,196
  • 16
  • 97
  • 124
  • Yep, I see id changed every time. I would thank you very much if you help me with some code of this, just saving file to documents folder and playing saved file in avaudioplayer – SwiftStudier Aug 31 '15 at 10:06
  • 2
    Save the file name, possibly by getting it from: `url.absolutePath.lastPathComponent`. When you want to play, make a new URL by appending the fieName to `let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! NSString` and using `fileURLWithPath` – mahboudz Aug 31 '15 at 10:14
  • I'm getting null-file error, can you please help me with full code?:) – SwiftStudier Aug 31 '15 at 13:53
  • I'm not writing in Swift, so I don't know if my code will help you. – mahboudz Aug 31 '15 at 20:13
  • Excellent! This is the trick to fetch the downloaded files from documents directory!. This has saved my day! – Pradeep Reddy Kypa Dec 05 '20 at 03:25
  • I get the same error with permanent remote url which is accessible from anywhere: https://www.learningcontainer.com/wp-content/uploads/2020/02/Kalimba.mp3 – Gargo Aug 02 '23 at 12:47
1

Look at NSFileManager. The method URLForDirectory:inDomain:appropriateForURL:create:error: is what you should use.

1
            let fileName = ("FILE_NAME.mp3")
        let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
        let soundFileURL = documentsDirectory.URLByAppendingPathComponent(fileName)
        AudioCenter.player.playURL(soundFileURL)
Atef
  • 2,872
  • 1
  • 36
  • 32
0
// create the soundFileURL for the word
        let toAppendString = "\(String)" //
        let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
        soundFileURL = documentsDirectory.URLByAppendingPathComponent(toAppendString)

    do {
    self.player = try AVAudioPlayer(contentsOfURL: soundFileURL)
        player.prepareToPlay()
        player.volume = 1.0
        player.play()

    } catch let error as NSError {
        print(error)
    }
richc
  • 1,648
  • 5
  • 20
  • 48