我只有一个按钮就有一个简单的应用程序。当我按下它时,它会播放一个声音(btn001)。我想要它做的是从原始文件夹播放随机声音。我有47个声音文件名为btn001到btn047。有没有简单的方法来修改我的代码使其工作。 我知道网上有很多相同的问题。我已经尝试过我找到的所有这些。我很沮丧,但没有工作。 这是我的代码:
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button pushMe = (Button) findViewById(R.id.button1);
final MediaPlayer cheer = MediaPlayer.create(MainActivity.this, R.raw.btn001);
pushMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cheer.start();
;
};}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:1)
创建一个列表,您可以在其中添加所有声音,然后播放该列表中的随机声音。
您可以通过(int)(Math.random()*47)
答案 1 :(得分:1)
谢谢!现在代码有效了。我发布最终代码,以便其他人需要这样的东西。它只需要改变它们的APPNAME和声音文件的名称。
package com.example.APPNAME;
import java.util.Random;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sound = (Button) findViewById(R.id.button1);
sound.setOnClickListener(new View.OnClickListener() {
final int[] btns = new int[] {R.raw.btn001, R.raw.btn002, R.raw.btn003, R.raw.btn004, R.raw.btn005, R.raw.btn006,
R.raw.btn007, R.raw.btn008, R.raw.btn009, R.raw.btn010, R.raw.btn011, R.raw.btn012, R.raw.btn013,
R.raw.btn014, R.raw.btn015, R.raw.btn016, R.raw.btn017, R.raw.btn018, R.raw.btn019, R.raw.btn020,
R.raw.btn021, R.raw.btn022, R.raw.btn023, R.raw.btn024, R.raw.btn025, R.raw.btn026, R.raw.btn027,
R.raw.btn028, R.raw.btn029, R.raw.btn030, R.raw.btn031, R.raw.btn032, R.raw.btn033, R.raw.btn034,
R.raw.btn035, R.raw.btn036, R.raw.btn037, R.raw.btn038, R.raw.btn039, R.raw.btn040, R.raw.btn041,
R.raw.btn042, R.raw.btn043, R.raw.btn044, R.raw.btn045, R.raw.btn046, R.raw.btn047,
};
private Random rnd = new Random();
@Override
public void onClick(View v) {
int sndToPlay = rnd.nextInt(btns.length);
final MediaPlayer mp = MediaPlayer.create(MainActivity.this, btns[sndToPlay] );
mp.start();
;
};}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 2 :(得分:0)
一个简单的应用程序,带有一个按钮,可以在每次点击时播放随机声音(btn001 - btn047)。现在它每次点击只播放一个随机选择的声音。要播放其他声音,我必须关闭并再次启动应用程序。
import java.util.Random;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
// MediaPlayer player;
private Random rnd = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// player = new MediaPlayer;
Button sound = (Button) findViewById(R.id.button1);
sound.setOnClickListener(new View.OnClickListener() {
// Random r = new Random();
// int x = r.nextInt(1);
final int[] btns = new int[] {R.raw.btn001, R.raw.btn002, R.raw.btn003, R.raw.btn004, R.raw.btn005, R.raw.btn006,
R.raw.btn007, R.raw.btn008, R.raw.btn009, R.raw.btn010, R.raw.btn011, R.raw.btn012, R.raw.btn013,
R.raw.btn014, R.raw.btn015, R.raw.btn016, R.raw.btn017, R.raw.btn018, R.raw.btn019, R.raw.btn020,
R.raw.btn021, R.raw.btn022, R.raw.btn023, R.raw.btn024, R.raw.btn025, R.raw.btn026, R.raw.btn027,
R.raw.btn028, R.raw.btn029, R.raw.btn030, R.raw.btn031, R.raw.btn032, R.raw.btn033, R.raw.btn034,
R.raw.btn035, R.raw.btn036, R.raw.btn037, R.raw.btn038, R.raw.btn039, R.raw.btn040, R.raw.btn041,
R.raw.btn042, R.raw.btn043, R.raw.btn044, R.raw.btn045, R.raw.btn046, R.raw.btn047,
};
// new int alfa = btns;
// private Random rnd = new btns;
int sndToPlay = rnd.nextInt(btns.length);
final MediaPlayer mp = MediaPlayer.create(MainActivity.this, btns[sndToPlay] );
// mp.seekTo(0);
// mp.start();
// final MediaPlayer cheer = MediaPlayer.create(MainActivity.this, R.raw.btn001);
@Override
public void onClick(View v) {
mp.start();
;
};}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}