如何在 Android 中以编程方式增加和减少音量
2022-09-02 00:02:09
我创建了一个音乐播放器应用程序,我想以编程方式设置音量。我想实现两个s来增加/减少音量并设置为媒体播放器。Button
活动:
control = (ImageView) findViewById(R.id.control);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
control.setOnClickListener(pausePlay);
control.setBackgroundResource(R.drawable.pause);
control id is my play and pause button :
{
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (playPause) {
control.setBackgroundResource(R.drawable.play);
if (mediaPlayer.isPlaying())
mediaPlayer.pause();
media.stop();
intialStage = false;
playPause = false;
} else {
control.setBackgroundResource(R.drawable.pause);
if (intialStage) {
new Player()
.execute("http://streaming.shoutcast.com/MUKILFMRADIO");
} else {
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
}
playPause = true;
}
}
布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/control1"
android:layout_margin="10dp"
android:layout_gravity="center"
android:background="@drawable/decrement"
android:layout_above="@+id/latestAddedSongs"
android:layout_alignEnd="@+id/musicArtistName" />
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="@+id/control"
android:layout_margin="10dp"
android:layout_gravity="center"
android:background="@drawable/play"
android:layout_above="@+id/latestAddedSongs"
android:layout_alignEnd="@+id/musicArtistName" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/control2"
android:layout_margin="10dp"
android:layout_gravity="center"
android:background="@drawable/increment"
android:layout_above="@+id/latestAddedSongs"
android:layout_alignEnd="@+id/musicArtistName" />
</LinearLayout>