所以目前我已经实现了这段代码:
最终MediaPlayer mp = new MediaPlayer(); 按钮b =(按钮)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp.isPlaying()) {
mp.stop();
mp.reset();
}
try {
AssetFileDescriptor afd;
afd = getAssets().openFd("pop.mp3");
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
完全没有错误。应用程序启动并正常工作,但即使上面的方法已经实现,我也听不到任何声音。
以下是我的其余代码:
public class MyActivity2 extends Activity {
private String[] colors;
private String[] values;
private TextView tv;
private RelativeLayout rl;
Button n;
int index = 0;
int position2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_activity2);
n = (Button) findViewById(R.id.button);
tv = (TextView) findViewById(R.id.textView);
rl = (RelativeLayout) findViewById(R.id.layout_view);
Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
n.setTypeface(typeface);
Typeface face = Typeface.createFromAsset(getAssets(), "OSP-DIN.ttf");
tv.setTypeface(face);
final MediaPlayer mp = new MediaPlayer();
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp.isPlaying()) {
mp.stop();
mp.reset();
}
try {
AssetFileDescriptor afd;
afd = getAssets().openFd("Facebook Chat 'pop' Sound 3.mp3");
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
}
}
});
values = getResources().getStringArray(R.array.allthings_array);
colors = getResources().getStringArray(R.array.colorcode_array);
n.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (index == 5) {
Toast.makeText(getApplicationContext(), "More coming soon!", Toast.LENGTH_SHORT).show();
} else {
position2 = (index++);
}
String textValue = values[position2];
tv.setText(textValue);
Random RAND = new Random();
int position = RAND.nextInt(colors.length);
String nextValue = colors[position];
rl.setBackgroundColor(Color.parseColor(nextValue));
n.setBackgroundColor(Color.argb(00, 00, 00, 00));
return true;
case MotionEvent.ACTION_UP:
n.setBackgroundColor(Color.argb(00, 00, 00, 00));
return true;
default:
return false;
}
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.animation4, R.anim.animation3);
}
}
答案 0 :(得分:0)
首先,我建议您在res文件夹中创建一个名为raw的新文件夹,然后将您的音频文件放在此文件夹中。
然后在onClick方法中编写此代码源:
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// mp = new MediaPlayer();
mp = MediaPlayer.create(this, R.raw.pop);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setLooping(true);
mp.start();
}
});