How to get Sampling rate and frequency of music file (MP3) in android?
Asked Answered
K

3

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?

Kalisz answered 28/2, 2011 at 9:22 Comment(2)
I think to calculate the frequency, you have to do a Fast-Fourier-Transformation (FFT).Dight
A FFT would give the frequency distribution of the actual sound, not the sample rate, no?Antisocial
A
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.

Antisocial answered 4/3, 2011 at 4:15 Comment(9)
Thanks Jake, I can get Bit-rate by using above formula. What about frequency?Kalisz
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.Antisocial
Yeah you are right. But i can get both by using native libraries in android. But i don't know how to use it.Kalisz
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. developer.android.com/sdk/ndk/overview.html#samples and it looks like theres a tutorial at mobile.tutsplus.com/tutorials/android/ndk-tutorialAntisocial
I have gone through both tutorials. But doesn't file what i am looking for.Kalisz
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.Antisocial
You are absolutely correct. But its a project requirement, so i can not ignore it :(Kalisz
You help me a lot so i am awarding you 50 bounty. Thanks for your reply.Kalisz
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.Translucent
I
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);
Impurity answered 23/12, 2015 at 11:48 Comment(1)
This should definitely be the accepted answer. :-)Byssinosis
C
4

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

Cubit answered 4/3, 2011 at 4:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.