在 Kotlin/Android Studio 中将 mp3 转换为 pcm 时遇到问题 - UnsupportedAudioFileException

问题描述

我尝试了以下方法:

mp3-to-wav-conversion-in-java

convert-mp3-to-pcm-in-java

当前实施:

...
var mp3URI = Uri.fromFile(file)
var mp3InputStream = this.contentResolver.openInputStream(mp3URI)
var byteArray = mp3InputStream!!.readBytes()

var pcmBytes = getAudioDataBytes(byteArray,AudioFormat(SAMPLE_RATE_HZ.toFloat(),16,1,true,false))
...

@Throws(UnsupportedAudioFileException::class,IllegalArgumentException::class,Exception::class)
fun getAudioDataBytes(sourceBytes: ByteArray?,audioFormat: AudioFormat?): ByteArray? {
    require(!(sourceBytes == null || sourceBytes.size == 0 || audioFormat == null)) { "Illegal Argument passed to this method" }
    var bais: ByteArrayInputStream? = null
    var baos: ByteArrayOutputStream? = null
    var sourceAIS: AudioInputStream? = null
    var convert1AIS: AudioInputStream? = null
    var convert2AIS: AudioInputStream? = null
    return try {
        bais = ByteArrayInputStream(sourceBytes)
        sourceAIS = AudioSystem.getAudioInputStream(bais)
        val sourceFormat: AudioFormat = sourceAIS.getFormat()
        val convertFormat = AudioFormat(
            AudioFormat.Encoding.PCM_SIGNED,sourceFormat.sampleRate,sourceFormat.channels,sourceFormat.channels * 2,false
        )
        convert1AIS = AudioSystem.getAudioInputStream(convertFormat,sourceAIS)
        convert2AIS = AudioSystem.getAudioInputStream(audioFormat,convert1AIS)
        baos = ByteArrayOutputStream()
        val buffer = ByteArray(8192)
        while (true) {
            val readCount: Int = convert2AIS.read(buffer,buffer.size)
            if (readCount == -1) {
                break
            }
            baos.write(buffer,readCount)
        }
        baos.toByteArray()
    } catch (uafe: UnsupportedAudioFileException) {
        //uafe.printStackTrace();
        throw uafe
    } catch (ioe: IOException) {
        //ioe.printStackTrace();
        throw ioe
    } catch (iae: IllegalArgumentException) {
        //iae.printStackTrace();
        throw iae
    } catch (e: Exception) {
        //e.printStackTrace();
        throw e
    } finally {
        if (baos != null) {
            try {
                baos.close()
            } catch (e: Exception) {
            }
        }
        if (convert2AIS != null) {
            try {
                convert2AIS.close()
            } catch (e: Exception) {
            }
        }
        if (convert1AIS != null) {
            try {
                convert1AIS.close()
            } catch (e: Exception) {
            }
        }
        if (sourceAIS != null) {
            try {
                sourceAIS.close()
            } catch (e: Exception) {
            }
        }
        if (bais != null) {
            try {
                bais.close()
            } catch (e: Exception) {
            }
        }
    }
}

我尝试过的方法在线上给了我 javax.sound.sampled.UnsupportedAudioFileException

sourceAIS = AudioSystem.getAudioInputStream(bais)

我认为需要将一些 .jar 文件添加到运行时类路径中。我认为将 jar 文件放在 libs 文件夹中并执行 gradle 实现是所需要的,但显然不是。 .jar 文件应该放在哪里,以便它们在运行时类路径中?或者,如果您有更简单的方法将 mp3 转换为 pcm 字节,我很乐意听到。

enter image description here

Gradle 依赖:

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation files('libs\\mp3plugin.jar')
implementation files('libs\\net.sourceforge.lame-3.98.4.jar')
implementation files('libs\\tritonus-share-0.3.7-2.jar')
implementation files('libs\\jl-1.0.1.jar')
implementation files('libs\\tritonus_mp3-0.3.6.jar')
implementation files('libs\\mp3spi-1.9.5-2.jar')
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}

解决方法

事实证明 jLayer 库有一个转换器,有些人觉得它太慢了,但对我来说效果很好

var converter = Converter()
converter.convert(mp3File,wavFile)

从那里只需要加载wav文件,删除wav头并修复edieness

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...