gpt4 book ai didi

android - 在 Android 上的 Audiotrack 上播放 mp3 时发出嗡嗡声

转载 作者:太空狗 更新时间:2023-10-29 16:24:03 25 4
gpt4 key购买 nike

我尝试在我的 Android 模拟器上播放我的 SDCard 中的 mp3 文件,但输出的只是一些奇怪的嗡嗡声。我确保采样率为 44.1k hz 我不知道还有什么问题

    if(AudioTrack.MODE_STATIC != mode && AudioTrack.MODE_STREAM != mode)
throw new InvalidParameterException();

String audioFilePath = "/sdcard/test.mp3";
long fileSize = 0;
long bytesWritten = 0;
int bytesRead = 0;
int bufferSize = 0;
byte[] buffer;
AudioTrack track;

File audioFile = new File(audioFilePath);
fileSize = audioFile.length();
if(AudioTrack.MODE_STREAM == mode)
{
bufferSize = 8000;
}
else
{// AudioTrack.MODE_STATIC
bufferSize = (int)fileSize;
}
buffer = new byte[bufferSize];
track = new AudioTrack(AudioManager.STREAM_MUSIC,/* this is for pcm*/ /*22050*/ /*this is for mp3*/ 44100,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_DEFAULT/*AudioFormat.ENCODING_PCM_16BIT*/,
bufferSize, mode);
// in stream mode,
// 1. start track playback
// 2. write data to track
if(AudioTrack.MODE_STREAM == mode)
track.play();
FileInputStream audioStream = null;
try {
audioStream = new FileInputStream(audioFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

while(bytesWritten < fileSize)
{
try {
bytesRead = audioStream.read(buffer, 0, bufferSize);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bytesWritten += track.write(buffer, 0, bytesRead);
}
// in static mode,
// 1. write data to track
// 2. start track playback
if(AudioTrack.MODE_STATIC == mode)
track.play();

最佳答案

你听到奇怪的声音是正确的,因为你需要先解码 MP3您将其提供给 AudioTrack! AudioTrack 仅播放原始 PCM 音频。

关于android - 在 Android 上的 Audiotrack 上播放 mp3 时发出嗡嗡声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6743286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com