Android AudioTrack playing .wav file, getting only white noise

前端 未结 3 1647
渐次进展
渐次进展 2020-11-30 07:08

When I play a file with the following code:

private void PlayAudioFileViaAudioTrack(int ResId) throws IOException {

    int intSize = android.media.AudioT         


        
3条回答
  •  情书的邮戳
    2020-11-30 07:52

    If you have saved file in wav format and want to play it using AudioTrack then follow this code:

                 File file=new File(Environment.getExternalStorageDirectory()+"/AudioRecorder/fahim.wav");
    
                   InputStream is;
                 DataInputStream         dis = null ;
                 BufferedInputStream     bis;
    
                try 
                {
                    is = new FileInputStream(file);
                    bis = new BufferedInputStream(is, 8000);
                    dis  = new DataInputStream(bis);      //  Create a DataInputStream to read the audio data from the saved file
    
                }
                catch (FileNotFoundException e1) 
                {
                    ShowToast("fILE NOT FOUND:"+e1.getMessage());
                }
    
                int i = 0;                                                          //  Read the file into the "music" array
                music=new byte[(int) file.length()];
                try 
                {
                    while (dis.available() > 0)
                    {
                        music[i] = dis.readByte();                                      //  This assignment does not reverse the order
                        i++;
                    }
                } 
                catch (IOException e) 
                {
                    ShowToast("I/O Exception:"+e.getMessage());
                }
    
                try {dis.close();} catch (IOException e) {e.printStackTrace();}
    
                int minBufferSize = AudioTrack.getMinBufferSize(11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
    
                AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
                at.play();
    
                ShowToast("size:"+music.length);
                //*/
                at.write(music, 0, music.length);
    

提交回复
热议问题