尝试使用此代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button id="@+id/cmd_play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play the music !!!"
/>
</LinearLayout>
MusicPlayer 活动代码
public class MusicPlayer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// Find the Button from the xml-file.
Button cmd_play = (Button)this.findViewById(R.id.cmd_play);
cmd_play.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
MediaPlayer mp = MediaPlayer.create(MusicPlayer.this,
R.raw.everlast);
mp.prepare();
mp.start();
// i.e. react on the end of the music-file:
mp.setOnCompletionListener(new OnCompletionListener(){
// @Override
public void onCompletion(MediaPlayer arg0) {
// File has ended !!!
Toast.makeText(context, "Media Completed with Success", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
将文件声音放在资产文件夹中