为什么 mediaPlayer.stop();不在这里工作?

Why mediaPlayer.stop(); is not working here?

[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>

请像下面这样更改您的代码:-

媒体播放器mediaPlayerOne; 媒体播放器 mediaPlayerTwo;

public void playSound(View view) {

        Button soundButton = (Button) view;


        mediaPlayerOne.start();
        mediaPlayerTwo.start();
    }

您正在尝试停止尚未启动的媒体播放器实例。您正在播放该方法本地的媒体播放器实例。

在 oncreate 方法中,如下所示:-

mediaPlayerOne = MediaPlayer.create(this, R.raw.sound_01);
mediaPlayerTwo = MediaPlayer.create(this, R.raw.sound_02);

停止时:-

 mediaPlayerOne.stop();
 mediaPlayerTwo.stop();