在java同级创建assets文件夹,将音频文件放在里面

/**
 * 播放本地音频
 *
 * @param path "hello.mp3" / "mp3/hello.mp3"
 */
private void startVoice(String path) {
    try {
        AssetFileDescriptor assetFileDescriptor = getAssets().openFd(path);
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
        mediaPlayer.prepare();
        mediaPlayer.start();
    } catch (Exception e) {

    }
}

标签: 安卓, 音频

添加新评论