我注意到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)
總是retuns空。
,我認爲除了使用外部庫將查詢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版本是< 10或文件的擴展名是mp3和SDK版本是15,我查詢MediaStore,否則我使用MediaMetaDataRetriever 。你
也可以嘗試這個
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());
}
謝謝你的回答!實際上,Android版本是4.4.2,所以它不應該是一個API版本問題,但我將在將文件轉換爲另一種格式(我只需要它用於我的原始文件夾中的文件)後再試一次。我會報告,如果它稍後工作。 – Zazama
@Zazama,我真的很想知道它是否會爲你工作。 – MCHAppy
我意識到,將文件轉換爲另一種格式,它會刪除我的ID3標籤:/所以我看着你的另一個解決方案,但沒有真正得到這麼遠(我是一個在Android開發初學者)。 「mmr.setDataSource(audio.url,new HashMap());」。我沒有得到這個工作。 Android工作室說「audio.url」必須是一個字符串,我試圖讓它與uri.toString()一起工作,但它不起作用。你有一個想法,我怎麼能得到這個與我從上面的uri一起工作? –
Zazama