Skip to content Skip to sidebar Skip to footer

Audiotrack Is Not Playing Audio File Properly As It Is Making Noise Instead

I have recorded user's voice successfully using mediaplayer and the file is stored in sd card. now i want to play that voice using audio track. But when I do it is making noise. Y

Solution 1:

Noise could be due to a small buffer.

Try:

int intSize = android.media.AudioTrack.getMinBufferSize(44100,
AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
init *= 2;

If you work with exactly the minimum buffer size, it might result in noisy sound. Having twice the minimum size is a good practice (in my experience).

Solution 2:

I have just resolve this by adding buffer size and check this code this will resolve your problem.

voidplayRecord(int position) {
    if (position==0){
        m=8000;
        Stringfolder_main="MyVoiceChanger";
        Filefilee=newFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + folder_main + "/Recording.mp3");
        intshortSizeInBytes= Short.SIZE / Byte.SIZE;
        intbufferSizeInBytes= (int) (filee.length() / shortSizeInBytes);
        short[] audioData = newshort[bufferSizeInBytes];
        try {
                InputStreaminputStream=newFileInputStream(filee);
                BufferedInputStreambufferedInputStream=newBufferedInputStream(inputStream);
                DataInputStreamdataInputStream=newDataInputStream(bufferedInputStream);
                    inti=0;
                    while (dataInputStream.available() > 0) {
                        try {
                            audioData[i] = dataInputStream.readShort();
                            i++;
                        }catch (EOFException e){
                            e.printStackTrace();
                        }
                    }
                dataInputStream.close();
                try {
                    bufferSizeInBytes = AudioTrack.getMinBufferSize(
                            m,
                            RECORDER_CHANNELS,
                            RECORDER_AUDIO_ENCODING
                    );
                    audioTrack = newAudioTrack(AudioManager.STREAM_MUSIC,44100, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                       AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes, AudioTrack.MODE_STREAM);
                    Log.i("Buffer", "playRecord: "+bufferSizeInBytes);
                    finalintfinalBufferSizeInBytes= bufferSizeInBytes;
                    mediaPlayer.setOnPreparedListener(newMediaPlayer.OnPreparedListener() {
                        @OverridepublicvoidonPrepared(MediaPlayer mp) {
                            audioTrack = newAudioTrack(3, m, 2, 2, finalBufferSizeInBytes, 1);
                            try {
                                audioTrack.play();
                                Log.i("Usman", "Audio "+audioTrack);
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
                mediaPlayer.prepareAsync();
                audioTrack.write(audioData, 0, bufferSizeInBytes);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

 }

Post a Comment for "Audiotrack Is Not Playing Audio File Properly As It Is Making Noise Instead"