【问题标题】:AudioTrack is not playing audio file properly as it is making noise insteadAudioTrack 没有正确播放音频文件,因为它会产生噪音
【发布时间】:2014-10-24 13:35:05
【问题描述】:

我已经使用媒体播放器成功录制了用户的声音,并且文件存储在 sd 卡中。 现在我想用音轨播放那个声音。但是当我这样做时,它会发出噪音。 是吗?

这是播放声音的代码..

private void PlayAudioFileViaAudioTrack(String filePath) throws IOException
    {
    // We keep temporarily filePath globally as we have only two sample sounds now..
    if (filePath==null)
    return;

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

    AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
    AudioFormat.ENCODING_PCM_16BIT, intSize, AudioTrack.MODE_STREAM); 


    if (at==null){ 
    Log.d("TCAudio", "audio track is not initialised ");
    return; 
    }

    int count = 512 * 1024; // 512 kb
    //Reading the file..
    byte[] byteData = null; 
    File file = null; 
    file = new File(filePath);

    byteData = new byte[(int)count];
    FileInputStream in = null;
    try {
    in = new FileInputStream( file );

    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    int bytesread = 0, ret = 0;
    int size = (int) file.length();
    at.play();
    while (bytesread < size) { ret = in.read( byteData,0, count); if (ret != -1) { // Write the byte array to the track 

        at.write(byteData,0, ret); bytesread += ret; } else break; } in.close(); at.stop(); at.release(); }

【问题讨论】:

  • 定义“制造噪音...”
  • 噪音你可以说它造成了很大的声音失真,它没有清晰地播放声音。

标签: android audio audiotrack


【解决方案1】:

噪音可能是由于缓冲区过小造成的。

试试:

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

如果您使用最小的缓冲区大小,它可能会导致嘈杂的声音。将最小尺寸设为两倍是一种很好的做法(根据我的经验)。

【讨论】:

    【解决方案2】:

    我刚刚通过添加缓冲区大小来解决此问题,并检查此代码这将解决您的问题。

         void playRecord(int position) {
        if (position==0){
            m=8000;
            String folder_main = "MyVoiceChanger";
            File filee = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + folder_main + "/Recording.mp3");
            int shortSizeInBytes = Short.SIZE / Byte.SIZE;
            int bufferSizeInBytes = (int) (filee.length() / shortSizeInBytes);
            short[] audioData = new short[bufferSizeInBytes];
            try {
                    InputStream inputStream = new FileInputStream(filee);
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                    DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);
                        int i = 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 = new AudioTrack(AudioManager.STREAM_MUSIC,44100, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                           AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes, AudioTrack.MODE_STREAM);
                        Log.i("Buffer", "playRecord: "+bufferSizeInBytes);
                        final int finalBufferSizeInBytes = bufferSizeInBytes;
                        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer mp) {
                                audioTrack = new AudioTrack(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();
                }
        }
    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      相关资源
      最近更新 更多