android MP3转wav

内容分享3周前发布
0 0 0

在Android上将MP3文件转换为WAV文件可以使用一些库来实现。一个常用的库是Lame和FFmpeg。

第一,将这些库添加到Android项目中。 在项目的build.gradle文件中添加以下依赖项:

implementation com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.14

implementation net.sourceforge.lame:lame:3.100

下面是将MP3文件转换为WAV文件的代码:

import android.media.AudioFormat;

import android.media.AudioManager;

import android.media.AudioTrack;

import android.os.Environment;

import net.sourceforge.lame.util.LameUtils;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public class MP3ToWAVConverter {

    public static void convertMP3toWAV(String mp3Path, String wavPath) {

        try {

            FileInputStream mp3InputStream = new FileInputStream(mp3Path);

            FileOutputStream wavOutputStream = new FileOutputStream(wavPath);

            byte[] mp3Buffer = new byte[1024 * 1024];

            byte[] wavBuffer = new byte[1024 * 1024];

            int bytesRead;

            while ((bytesRead = mp3InputStream.read(mp3Buffer)) > 0) {

                int bytesWritten = LameUtils.decode(mp3Buffer, bytesRead, wavBuffer);

                if (bytesWritten > 0) {

                    wavOutputStream.write(wavBuffer, 0, bytesWritten);

                }

            }

            wavOutputStream.close();

            mp3InputStream.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

将转换的MP3文件为wav:

String mp3Path = Environment.getExternalStorageDirectory().getAbsolutePath() + “/input.mp3”;

String wavPath = Environment.getExternalStorageDirectory().getAbsolutePath() + “/output.wav”;

MP3ToWAVConverter.convertMP3toWAV(mp3Path, wavPath);

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
none
暂无评论...