我已将所有音频文件存储在res / raw中。现在我想将它提供给以下功能
public void audioPlayer(raw raduio) {
MediaPlayer mp5 = new MediaPlayer();
try {
mp5.setDataSource(); //how to feed raduio here ?
mp5.prepare();
mp5.start();
} catch (Exception e) {
e.printStackTrace();
}
finally
{
mp5.reset();
}
}
怎么做? 如何在方法中发送R.raw.a01作为参数?
答案 0 :(得分:0)
使用以下代码[wavId - R.raw.a01]:
public void audioPlayer(raw raduio, int wavId) {
MediaPlayer mp5 = new MediaPlayer();
AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(wavId);
try {
mp5.setDataSource(afd.getFileDescriptor()));
mp5.prepare();
mp5.start();
} catch (Exception e) {
e.printStackTrace();
}
finally
{
mp5.reset();
}
}
答案 1 :(得分:0)
你可以直接使用这个
MediaPlayer mPlayer = MediaPlayer.create( MainActivity.this, R.raw.song1 );
mPlayer.setOnCompletionListener( new OnCompletionListener()
{
@Override
public void onCompletion( MediaPlayer mp )
{
mp.release();
}
} );
mPlayer.start();