0

I have problem with my application

  1. I have a main layout: main.xml, it includes a button named "Setting"
  2. When I click the Setting button, I use setContentView(R.layout.setting); to go to the setting.xml
  3. In setting.xml, I have a seekbar to control the volume of app

I have followed this post "Using SeekBar to Control Volume in android?"

I do like this in my activity_main, but nothing happens when i change the seekbar

Community
  • 1
  • 1
Liar
  • 1,235
  • 1
  • 9
  • 19

1 Answers1

0

Here is the code:

    //control volume using seek bar in Setting
    private AudioManager audiomanager = null;
    private SeekBar seekbar = null;

    // SoundStatus
    private TextView soundstatus = null;

    // check Sound on
    private boolean soundon = true;

    // control Background Sound
    private MediaPlayer mp = null;

    // Sound icon
    private ImageView sound = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setVolumeControlStream(AudioManager.STREAM_MUSIC);

        setContentView(R.layout.activity_main);


        // Background music when the app launch
        // music in raw folder
        mp = MediaPlayer.create(this, R.raw.backgroundsound);
        mp.start();
        mp.setLooping(true);


        // Control volume 
        ControlVolume();

    }

   ...
   ...
   ...

            public void ControlVolume()
    {

            seekbar = (SeekBar)findViewById(R.id.seekBar);
            audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            seekbar.setMax(audiomanager
                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
            seekbar.setProgress(audiomanager
                    .getStreamVolume(AudioManager.STREAM_MUSIC));   


            seekbar.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) 
                {
                    audiomanager.setStreamVolume(AudioManager.STREAM_MUSIC,
                            progress, 0);
                }
            });

    }

this is MainActivity.java main layout include button setting and the seekbar is in setting layout

Liar
  • 1,235
  • 1
  • 9
  • 19