Check If Avaudioplayer Is Playing

Check if AVAudioPlayer is playing

The problem was because the AVAudioPlayer hasn't been initialized yet, so what i did to fix it is to initialize it and then stop it.

var soundURL: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("013", ofType: "m4a")!)!
var error:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: &error)
audioPlayer.stop()

How do I continuously check if AVAudioplayer is done playing in Swift?

You don't have to continually check - you should instead implement the AVAudioPlayerDelegate protocol in your View Controller: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerDelegateProtocolReference/index.html#//apple_ref/occ/intfm/AVAudioPlayerDelegate/audioPlayerDidFinishPlaying:successfully:

See: audioPlayerDidFinishPlaying:successfully:. I think you'll find it does exactly the opposite of what you want to do, which is actually the correct way. This way, your player will simply call the "end" function that you define.

Swift checking if AVAudioPlayer is playing crash

I figured it out, so I share with the community:

I also had to declare the sound NSURL property on my ViewController Class:

var session = AVAudioSession.sharedInstance()
var audioPlayer = AVAudioPlayer()
var audioNSURL = NSURL()

and also to prepare my audioPlayer on the ViewDidLoad function:
(I had to use a sample sound in order to initialize the AVAudioPlayer)

let samplePath = NSBundle.mainBundle().pathForResource("sample", ofType: "mp4")
audioNSURL = NSURL.fileURLWithPath(samplePath!)!
audioPlayer = AVAudioPlayer(contentsOfURL: audioNSURL, error: nil)
audioPlayer.prepareToPlay()

then, on the didSelectRowAtIndexPath method, I check whether the AVAudioPlayer instance is playing and also if it's playing the sound reflected by the cell that was tapped. If yes, stop the audioPlayer, if not then play the other sound (the audioPlayer stops and plays the other sound)

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var sound = self.sounds[indexPath.row]
var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
var pathComponents = [baseString, sound.url]

var rowSoundURL = NSURL.fileURLWithPathComponents(pathComponents)!

if audioPlayer.playing && rowSoundURL == audioNSURL {

audioPlayer.stop()

} else {

audioNSURL = rowSoundURL

self.audioPlayer = AVAudioPlayer(contentsOfURL: audioNSURL, error: nil)
self.audioPlayer.play()

}

tableView.deselectRowAtIndexPath(indexPath, animated: true)

}

Be aware, that, if you also record sounds on your app, you have to set the session category as AVAUdioSessionCategoryPlayback or the sound will be heard through the small speaker of the device. So, in viewWillAppear function:

session.setCategory(AVAudioSessionCategoryPlayback, error: nil)

How to manage the AVAudioPlayer isPlaying in each tableviewCell?

Probably first initialize to check if your player is playing

if audioPlayer != nil{
if audioPlayer?.isPlaying == true {

audioPlayer?.stop()

DispatchQueue.main.async {
self.resetTimer(second: self.elapsedTimeInSecond)
self.startTimer()
}

}
}


Related Topics



Leave a reply



Submit