'Why mediaPlayer.stop(); is not working here?
[AS] I've loaded two sounds that can play and overlap with no problem, however I can't seem to stop or pause them.
In its current state, the stop button only writes to a log, but I can't seem to get mediaplayer.stop(); or mediaplayer.pause(); to work.
To be clear, I want stopSound to end all sounds currently playing and have each sound start from the beginning when called again.
Is volumeControl creating a conflict? Am I missing something? Any help is greatly appreciated.
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>
Solution 1:[1]
Please change your code like below:-
MediaPlayer mediaPlayerOne; MediaPlayer mediaPlayerTwo;
public void playSound(View view) {
Button soundButton = (Button) view;
mediaPlayerOne.start();
mediaPlayerTwo.start();
}
You are trying to stop a media player instance which is not started yet. You are playing a media player instance which is local to that method.
In oncreate method like below:-
mediaPlayerOne = MediaPlayer.create(this, R.raw.sound_01);
mediaPlayerTwo = MediaPlayer.create(this, R.raw.sound_02);
When stop:-
mediaPlayerOne.stop();
mediaPlayerTwo.stop();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |