英文:
Playing random sound when ImageButton is clicked on Android
问题
我是安卓新手,正在尝试在点击“ImageButton”时播放随机声音。
但每次我按按钮时仍然得到相同的音频。按钮每次被按下时应该播放不同的声音。以后我还想添加一个停止按钮。
这是我的代码:
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
MediaPlayer mp;
ImageButton soundbutton;
int[] sounds = {R.raw.audi, R.raw.berlin, R.raw.bratanki, R.raw.budzik, R.raw.cztery, R.raw.drzyz, R.raw.dziewczyny, R.raw.emeryt, R.raw.enter, R.raw.faza};
Random r = new Random();
int rndm = r.nextInt();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
soundbutton = (ImageButton)this.findViewById(R.id.playButton);
mp = MediaPlayer.create(getApplicationContext(), sounds[rndm]);
soundbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
rndm = r.nextInt(sounds.length);
mp = MediaPlayer.create(getApplicationContext(), sounds[rndm]);
}
mp.start();
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
谢谢您的帮助。
英文:
I'm new in android and I'm trying to play random sound when ImageButton
is clicked.
I'm still getting the same audio when I press the button. It should play different sounds every time the button is pressed. Also I would like to add a stop button later.
Here is my code:
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import java.util.Random;
public
class MainActivity extends AppCompatActivity {
MediaPlayer mp;
ImageButton soundbutton;
int[] sounds = {R.raw.audi, R.raw.berlin, R.raw.bratanki, R.raw.budzik, R.raw.cztery, R.raw.drzyz, R.raw.dziewczyny, R.raw.emeryt, R.raw.enter, R.raw.faza};
Random r = new Random();
int rndm = r.nextInt();
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//media player
soundbutton = (ImageButton)this.findViewById(R.id.playButton);
mp = MediaPlayer.create(getApplicationContext(), sounds[rndm]);
soundbutton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
rndm = r.nextInt();
mp = MediaPlayer.create(getApplicationContext(), sounds[rndm]);
}
mp.start();
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Thank you for your help.
答案1
得分: 1
你需要充分发挥你的随机函数。你有10个声音,所以你想要一个在0-9之间的数字。
rndm = r.nextInt(10);
这将给你一个在0-9之间的数字。
英文:
You need to give a maximum to your Random. You have 10 sounds so you want a number between 0-9.
rndm = r.nextInt(10);
This will give you a number between 0-9.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论