其他分享
首页 > 其他分享> > android – 使用AudioTrack从SD卡播放音频(mp3,ogg)

android – 使用AudioTrack从SD卡播放音频(mp3,ogg)

作者:互联网

我想用AudioTrack从SD卡播放音频文件.我试过这段代码:

int minBufferSize = AudioTrack.getMinBufferSize(8000,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT);
        int bufferSize = 512;
        AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT, minBufferSize,
                AudioTrack.MODE_STREAM);

        int i = 0;
        byte[] s = new byte[bufferSize];

        FileInputStream fin = new FileInputStream(path);
        DataInputStream dis = new DataInputStream(fin);

        at.play();
        while ((i = dis.read(s, 0, bufferSize)) > -1)
        {
            at.write(s, 0, i);

        }
        at.stop();
        at.release();
        dis.close();
        fin.close();

但它不能正常播放音频文件.它不是原始音频,而是播放某种噪音.

解决方法:

从以下链接我发现AudioTrack只播放PCM音频,
http://developer.android.com/guide/topics/media/index.html

请看一下这段代码.
http://mindtherobot.com/blog/624/android-audio-play-an-mp3-file-on-an-audiotrack/
这提供了有关如何在AudioTrack上播放MP3文件的信息.

希望这有助于你.

标签:android,audiotrack
来源: https://codeday.me/bug/20190729/1568732.html