Android:如何为我的应用程序播放的任何音乐文件创建淡入/淡出音效?
我正在开发的应用程序播放音乐文件。如果计时器到期,我希望音乐淡出。我该怎么做呢。我正在使用 MediaPlayer 播放音乐,音乐文件位于我的应用程序的原始文件夹中。
The application that I am working on plays music files. If a timer expires I want the music to fade out. How do I do that. I am using MediaPlayer to play music and music files are present in raw folder of my application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我的 Android MediaPlayer 的整个处理程序类。查看 play() 和pause() 函数。两者都具有褪色或不褪色的能力。 updateVolume()函数是让声音线性增加/减少的关键。
This is my entire handler class for Android MediaPlayer. Look at the play() and pause() functions. Both contain the ability to either fade or not. The updateVolume() function was the key to let the sound increase/decrease linearly.
一种方法是使用 MediaPlayer.setVolume(right, left) 并让这些值在每次迭代后递减。这是一个粗略的想法
FadeIn() 或一旦您的计时器到期,就应该调用
FadeOut()
。该方法不需要采用 deltaTime,但它更好,因为它会在所有设备上以相同的速率降低音量。One way to do it is to use
MediaPlayer.setVolume(right, left)
and have these values decrement after every iteration..here is a rough ideaThe
FadeIn()
orFadeOut()
should be called once this timer of yours has expired. The method doesn't need to take the deltaTime, but it's better as it will lower the volume at the same rate across all devices.这是一个非常好的班级 sngreco。
为了使其更完整,我将添加 stop() 函数来停止播放器的淡入淡出,以及 stopAndRelease() 来停止播放器并安全地释放资源,这对于当您调用 Activity 方法(如 onStop() 或 onDestroy())时使用。
两种方法:
It is a very good class sngreco.
To make it more complete I will add stop() function to stop the player with fade, and stopAndRelease() to stop the player and release the resources securely, very useful to use when you call Activity methods like onStop() or onDestroy().
The two methods:
我一直在研究这个问题,希望它能有所帮助:D:
I have been working on this I hope it helps :D :
这是我对 Android 闹钟的淡入淡出实施的简化改编。
它不是定义步长/增量的数量,然后逐步增加音量(如该问题的其他答案),而是每 50 毫秒(可配置值)调整音量,计算出 -40dB 之间的步长/增量(接近静音)和0dB(最大;相对于流音量)基于:
请参阅下面的
computeVolume()
了解更多有趣的内容。完整的原始代码可以在这里找到: Google 来源
Here is my simplified adaptation of stock Android Alarm Clock's fade in implementation.
Rather than defining the number of steps/increments and then increasing the volume step by step (as in other answers to this question), it adjusts volume every 50ms (configurable value) working out steps/increments on the scale between -40dB (near silent) and 0dB (max; relative to the stream volume) based on:
See
computeVolume()
below for the juicy bits.Full original code can be found here: Google Source