gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:53:32 24 4
gpt4 key购买 nike

[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();

关于java - 为什么 mediaPlayer.stop();不在这里工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57213629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com