2

我有一个应用程序具有音量偏好,在我的布局代码下方:

<VolumePreference
android:name="Volume Preference"
android:summary="Ringtone Volume"
android:title="Ringtone Volume"
android:key="ringVolPref"/>

但我不知道,如何在课堂上实现它?

4

2 回答 2

1

Try this code

XML

<SeekBar android:id="@+id/seekbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:minWidth="250px"  />

CODE

public class MainActivity extends Activity 
{

    /** Called when the activity is first created. */

    private SeekBar volumeSeekbar = null;
    private AudioManager audioManager = null; 

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        setContentView(R.layout.main);
        initControls();
    }

    private void initControls()
    {
        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));   


            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) 
                {
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                            progress, 0);
                }
            });
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
于 2012-08-04T12:57:15.457 回答
0

在您的情况下,最好的方法是添加到您的 .xml android:streamType中。这将改变所选流类型的音量。

<VolumePreference
           android:title="Alarm volume"
           android:persistent="false"
           android:streamType="alarm"/>
于 2017-03-12T10:13:38.357 回答