gpt4 book ai didi

java - 创建新的随机音频文件时,播放随机音频onClick不会重置-Android

转载 作者:行者123 更新时间:2023-12-03 02:36:14 25 4
gpt4 key购买 nike

这里是一个android编程菜鸟的位。只是试图让我的音频停止播放,并在每次单击imageButton时播放新的随机音频。如果您能帮助指出我的问题,那就太好了。

package com.shamu11.musesoundboard;

import java.util.Random;

//import com.shamu11.firstapp.MainActivity;
//import com.shamu11.firstapp.R;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends Activity {

ImageButton buttonOne;
private final int NUM_SOUND_FILES = 3;
private int mSndFilesMatt[] = new int[NUM_SOUND_FILES];
private Random rnd = new Random();
MediaPlayer matt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mSndFilesMatt[0] = R.raw.fury;
mSndFilesMatt[1] = R.raw.knights_of_cydonia;
mSndFilesMatt[2] = R.raw.supremacy;

buttonOne = (ImageButton) findViewById(R.id.imageButton1);
buttonOne.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int sndToPlay = rnd.nextInt(NUM_SOUND_FILES);
matt = MediaPlayer.create(MainActivity.this, mSndFilesMatt[sndToPlay]);
matt.seekTo(0);
matt.start();

}
});
}
}

最佳答案

试试这个

public void onClick(View v) {
// TODO Auto-generated method stub
int sndToPlay = rnd.nextInt(NUM_SOUND_FILES);
matt.reset();
matt = MediaPlayer.create(MainActivity.this,mSndFilesMatt[sndToPlay]);
matt.seekTo(0);
matt.start();
}

关于java - 创建新的随机音频文件时,播放随机音频onClick不会重置-Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23573423/

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