1..播放res/raw
//方法一:根据音频文件的ID来创建MediaPlayer MediaPlayer mp = new MediaPlayer(this,R.raw.test); mp.start(); //方法二: MediaPlayer mp = new MediaPlayer(); AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.test); try { mp.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); mp.prepare(); file.close(); } catch (IOException e) { e.printStackTrace(); } mp.setVolume(0.5f, 0.5f); mp.setLooping(true); mp.start();
2.播放res/assets
AssetManager am = getAssets(); AssetFileDescriptor afd = am.openFd("bomb.mp3"); MediaPlayer mp = new MediaPlayer(); mp.setDataSource(file.getFileDescriptor()); mp.prepare();