简体   繁体   English

Java Android:Mediaplayer自动消失

[英]Java Android: Mediaplayer fades automatically

I've figured out that the MediaPlayer on Android 4.4.2 seems to fade-in the Audiofile automatically. 我发现Android 4.4.2上的MediaPlayer似乎会自动淡入Audiofile。 I am using the MediaPlayer for playing a Sound that mustn't be faded in. It worked well with a Smarthpone with 4.2.1, but on another Phone with 4.4.2 the Fading occurs. 我正在使用MediaPlayer播放不能淡入的声音。它与带有4.2.1的Smarthpone一起使用时效果很好,但是在带有4.4.2的另一部手机上会出现淡入淡出。

I've also had a look at the SoundPool, but it misses the Feature of letting me know if the File is still playing. 我也看过SoundPool,但是它错过了让我知道File是否还在播放的功能。

Am I able to switch off the automatic fade-in or do I need to use the Soundpool and keep an eye on the length of the Track on my own? 我是否可以关闭自动淡入功能,还是需要使用Soundpool自己监视音轨的长度?

Thanks, VanDahken 谢谢,VanDahken

You can try to setAudioAttributes(AudioAttributes attributes) before start playing. 您可以尝试在开始播放之前设置AudioAttributes(AudioAttributes属性)

 atrs = new AudioAttributes.Builder()
             .setUsage(AudioAttributes.USAGE_MEDIA)
             .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
             .build();

Try to play with CONTENT_TYPE . 尝试玩CONTENT_TYPE

I have the same issue. 我有同样的问题。 The good thing is that it worked on a couple other Android versions, so hopefully only 4.4.2 has the problem. 好消息是它可以在其他两个Android版本上使用,因此希望只有4.4.2可以解决此问题。 My first solution I discovered was to use an uncompressed music file... (.wav with data format LEI16) but the music file was way too big. 我发现的第一个解决方案是使用未压缩的音乐文件...(数据格式为LEI16的.wav),但是音乐文件太大。

I also found that the fade-in doesn't seem to happen if you set the audio stream type to STREAM_RING but that's probably not a good idea since it's meant for phone rings. 我还发现,如果将音频流类型设置为STREAM_RING,则似乎不会发生淡入,但这可能不是一个好主意,因为它是用于电话铃响的。

For my app I only have one critical spot (when the app first starts) where the music has to play without the fade-in, and I was able to get a fix for that. 对于我的应用程序,我只有一个关键位置(应用程序首次启动时),音乐必须在播放时没有淡入淡出,而我能够解决此问题。 The intro takes a few seconds before the music starts, so what I am doing is: 在音乐开始播放前,简介需要花费几秒钟的时间,因此我正在做的是:

-prepareAsync() at the start of the intro -prepareAsync()在简介开始时

-then when prepared, setVolume() to 0f and start() -然后在准备好之后,将Volume()设置为0f并启动()

-then, after a small delay (for my case that ends up being a few seconds: the time it takes for the app's intro to complete), add a seek listener, and seekTo(0) -然后,经过一小段延迟(对于我来说,这是最后几秒钟:完成应用程序简介所需的时间),添加搜索监听器和seekTo(0)

-when the seek completes, set the volume to the desired value -搜索完成后,将音量设置为所需值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM