我创建了音乐播放器应用,我想以编程方式设置volume
向上/向下。
我想为另外两个Button
实现增加/减少音量并设置为媒体播放器。
请帮助我在 android 中访问音量向上/向下。
活动代码:
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>
以编程方式在android
中提供关于静音/取消静音和音量向上/向下的链接或教程。
答案 0 :(得分:19)
为音频管理器创建一个对象
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
Button upButton = (Button) findViewById(R.id.upButton);
upButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//To increase media player volume
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
}
});
Button downButton = (Button) findViewById(R.id.downButton);
downButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//To decrease media player volume
audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
}
});
以上示例使用了Button标签
用于音量增大和减小 代码
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
答案 1 :(得分:7)
尝试以下代码
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
seekbar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int newVolume, boolean b) {
textview.setText("Media Volume : " + newVolume);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newVolume, 0);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
答案 2 :(得分:1)
跟进jesu的解决方案,如果你想在Service而不是Activity中更改音量,请替换:
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
使用:
AudioManager audioManager;
audioManager = (AudioManager) YourServiceName.this.getSystemService(Context.AUDIO_SERVICE);
答案 3 :(得分:0)
以下代码效果很好
Android中有多个流可以用来管理音频
/** The audio stream for phone calls */
public static final int STREAM_VOICE_CALL = AudioSystem.STREAM_VOICE_CALL;
/** The audio stream for system sounds */
public static final int STREAM_SYSTEM = AudioSystem.STREAM_SYSTEM;
/** The audio stream for the phone ring */
public static final int STREAM_RING = AudioSystem.STREAM_RING;
/** The audio stream for music playback */
public static final int STREAM_MUSIC = AudioSystem.STREAM_MUSIC;
/** The audio stream for alarms */
public static final int STREAM_ALARM = AudioSystem.STREAM_ALARM;
/** The audio stream for notifications */
public static final int STREAM_NOTIFICATION = AudioSystem.STREAM_NOTIFICATION;
/** @hide The audio stream for phone calls when connected to bluetooth */
public static final int STREAM_BLUETOOTH_SCO = AudioSystem.STREAM_BLUETOOTH_SCO;
/** @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */
public static final int STREAM_SYSTEM_ENFORCED = AudioSystem.STREAM_SYSTEM_ENFORCED;
/** The audio stream for DTMF Tones */
public static final int STREAM_DTMF = AudioSystem.STREAM_DTMF;
/** @hide The audio stream for text to speech (TTS) */
public static final int STREAM_TTS = AudioSystem.STREAM_TTS;
根据您的需要,您可以在此处更改流,这是STREAM_MUSIC示例:
您可以使用AudioSytem类中的反射获得恒定值:
private int getStreamType(String streamName) {
final String streamSourceClassName = "android.media.AudioSystem";
int streamType = 0;
try {
streamType = (int) Class.forName(streamSourceClassName).getDeclaredField(streamName).get(null);
} catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}
return streamType;
}
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
seekbar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int newVolume, boolean b) {
textview.setText("Media Volume : " + newVolume);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newVolume, 0);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
答案 4 :(得分:0)
如果您的类型是AudioManager.STREAM_MUSIC:使用此代码,请按电话音量按钮
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
audio.adjustStreamVolume(
AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
audio.adjustStreamVolume(
AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
return true;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
答案 5 :(得分:0)
当按下buttonSES时,此代码将立即将音量设置为100,而不会反馈给用户。
public void perform_actionSES(View v) {
Button buttonSES = (Button) findViewById(R.id.buttonSES);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);
}
答案 6 :(得分:0)
各位程序员。我已经尽力使代码尽可能简单。我希望这对某人有帮助。祝你有美好的一天!
我正在使用: 安卓工作室 4.2.2 Build #AI-202.7660.26.42.7486908,建于2021年6月24日 运行时版本:11.0.8+10-b944.6842174 amd64 VM:不适用的 OpenJDK 64 位服务器 VM 视窗 10 10.0
对于 MainActivity.java,这里是代码。
package com.tutorial.android.mediaplayertest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private AudioManager audioManager;
private Button play, pause, decVolume, incVolume;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// I want my media player instantiated as soon as the app starts
mediaPlayer = MediaPlayer.create(this, R.raw.test);
// play button
play = findViewById(R.id.play_button);
play.setOnClickListener(view -> {
Toast.makeText(this, "Music Start", Toast.LENGTH_SHORT).show();
mediaPlayer.start();
});
// pause button
pause = findViewById(R.id.pause_button);
pause.setOnClickListener(view -> {
Toast.makeText(this, "Pause", Toast.LENGTH_SHORT).show();
mediaPlayer.pause();
});
audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
// volume down button
decVolume = findViewById(R.id.volume_down);
decVolume.setOnClickListener(view -> {
Toast.makeText(this, "Volume down", Toast.LENGTH_SHORT).show();
audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
});
// volume up button
incVolume = findViewById(R.id.volume_up);
incVolume.setOnClickListener(view -> {
Toast.makeText(this, "Volume up", Toast.LENGTH_SHORT).show();
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
});
}
}
如果是activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play" />
<Button
android:id="@+id/pause_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/volume_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<Button
android:layout_marginLeft="16dp"
android:id="@+id/volume_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
</LinearLayout>
答案 7 :(得分:-1)
s