1

I have a volume control slider to control the volume of a audio file playback in Android.

vSlider=(SeekBar) findViewById(R.id.seekBar2);
        vSlider.setMax(10);
        if(mediaPlayer.isPlaying()){
            isPlaying=true;
            try {
                currentPosition = mediaPlayer
                        .getCurrentPosition();
                double seconds=currentPosition/1000;
                int time= (int) Math.round(seconds);
                String timeS=Integer.toString(time);
                timer.setText(timeS+"s");
                Log.d("position",timeS);
                fSlider.setProgress(currentPosition);
            }  catch (Exception e) {

            }
            vSlider.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromUser) {
                    float volume=progress/10;
                    Log.d("Progress", String.valueOf(progress));
                    Log.d("Volume",String.valueOf(volume));
                    mediaPlayer.setVolume(volume, volume);

                }
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) { }
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) { }                        
            });

The int value of the slider progress is being detected correctly, but the float volume always becomes 0.0. What am I doing wrong here?

StephenChen
  • 711
  • 2
  • 8
  • 20
  • `progress/10` is an integer division. Try `progress/10f` instead. You possibly have another similar issue if `currentPosition` is an int. – assylias Jul 30 '13 at 07:15
  • progress/10 is an integer division, which yields 0 if progress is less than 10. Afterwords it is converted to a float 0.0. You need to perform a float division: volume = ((float) progress) / 10.0; – Stefan Jul 30 '13 at 07:16

6 Answers6

4
float volume=progress/10;

Operation on two int gives result as int. So cast the result as

float volume=(float)progress/10;

or

float volume=progress/10.0f;

See casting hierarchy there are explicit and implicit cast

Shahrzad
  • 1,062
  • 8
  • 26
  • 1
    (float) is not required. But otherwise correct. Fix this and I'll +1. – Bathsheba Jul 30 '13 at 07:15
  • @aquestion That won't work , `10.0` is a `double` literal. – AllTooSir Jul 30 '13 at 07:18
  • @The New Idiot: are you sure about this? 10 is an int literal. 10.0 is a double literal which will cause progress to be promoted to a double. I agree though that 10.0f is better. – Bathsheba Jul 30 '13 at 07:18
  • @Bathsheba Sorry for the typo , i meant `10.0` will result in compilation error. – AllTooSir Jul 30 '13 at 07:20
  • @The New Idiot: but I compiled with no error, Eclipse – Shahrzad Jul 30 '13 at 07:20
  • @aquestion It should be a compilation error , unless `volume` is `double` or you use a cast `(double)` in front of the expression. – AllTooSir Jul 30 '13 at 07:21
  • @The New Idiot. Wow I didn't know that. Is that a Java 'standard'? I agree though that the answer should have 10.0f though just do stop unnecessary promotions and implicit casts. – Bathsheba Jul 30 '13 at 07:21
  • @Bathsheba A compile-time error on assignment of a double expression to a float variable is indeed required by the [Java Language Specification](http://docs.oracle.com/javase/specs/jls/se5.0/html/conversions.html#184206). – Patricia Shanahan Jul 30 '13 at 07:45
1

Use a float literal 10.0f to force a floating point arithmetic.

float volume=progress/10.0f;

By default the R.H.S of the below code is an integer airthmetic expression :

float volume=progress/10;

Since progress is an int and 10 is an int literal. Since it performs an integer division , if progress is lesser than 10 , the expression will evaluate to 0 and volume will be 0.0.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

In the line

float volume=progress/10;

first you're doing the integer division progress/10 and getting an integer result (and so if progress is anything < 10, you'll get 0), and then you're just casting that result to a float.

You want a floating-point division instead:

float volume=(float)progress/10;

There, we're first atking progress and making it a float, then doing the division and saving the result.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0
float volume=progress/10.0;

do it as above to get right answer. Int divide by int will always be a integer you are just assigining a integer 0 to float , by which result will be 0.0

ManMohan Vyas
  • 4,004
  • 4
  • 27
  • 40
0

That is because of type conversion:

When you do any int/int --Output is in-> int which is then assigned to float

So if you want output in float

float/int or int/float --Output is in--> float

Keeping that in mind:

float value = progress/10.0f //as by default 10.0 is double in java
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
0

That's because you are making an integer division and then cast the result to a float. Try this instead:

float volume = progress / 10.0f;
loscuropresagio
  • 1,922
  • 15
  • 26