我在 ICS 上注意到了同样的问题(在 Galaxy SII 和 Galaxy Tab II 上都运行 ICS 4.0.3)。这似乎只影响 mp3。
我想其中一种解决方案是使用外部库,但我也更喜欢使用 android 提供的而不是外部库。
我尝试了两种解决方案:
MediaMetadataRetriever mmdr = new MediaMetadataRetriever();
mmdr.setDataSource(path);
String title = mmdr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
和
File file = new File(path);
FileInputStream inputStream;
inputStream = new FileInputStream(file);
mmdr = new MediaMetadataRetriever();
mmdr.setDataSource(inputStream.getFD());
inputStream.close();
String title = mmdr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
但问题依然存在:
MediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE) 总是返回 null。
除了使用外部库之外,我想到的一个解决方案是在文件路径上查询 MediaStore:
Cursor c = mContext.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] {MediaStore.MediaColumns.TITLE},
MediaStore.MediaColumns.DATA + "=?",
new String[] { path }, null);
String title;
if(c!=null && c.moveToFirst())
title = c.getString(c.getColumnIndex(MediaStore.MediaColumns.TITLE))
}
如果 MediaScanner 扫描了它(它应该有),信息应该在那里。这也应该适用于 10 之前的 API 级别。
基本上是这样:如果SDK版本
你也可以试试这个
if (Build.VERSION.SDK_INT >= 14) {
mmr = new MediaMetadataRetriever();
mmr.setDataSource(audio.url, new HashMap<String, String>());
} else {
mmr = new MediaMetadataRetriever();
mmr.setDataSource(audio.url);
}
这是我迄今为止尝试解决的问题。但我不能保证它是否适合你。
希望我能帮上忙。
编辑:您需要将路径或文件描述符作为参数传递给 setDataSource。试试这个代码:
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.music);
if (afd != null) {
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
metaRetriever.setDataSource(afd.getFileDescriptor());
}