Swift - AVAudioPlayer, sound doesn't play correctly

后端 未结 1 759
陌清茗
陌清茗 2020-12-11 11:29

Because UILocalNotification is not being displayed while the application is in active stat, I\'m trying to configure an UIAlertController and play

相关标签:
1条回答
  • 2020-12-11 11:58

    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!

    0 讨论(0)
提交回复
热议问题