简体   繁体   English

使用MediaPlayer API的setVolume无法正常工作

[英]setVolume Using MediaPlayer API is Not Working

I don't want to use AudioManager... Using the MediaPlayer API, my app is not able to set the volume to desired level. 我不想使用AudioManager ...使用MediaPlayer API,我的应用程序无法将音量设置为所需的水平。 As it playes on previous level of volume which is set by Volume UP and Volume Down Key. 在按上调音量和下调音量键设置的上一级音量播放时。

public class MainActivity extends AppCompatActivity {

    protected static MediaPlayer mp;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button next=(Button) findViewById(R.id.button);
        mp=MediaPlayer.create(this,R.raw.m1);
        mp.setVolume(0.02f,0.02f);
        mp.start();
        mp.setLooping(true);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        });

As mentioned in documentation: 如文档中所述:

"This API is recommended for balancing the output of audio streams within an application. Unless you are writing an application to control user settings, this API should be used in preference to setStreamVolume(int, int, int) which sets the volume of ALL streams of a particular type. " “建议使用此API来平衡应用程序中音频流的输出。除非编写应用程序来控制用户设置,否则应优先使用此API来setStreamVolume(int, int, int)来设置所有流的音量特定类型的。”

https://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float,%20float) https://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float,%20float)

You can however set a stream to your mediaplayer and let is play on the desired stream like Music / Notification or Alarm which should suffice the req. 但是,您可以为媒体播放器设置一个流,并让其在所需的流上播放,例如“音乐/通知”或“警报”,即可满足需求。

mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

Otherwise if you want to play some sound with certain level you have to use AudioManager API's to set a certain stream & set the volume of the stream and play the audio. 否则,如果您要播放某些级别的声音,则必须使用AudioManager API设置特定的流并设置流的音量并播放音频。 This is a common practice. 这是一种常见的做法。

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

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