Swift - AVAudioPlayer, sound doesn't play correctly

﹥>﹥吖頭↗ 提交于 2019-11-28 12:54:33

You already provided the correct solution to your problem and that would be #2; to have a class-level audioPlayer property. Why is this so? Well, that's because in the code you setup the player, start the playback, show the pop-up and after everything is done, the method exits its scope. The automated memory management (ARC) works so that any local variables get released when you exit the scope of that variable. If you consider that sound playback is asynchronous (eg. it will play without blocking the main thread), you exit the scope before the audio player is finished playing the sound. ARC notices that audioPlayer is a local variable and releases it at that point, but the sound is not done playing yet.

I hope I was clear enough, if not feel free to ask more!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!