当前位置 : 主页 > 手机开发 > android >

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

来源:互联网 收集:自由互联 发布时间:2021-06-11
我想用AudioTrack从SD卡播放音频文件.我试过这段代码: int minBufferSize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); int bufferSize = 512; AudioTrack at = new
我想用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文件的信息.

希望这有助于你.

网友评论