簡體   English   中英

Android AudioTrack播放.wav文件,只獲得白噪聲

[英]Android AudioTrack playing .wav file, getting only white noise

當我使用以下代碼播放文件時:

private void PlayAudioFileViaAudioTrack(int ResId) throws IOException {

    int intSize = android.media.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, intSize,
            AudioTrack.MODE_STREAM);

    int count = 256 * 1024; // 256 kb
    byte[] byteData = null;
    byteData = new byte[(int) count];
    InputStream in = null;
    AssetFileDescriptor fd = null;
    fd = mResources.openRawResourceFd(ResId);
    in = mResources.openRawResource(ResId);

    int bytesRead = 0, amount = 0;
    int size = (int) fd.getLength();
    at.play();
    while (bytesRead < size) {
        amount = in.read(byteData, 0, count);
        if (amount != -1) {
            at.write(byteData, 0, amount);
        }
    }
    in.close();
    at.stop();
    at.release();

}

我聽到的唯一的東西是靜電,白噪聲。 我檢查過我的.wav文件具有相同的屬性(samplerate,bitrate)。 我對原始音頻數據(PCM)不太了解,所以我想知道是否有人能看到我的代碼有什么問題。

從您的代碼中我可以看到您只是從wav文件中讀取數據並將它們導入AudioTrack。 Wav文件有一個小標題,你可以在這里看到https://ccrma.stanford.edu/courses/422/projects/WaveFormat/所以你必須跳過標題並將你的文件描述符指向實際音頻數據的正確位置是。

此外,當您播放音頻文件並處理字節操作時,您應該處理Endianess。 看看這里在Android中使用AudioTrack播放WAV文件

在我的代碼下面(一些檢查和WAV標題跳過丟失),在Nexus One和Galaxy S中均可使用頻率為8000Hz和16位編碼的wav文件。

public void playWav(){
    int minBufferSize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
    int bufferSize = 512;
    AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
    String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();

    int i = 0;
    byte[] s = new byte[bufferSize];
    try {
        FileInputStream fin = new FileInputStream(filepath + "/REFERENCE.wav");
        DataInputStream dis = new DataInputStream(fin);

        at.play();
        while((i = dis.read(s, 0, bufferSize)) > -1){
            at.write(s, 0, i);

        }
        at.stop();
        at.release();
        dis.close();
        fin.close();

    } catch (FileNotFoundException e) {
        // TODO
        e.printStackTrace();
    } catch (IOException e) {
        // TODO
        e.printStackTrace();
    }       
}

如果您已將文件保存為wav格式並希望使用AudioTrack播放,請遵循以下代碼:

             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);

這看起來比我做的更復雜。 我用這個播放了聲音。 我認為.wav文件也可以正常工作。

MediaPlayer mpPlayProgram = new MediaPlayer();
mpPlayProgram.setDataSource("/sdcard/file.mp3");
mpPlayProgram.prepare();
mpPlayProgram.start();
mpPlayProgram.release();

對於靜態資源,它甚至更容易:

MediaPlayer mpStart = MediaPlayer.create(this, resID);
mpStart.start();
mpStart.release();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM