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

发布于 2024-11-25 03:05:21 字数 1934 浏览 2 评论 0原文

我尝试在 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();

i try and play an mp3 file in my SDCard for my Android emulator but all that comes out is some weird buzzing noise. I made sure the sample rate is 44.1k hz i don't know what else could be wrong

    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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏蝶舞 2024-12-02 03:05:21

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

It's correct that you hear strange noise, since you need to decode the MP3 first before
you feed it to an AudioTrack! AudioTrack only plays raw PCM audio.

小草泠泠 2024-12-02 03:05:21

您应该使用 android.media.MediaPlayer 来播放 mp3 音频文件

You should use android.media.MediaPlayer to play mp3 audio file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文