8

I need your help for the below query:

Query: Is there any way of getting following info of an audio file. Sample rate, Channel, Bitrate of an audio file.

For extracting bitrate, "MediaMetadataRetriever" API is available (METADATA_KEY_BITRATE).

Please suggest if it can be done using any android API.

Found this below API, But its use is actually in different. http://developer.android.com/reference/android/medi/AudioTrack.html

I want to extract these using Android API programmactically : Sampling rate, Quantization, Channel of an input audio file.

Please help on this.

Thanks in advance.

user1850671
  • 91
  • 1
  • 1
  • 4
  • Seems helpful: http://stackoverflow.com/questions/5140085/how-to-get-sampling-rate-and-frequency-of-music-file-mp3-in-android – dasar Sep 11 '14 at 09:38

2 Answers2

4

This can be done using MeiaExtractor like this:

MediaExtractor mex = new MediaExtractor();
    try {
        mex.setDataSource(path);// the adresss location of the sound on sdcard.
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    MediaFormat mf = mex.getTrackFormat(0);

    int bitRate = mf.getInteger(MediaFormat.KEY_BIT_RATE);
    int sampleRate = mf.getInteger(MediaFormat.KEY_SAMPLE_RATE);
    int channelCount = mf.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
kc ochibili
  • 3,103
  • 2
  • 25
  • 25
  • 2
    this line mex.setDataSource(path); getting error like ""Failed to open libwvm.so: dlopen failed: library "libwvm.so" not found"" – Bhanu Sharma Oct 13 '15 at 05:28
  • Try testing it on a physical device, the problem you're encountering seems to be common with emulators – kc ochibili Oct 13 '15 at 15:06
  • Don't working to file wav – Đốc.tc Dec 08 '22 at 02:06
  • @Mr.Lemon are you sure? if so I recommend creating a separate question on StackOverflow, saying that MediaExtractor is not working for .wav files. be sure to also post any associated errors messages you may have from trying to run the code. – kc ochibili Dec 09 '22 at 01:19
  • @kc ochbili I tried with both MediaMetadataRetriever and MediaExtractor options and I found MediaMetadataRetriever works for .wav files and MediaExtractor doesn't – Đốc.tc Dec 09 '22 at 03:41
  • oh, okay thats good to hear. Pleas update my answer above adding how you did it with MediaMetadataRetriever. – kc ochibili Dec 09 '22 at 04:05
2

Use MediaPlayer.getTrackInfo() during playback (after METADATE_UPDATE event come to onInfo callback) to obtain MediaFormat object by invoke getFormat for audio stream track. And then from MediaFormat you can get:

BIT_RATE

CHANNEL_COUNT

SAMPLE_RATE

dasar
  • 5,321
  • 4
  • 24
  • 36