Audio Start Delay For The First Time - ios Swift -


i creating application 5 buttons. when click on each button each audio play. working. problem when click first button audio play 1 second delay(app stuck 1 second) , play. next time clicks button audio play without delay. issue here?

i using following code play audio

var currentaudio = try? avaudioplayer(contentsofurl: nsurl(fileurlwithpath: nsbundle.mainbundle().pathforresource("sample_audio", oftype: "mp3")!));  currentaudio!.stop() currentaudio!.currenttime = 0  currentaudio!.play(); 

please me finds issue.

you use avaudioplayer's .preparetoplay() method preload player's buffers, increase avaudioplayer's performance (faster start).

the idea prepare player time before playing it:

currentaudio?.preparetoplay() 

then later, in play function, start immediately:

currentaudio?.play() 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -