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
Swift 3/4 Dash to Camel Case (Snake to Camelcase)
What Is The Reason to Store Subscription into a Subscriptions Set
How to Query Firebase Data Childbyautoid
How to Set a Custom Annotations for All Points Except for User Location
Lazy Initialization and Deinit
Parse Codable Classes and Avoid Repetition
Reload Tableview After Clicking The Button Present Inside One of The Tableview Cell in Swift
Pass and Print All Cases in an Enum in Swift
Swift "Print" Doesn't Appear in Stdout But 3Rd Party C Library Logs Do When Running in Docker on Ecs
Swift How to Make List Start at The Top with No Padding
Crashlytics Doesn't Work with Xcode 10 Beta
How to Dynamically Hide Navigation Back Button in Swiftui
What Do Detached and Assigncurrentcontext Meaning
How to Pass a Completion Block to Another Class in Swift
Arkit/Scenekit on iOS 14 Throws New Warning (Metal)