13

I am developing audio player in android. So i want to add the details of the playing song i.e. Artist Name, Duration, Bit rate and sampling frequency. I can get Artist Name and duration of a music file by using MediaStore.Audio.Media library. But i am unable to get Bit rate and sampling frequency of the same file. So how can i get the same?

As i know, it can be done by using native library. But don't know how? So anyone can help me on this?

Sandy
  • 6,285
  • 15
  • 65
  • 93

3 Answers3

17

You can approximate it by dividing the file size by the length of the audio in seconds, for instance, from a random AAC encoded M4A in my library:

File Size: 10.3MB (87013064 bits)
Length: 5:16 (316 Seconds)
Which gives: 87013064 bits / 316 seconds = 273426.147 bits/sec or ~273kbps
Actual Bitrate: 259kbps

Since most audio files have a known set of valid bitrate levels, you can use that to step the bit rate to the appropriate level for display.

jakebasile
  • 8,084
  • 3
  • 28
  • 34
  • Thanks Jake, I can get Bit-rate by using above formula. What about frequency? – Sandy Mar 04 '11 at 04:20
  • That's a tougher one, as sampling rate would be rather hard to derive from other information. Almost all consumer audio after CDs use 44.1KHz sampling, so it might be safe to assume that, or simply not show it because it is of little consequence to many users. – jakebasile Mar 04 '11 at 04:22
  • Yeah you are right. But i can get both by using native libraries in android. But i don't know how to use it. – Sandy Mar 04 '11 at 04:25
  • 1
    The hello-jni sample app seems to do something very similar to what you need to do, namely get a simple value from native code back to managed code. I'd check that out. http://developer.android.com/sdk/ndk/overview.html#samples and it looks like theres a tutorial at http://mobile.tutsplus.com/tutorials/android/ndk-tutorial/ – jakebasile Mar 04 '11 at 04:29
  • I have gone through both tutorials. But doesn't file what i am looking for. – Sandy Mar 04 '11 at 05:02
  • 1
    Those can show you how to communicate across the managed/native divide, but to find the actual code you need to call to get that information you'll need to search through the docs. Those aren't available online, and I don't have the NDK installed, so I can't help you there. I'd strongly suggest just ignoring the sample rate info, as almost no one cares about it, and almost all audio files are at the same rate. You'll get a pretty bad effort:reward ratio from it. – jakebasile Mar 04 '11 at 05:05
  • You are absolutely correct. But its a project requirement, so i can not ignore it :( – Sandy Mar 04 '11 at 05:14
  • You help me a lot so i am awarding you 50 bounty. Thanks for your reply. – Sandy Mar 04 '11 at 05:25
  • 4
    Sure. When recording digital audio, a pressure measurement is taken several times a second. This is a sample. For CD-quality audio, 44.1kHz is used for a sample rate. That is, 44,100 samples are taken every second. 48kHz is also often used, especially on DAT and DV tapes. The bit rate is the amount of bandwidth required for a given time period. 128kbit implies that there are close to 16kbytes of data representing 1 second of audio. You show how to get the bitrate, which has **absolutely nothing** to do with the sample rate. – Brad Nov 23 '11 at 16:57
15

I know it's been resolved but i got much better way to get accurate answer

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);
architjn
  • 1,397
  • 2
  • 13
  • 30
4

MediaMetadataRetriever offers METADATA_KEY_DURATION which you can divide with the file size for bitrate.

chx
  • 11,270
  • 7
  • 55
  • 129