Play local audio file with AVAudioPlayer

后端 未结 4 1893
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 07:51

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/

相关标签:
4条回答
  • 2020-12-19 08:09
                let fileName = ("FILE_NAME.mp3")
            let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
            let soundFileURL = documentsDirectory.URLByAppendingPathComponent(fileName)
            AudioCenter.player.playURL(soundFileURL)
    
    0 讨论(0)
  • 2020-12-19 08:11

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

    0 讨论(0)
  • 2020-12-19 08:17
    // 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)
        }
    
    0 讨论(0)
  • 2020-12-19 08:22

    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.

    0 讨论(0)
提交回复
热议问题