简体   繁体   English

Android MediaPlayer setVolume()无法正常工作

[英]Android MediaPlayer setVolume() not working

I have a class called MusicPlayer that has a MediaPlayer and its setVolume() method simply takes a float and applies it to both left and right volume of the MediaPlayer: 我有一个名为MusicPlayer的类,该类具有MediaPlayer,它的setVolume()方法仅采用浮点数并将其应用于MediaPlayer的左右音量:

public void setVolume(float f) {
    mediaPlayer.setVolume(f, f);
}

In my MainActivity class, I create a SeekBar that calls my setVolume() method to logarithmically change the volume of the MusicPlayer: 在我的MainActivity类中,我创建了一个SeekBar,它调用我的setVolume()方法以对数形式更改MusicPlayer的音量:

SeekBar musicVolume = (SeekBar) findViewById(R.id.music_volume);
musicVolume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            float log1 = (float) (Math.log(maxVolume - progress) / Math.log(maxVolume));
            mp.setVolume(1 - log1);
        }
    }); 

The problem is that setVolume() does not seem to be doing anything. 问题是setVolume()似乎没有做任何事情。 I have already debugged to ensure my SeekBar is set up correctly and that the setVolume() method is running at all, but my problem seems to be that the MediaPlayer's setVolume() does nothing. 我已经调试过以确保我的SeekBar正确设置并且setVolume()方法完全在运行,但是我的问题似乎是MediaPlayer的setVolume()什么也不做。 Any suggestions? 有什么建议么?

EDIT: I forgot to mention that my app is planned to be something like an audio mixer, where the user can change the volume of one MediaPlayer without changing the others. 编辑:我忘了提一下,我的应用程序计划像是混音器,用户可以在其中更改一个MediaPlayer的音量而无需更改其他音量。 For example, the user wants to increase the volume of a MediaPlayer that handles sound effects while lowering the volume of a MediaPlayer that handles music, so I don't think AudioManager would be the right solution. 例如,用户想要增加处理声音效果的MediaPlayer的音量,同时降低处理音乐的MediaPlayer的音量,因此我认为AudioManager不是正确的解决方案。

This API is recommended for balancing the output of audio streams within an application. 建议使用此API来平衡应用程序中音频流的输出。 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来代替setStreamVolume(int,int,int),后者设置特定类型的所有流的量。 Note that the passed volume values are raw scalars in range 0.0 to 1.0. 请注意,传递的体积值是0.0到1.0范围内的原始标量。 UI controls should be scaled logarithmically UI控件应按对数比例缩放

http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume(int , int, int) http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume(int,int,int

U need to use audiomanger AudioManager manager = Context.getSystemService(Context.AUDIO_SERVICE) ; 您需要使用audiomanger AudioManager管理器= Context.getSystemService(Context.AUDIO_SERVICE);

manager.setStreamVolume(AudioManager.STREAM_MUSIC , int value ,MODE_CURRENT); manager.setStreamVolume(AudioManager.STREAM_MUSIC,int值,MODE_CURRENT);

  • Max value is can checked using getStreamMaxVolume() for particular stream. 可以使用getStreamMaxVolume()检查特定流的最大值。

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

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