可以使用Java的AudioSystem类和AudioInputStream类来实现将音频文件转为数组的功能。下面是一个示例方法的代码:

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class AudioToArrayConverter {

    public static byte[] convertToByteArray(String filePath) throws IOException, UnsupportedAudioFileException {
        File audioFile = new File(filePath);
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFile);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int bytesRead;

        while ((bytesRead = audioInputStream.read(buffer)) != -1) {
            byteArrayOutputStream.write(buffer, 0, bytesRead);
        }

        byteArrayOutputStream.flush();
        byteArrayOutputStream.close();

        return byteArrayOutputStream.toByteArray();
    }

    public static void main(String[] args) {
        String filePath = "path_to_your_wma_file.wma";

        try {
            byte[] audioBytes = convertToByteArray(filePath);
            // Do something with the audioBytes array
        } catch (IOException e) {
            e.printStackTrace();
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        }
    }
}

请替换filePath变量的值为你的WMA文件的路径。在main方法中调用convertToByteArray方法并传入文件路径,该方法将返回音频文件的字节数组。你可以根据需要对该字节数组进行进一步的操作

java写一个方法通过音频文件wma格式的路径找到文件把音频文件转为数组返回

原文地址: https://www.cveoy.top/t/topic/ipkA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录