简体   繁体   English

获取有关Android中音频文件的信息

[英]Get information about audio file in Android

I am new to Android and I want load an audio file (wav or mp3) from the file system and display audio information, such as sampling rate etc. 我是Android新手,我想从文件系统加载音频文件(wav或mp3)并显示音频信息,如采样率等。

How can I do this? 我怎样才能做到这一点? Do you know any examples? 你知道任何例子吗?

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: 您可以通过将文件大小除以音频的长度(例如,从我的库中的随机AAC编码的M4A)来近似它:

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. 由于大多数音频文件具有一组已知的有效比特率级别,因此您可以使用该比特率将比特率调整到适当的级别以进行显示。

Link to original answer by Jake Basile 链接到Jake Basile的原始答案

Or use this code to get it much more accurate: 或者使用此代码使其更加准确:

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

Link to original answer by architjn 链接到architjn的原始答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM