2017-04-10 89 views
0

我想顯示音頻波形,我得到了這個代碼它需要.raw音頻輸入並顯示音頻波形,但是當我把.3gp,.mp3音頻我得到白噪聲任何人都可以幫助我怎麼能使它與.3gp一起工作,因爲我需要使用.3gp音頻來運行它。音頻波形與.raw輸入

InputStream is = getResources()。openRawResource(R.raw.test1);

TEST1是一個.raw文件

最終WaveformView mPlaybackView =(WaveformView)findViewById(R.id.playbackWaveformView);

short[] samples = null; 
    try { 
     samples = getAudioSample(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    if (samples != null) { 
     final FloatingActionButton playFab = (FloatingActionButton) findViewById(R.id.playFab); 

     mPlaybackThread = new PlaybackThread(samples, new PlaybackListener() { 
      @Override 
      public void onProgress(int progress) { 
       mPlaybackView.setMarkerPosition(progress); 
      } 
      @Override 
      public void onCompletion() { 
       mPlaybackView.setMarkerPosition(mPlaybackView.getAudioLength()); 
       playFab.setImageResource(android.R.drawable.ic_media_play); 
      } 
     }); 
     mPlaybackView.setChannels(1); 
     mPlaybackView.setSampleRate(PlaybackThread.SAMPLE_RATE); 
     mPlaybackView.setSamples(samples); 

     playFab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (!mPlaybackThread.playing()) { 
        mPlaybackThread.startPlayback(); 
        playFab.setImageResource(android.R.drawable.ic_media_pause); 
       } else { 
        mPlaybackThread.stopPlayback(); 
        playFab.setImageResource(android.R.drawable.ic_media_play); 
       } 
      } 
     }); 
    } 
} 

@Override 
protected void onStop() { 
    super.onStop(); 

    mPlaybackThread.stopPlayback(); 
} 

private short[] getAudioSample() throws IOException{ 


    InputStream is =getResources().openRawResource(R.raw.test1); 

    byte[] data; 
    try { 
     data = IOUtils.toByteArray(is); 
    } finally { 
     if (is != null) { 
      is.close(); 
     } 
    } 

    ShortBuffer sb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer(); 
    short[] samples = new short[sb.limit()]; 
    sb.get(samples); 
    return samples; 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

回答

1

你是什麼意思的白噪聲?

.3gp,.mp3是壓縮音頻,因此您需要解壓縮流然後顯示。

+0

如何解壓縮? –

+0

@AbdulSamadKhan搜索網頁爲**如何解碼MP3到PCM教程android sdk **。 PCM是數字音頻(所有音頻文件/格式必須在音箱正確播放之前解壓縮到PCM,然後您還可以讀取字節值以創建波形)。 –