0

I've created a DJ program with a mediaplayer on each side of the screen(like in the picture below)

Screenshot of my app

The seekbar between those two whenever moved should increase volume of one mediaplayer and decrease the other mediaplayer's volume respectively. When I drag it very slowly it works perfectly, the problem is when I move it faster. When dragged faster the volumes doesn't always decrease to the end or doesn't decrease at all, at super speed the volume doesn't change at all. Here is the code:

    private void initiateSeekBar()
{
    try
    {
        volumeSeekbar = (SeekBar)findViewById(R.id.seekBar);
        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        //volumeSeekbar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
        //volumeSeekbar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
        final int initVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        final int maxVolume  = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,maxVolume, 0);

        volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
        {
            @Override
            public void onStopTrackingTouch(SeekBar arg0)
            {
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0)
            {
            }

            @Override
            public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
            {
                float volume = (float) (1 - (Math.log(100 - progress) / Math.log(100)));
                //audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
                if (volume <= 0.05) {
                    volume = 0;
                }
                System.out.println("\nVolume: \n" + volume);
                if (mediaplayerLeft != null) {
                    mediaplayerLeft.setVolume(1 -volume, 1 - volume);
                }
                if (mediaplayerRight != null) {
                    mediaplayerRight.setVolume(volume, volume);
                }
            }
        });
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

Why is that happening? Is that how seekbar works or is it a problem with my code?

Aleksander Lipka
  • 354
  • 1
  • 9
  • 20
  • comment out everything from `onProgressChanged` and just leave one `Log.d` call where you log the value of `progress`, test it again and watch the logcat – pskink Jan 11 '16 at 10:17
  • And then set the volume outside onProgressChanged function? – Aleksander Lipka Jan 11 '16 at 10:20
  • no, comment out just for testing – pskink Jan 11 '16 at 10:22
  • I have the exact same problem. The value of "progress" in onProgressChanged is never 0, in more than a 100 trials. It will go to 1, but never to 0 for some reason. – AndroidRocks Sep 14 '16 at 02:55
  • Actually I solved my problem by removing this line: `float volume = (float) (1 - (Math.log(100 - progress) / Math.log(100)));`. Math in this made the seekbar fail. Without any rounding and so on the seekbar works perfectly. – Aleksander Lipka Sep 25 '16 at 19:36

0 Answers0