Record audio to Local and fetch and Play iOS swift 3


When the audio has to be recorded and saved to the document directory then save the filename to the database.
If you save the path then it will vary every time.you cannot able to play.
     Code:


        let currentFileName = "recording-\(format.string(from: Date())).m4a"

        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let URL = documentsDirectory.appendingPathComponent(currentFileName)
        let FileName = currentFileName

Get the filename from the database and use the below code to play the saved audio
     Code:

            let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
            let urls = documentsDirectory.appendingPathComponent(soundFileURL)
            let asset = AVURLAsset(url: urls, options: nil)

Get the duration from the file and display.
     Code:
            let audioDuration = asset.duration
            let audioDurationSeconds = CMTimeGetSeconds(audioDuration)

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift