代码示例
public class MainActivity extends Activity{
MediaPlayer mediaPlayer1,mediaPlayer2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer1 = MediaPlayer.create(this,R.raw.shoot.mp3);
AssetManager asset = getAssets();
try{
AssetFileDescriptor asf = asset.openFd("shot.mp3");
mediaPlayer2 = new MediaPlayer();
mediaPlayer2.setDataSource(asf.getFileDescriptor());
mediaPlayer2.prepare();
}catch (Exception e){
e.printStackTrace();
}
findViewById(R.id.btnRaw).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaPlayer1.start();
}
});
findViewById(R.id.btnPlay).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaPlayer2.start();
}
});
}
}