Android的原始资源Raw和Assert资源的使用-android学习之旅(五十七)

时间:2022-11-08 04:16:51

Android的原始资源Raw和Assert资源的使用-android学习之旅(五十七)

Android的原始资源Raw和Assert资源的使用-android学习之旅(五十七)

代码示例

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();
            }
        });
    }
}