我正在开发android中的音频播放器。所以我想添加播放歌曲的细节,即艺术家姓名,持续时间,比特率和采样频率。
我可以使用MediaStore.Audio.Media library
获取音乐文件的艺术家姓名和持续时间。但我无法获得同一文件的比特率和采样频率。那么我怎么能得到相同的?
据我所知,可以使用本机库来完成。但不知道怎么办?所以任何人都可以帮助我吗?
答案 0 :(得分:16)
您可以通过将文件大小除以音频的长度(例如,从我的库中的随机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
由于大多数音频文件都有一组已知的有效比特率级别,因此可以使用该比特率将比特率调整到适当的级别进行显示。
答案 1 :(得分:6)
我知道它已经解决但我有更好的方法来获得准确的答案
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);
答案 2 :(得分:4)
MediaMetadataRetriever提供METADATA_KEY_DURATION
,您可以将文件大小除以比特率。