hessaum / swift-audio-recorder

A simple app and UI for recording/playing audio
1 stars 0 forks source link

soundPlayer identifier error #1

Open hessaum opened 6 years ago

hessaum commented 6 years ago

Im not sure why I am receiving this identifier error in the function call.

` func playerSetup() { let audioFileName = getDocumentsDirector().appendingPathComponent(fileName)

    do {
        // Whats going on here? Defining a constant does not resolve the identifier in the function call
        soundPlayer = try AVAudioPlayer(contentsOf: audioFileName)
        soundPlayer.delegate = self
        soundPlayer.prepareToPlay()
        soundPlayer.volume = 1

    } catch {
        print(error)
    }
}

`

corey6058 commented 4 years ago

when you create a property inside of a function, that property only exists within that function. To use a property in another function or elsewhere in the code, you must set that property as a global property; like you did towards the top of your code with audioPlayer AudioRecorder. Once you do that, you can just alter these properties within your function playerSetup before running soundPlayer.play() down at the bottom when the play button is pressed.

Also make sure to add your Privacy description in the info.plist for the microphone usage otherwise the app will crash immediately for trying to access the mic without user permission.

Hope this helps.