白小花同学 2014-09-30 08:09 采纳率: 0%
浏览 4093

如何播放科大讯飞录音后的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");
    }
}
  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-02 19:23
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在Android中播放PCM声音文件,可以使用MediaRecorder和MediaPlayer类。以下是一个简单的示例:

    public class MainActivity extends AppCompatActivity {
    
        private MediaPlayer mediaPlayer;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // 创建一个媒体录制器实例
            MediaRecorder mediaRecorder = new MediaRecorder();
    
            // 设置音频格式为PCM
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    
            // 获取输出文件路径
            String filePath = getOutputFilepath();
    
            // 设置输出文件路径
            mediaRecorder.setOutputFile(filePath);
    
            // 开始录制
            mediaRecorder.prepare();
            mediaRecorder.start();
    
            // 创建一个MediaPlayer实例
            mediaPlayer = new MediaPlayer();
    
            // 将媒体录制器的结果设置到MediaPlayer中
            mediaPlayer.setDataSource(filePath);
            mediaPlayer.prepare();
        }
    
        /**
         * 获取输出文件路径的方法。
         *
         * @return 输出文件路径
         */
        private String getOutputFilepath() {
            // 指定保存文件的目录(例如:/data/data/com.example.myapp/files/audio)
            String directoryPath = "/data/data/com.example.myapp/files/audio";
            File directory = new File(directoryPath);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            return directory.getAbsolutePath() + "/" + System.currentTimeMillis() + ".amr";
        }
    
        @Override
        protected void onDestroy() {
            if (mediaPlayer != null) {
                mediaPlayer.release();
                mediaPlayer = null;
            }
            super.onDestroy();
        }
    }
    
    

    在这个例子中,我们首先创建了一个MediaRecorder对象,并设置了音频源、输出格式和编码方式。然后,我们将音频数据写入到指定的输出文件中。接着,我们创建了一个MediaPlayer对象,并将媒体录制器的结果设置到MediaPlayer中。最后,当MediaPlayer准备完成后,我们就完成了播放操作。

    评论

报告相同问题?