更改android中的应用音乐量 - Eclipse

时间:2015-03-19 15:38:46

标签: android

我的申请有问题

  1. 我有一个主layout: main.xml,它包含一个名为“设置”的按钮
  2. 点击“设置”按钮后, 我使用setContentView(R.layout.setting);转到setting.xml
  3. setting.xml中,我有一个搜索栏来控制应用程序的数量
  4. 我已关注此帖“Using SeekBar to Control Volume in android?

    我在activity_main中喜欢这样,但是当我更改搜索栏时没有任何反应

1 个答案:

答案 0 :(得分:0)

以下是代码:

    //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);
                }
            });

    }

这是MainActivity.java 主要布局包括按钮设置 并且搜索栏处于设置布局