白小花同学 2014-09-30 08:09
浏览 4062

如何播放科大讯飞录音后的pcm?

用这个方法播放的话全是噪音,因为播放要在listview的item中进行,而且要有图片的变换,所以用thread效果不好。
public void play() {

    // Get the file we want to playback./storage/emulated/0/oral/pcm/14091714280386951041300.pcm
    File file = new File("/storage/emulated/0/oral/pcm/14091714280386951041300.pcm");
    // Get the length of the audio stored in the file (16 bit so 2 bytes per short)
    // and create a short array to store the recorded audio.
    int musicLength = (int)(file.length()/2);
    short[] music = new short[musicLength];
    try {
        // Create a DataInputStream to read the audio data back from the saved file.
        InputStream is = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(is);
        DataInputStream dis = new DataInputStream(bis);

        // Read the file into the music array.
        int i = 0;
        while (dis.available() > 0) {
            music[i] = dis.readShort();
            i++;
        }

        // Close the input streams.
        dis.close();    

        // Create a new AudioTrack object using the same parameters as the AudioRecord
        // object used to create the file.
        AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                44100,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT,
                musicLength*2,
                AudioTrack.MODE_STREAM);
        // Start playback
        audioTrack.play();
        // Write the music buffer to the AudioTrack object
        audioTrack.write(music, 0, musicLength);
        audioTrack.stop() ;

    } catch (Throwable t) {
        Log.e("AudioTrack","Playback Failed");
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示