1、复制音频文件到raw文件夹下

Android 播放raw文件夹下音频文件

 

2、实例化音频文件

private static final float BEEP_VOLUME = 9.10f;
private MediaPlayer mediaPlayer;
private void initBeepSound() {
    if ( mediaPlayer == null) {
        // The volume on STREAM_SYSTEM is not adjustable, and users found it
        // too loud,
        // so we now play on the music stream.

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnCompletionListener(beepListener);

        AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.sou);
        try {
            mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
            file.close();
            mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
            mediaPlayer.prepare();
        } catch (IOException e) {
            mediaPlayer = null;
        }
    }
}

 

3、开始播放

mediaPlayer.start();

相关文章:

  • 2021-05-31
  • 2021-08-13
  • 2021-05-08
  • 2022-12-23
  • 2021-10-02
  • 2021-10-01
  • 2021-12-16
猜你喜欢
  • 2022-12-23
  • 2021-08-07
  • 2021-12-16
  • 2021-07-30
  • 2022-02-19
  • 2021-12-03
相关资源
相似解决方案