Swift requires music playback to pause and fade out (sound gradually disappears) (video) (source code)

I always feel that what I write is not technology, but feelings, and each tutorial is the trace of my journey. Success relying on professional skills is the most reproducible. I hope that my road will save you from detours. I hope that I can help you erase the dust of knowledge. I hope that I can help you sort out the context of knowledge. I hope that in the future There are you and me at the top of technology.

Swift-pause music sound attenuation source code

Effect video with clicking sound when playing music

foreword

On October 8, 2022, I just came back from the National Day broadcast and encountered a hot bug. I am currently making an app for classical music, which has relatively high requirements for the sound quality of the music. When playing music, if I suddenly click the pause button in the high-pitched part. A slight "click" sound will be heard.

You can listen to the content of the video below to see if you can hear it. The video involved in the following needs to be downloaded from the above (the video with the effect of clicking sound when playing music)
insert image description here

At that time, I was using the DOUAudioStreamer third-party SDK to play music, so I wanted to switch to the AVPlayer of the system to try this situation, and found that there was no
insert image description here

Then downloading other players in the appStore does not have this problem. At present, there are only two possibilities for this problem. One is caused by my writing code. for use,

player?.pause()
player?.start()

At most, it is caused by a timing problem, but it is unlikely. In order to eliminate the problem that is not my code, so I wrote a demo myself, start playing music with the simplest 3 lines of code, and then click the pause and start buttons. Found the same "click" sound.
insert image description here

Then there is the last question, the problem of the SDK itself, logically speaking, there is no need to change this, because the method is provided by others, I just use it, it just stopped and started like this, but as a pure classical music app, the sound quality The requirements are so high, it must sound uncomfortable, so it still needs to be solved.

The reason for this problem is that when playing to a high pitch, if you suddenly click the pause button, the sound will stop rapidly, and other players stop very softly.

So I thought, when you stop the music, first gradually lower the volume, and then gradually fade the sound, and stop it when you know you can’t hear it.

topic

GCD timing

I encapsulated a GCD timing, and every certain time is used to gradually reduce the sound.
insert image description here
Then make a classification for DOUAudioStreamer and add two methods, one is the pause method and the other is the start method, so that it will not affect the internal packaging of the third-party DOUAudioStreamer.
insert image description here
Finally, use it and call it when the button is clicked
insert image description here

Guess you like

Origin blog.csdn.net/weixin_38716347/article/details/127248334