[AS]我已经加载了两个可以毫无问题地播放和重叠的声音,但是我似乎无法停止或暂停它们。
在当前状态下,停止按钮仅写入日志,但我似乎无法获取 mediaplayer.stop(); 或 mediaplayer.pause(); 去工作。
需要明确的是,我希望 stopSound 结束当前播放的所有声音,并在再次调用时让每个声音从头开始。
volumeControl 是否会产生冲突?我错过了什么吗?任何帮助是极大的赞赏。
MainActivity.java
public class MainActivity extends AppCompatActivity {
public MediaPlayer mediaPlayer;
AudioManager audioManager;
public void playSound(View view) {
Button soundButton = (Button) view;
Log.i("INFO", soundButton.getTag().toString() + " button pressed.");
MediaPlayer mediaPlayer = MediaPlayer.create(this, getResources().getIdentifier(soundButton.getTag().toString(), "raw", getPackageName()));
mediaPlayer.start();
}
public void stopSound(View view) {
Log.i("INFO", "stop button pressed.");
mediaPlayer.stop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mediaPlayer = MediaPlayer.create(this, R.raw.sound_01);
mediaPlayer = MediaPlayer.create(this, R.raw.sound_02);
SeekBar volumeControl = (SeekBar) findViewById(R.id.seekBar);
volumeControl.setMax(maxVolume);
volumeControl.setProgress(currentVolume);
volumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Log.i("Seekbar changed", Integer.toString(i));
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, i, 0);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
tools:context="com.example.company.app.MainActivity">
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="128dp"
android:layout_marginTop="64dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:columnCount="2"
android:paddingBottom="4dp"
android:paddingTop="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/buttonL1"
android:layout_columnWeight="1"
android:layout_gravity="center_vertical"
android:layout_marginLeft="6dp"
android:layout_marginRight="1dp"
android:layout_rowWeight="1"
android:onClick="playSound"
android:tag="sound_01"
android:text="Sound One" />
<Button
android:id="@+id/buttonR1"
android:layout_columnWeight="1"
android:layout_gravity="center_vertical"
android:layout_marginLeft="1dp"
android:layout_marginRight="6dp"
android:layout_rowWeight="1"
android:onClick="playSound"
android:tag="sound_02"
android:text="Sound Two" />
</GridLayout>
</ScrollView>
<SeekBar
android:id="@+id/seekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/scrollView" />
<Button
android:id="@+id/stopButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:onClick="stopSound"
android:text="stop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/seekBar" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>