我正在使用 ReactNative 构建媒体播放器。为了完成这样的应用程序,我必须导出我为检索音乐元数据(如专辑,艺术家等)以及文件路径而构建的模块。
上面的代码使用 jdk1.8.0_112 完美运行,但自从我更新为 jdk1.8.0_144 后它停止了工作。
在这个例子中,我没有检查not null,而不是empty,length> 0等,但我真的是在原来的那个。
try {
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource("Path to the file"); // /storage/337C-1C15/Music/Edguy/Speedhoven.mp3
} catch (RuntimeException ex) {
// java.lang.RuntimeException: setDataSource failed: status = 0xFFFFFFEA
}
我面临两个问题。一方面,我不是一个伟大的Android开发者,所以获得一些线索是一项艰巨的任务。另一方面,错误确实提供了良好的描述。
为了防止你们中的一些人有更好的方法来完成我的尝试,我在这里留下了整个代码:
@ReactMethod
public void getAll(Callback errorCallback, Callback successCallback){
ContentResolver musicResolver = this.getCurrentActivity().getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if (musicCursor != null && musicCursor.moveToFirst()) {
WritableArray jsonArray = new WritableNativeArray();
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
WritableMap items = new WritableNativeMap();
int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
try {
do {
items = new WritableNativeMap();
byte[] art;
long thisId = musicCursor.getLong(idColumn);
String thisPath = musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
String duration = musicCursor.getString(musicCursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
if(thisPath != null && thisPath != "" && thisPath.endsWith(".mp3")) {
mmr.setDataSource(thisPath);
String album = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
String artist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
String title = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
String genre = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
String encoded = "";
String encodedImage = "";
art = mmr.getEmbeddedPicture();
if (album == null) {
album = thisArtist;
}
if (artist == null) {
artist = thisArtist;
}
if (title == null) {
title = thisTitle;
}
if (art != null) {
Bitmap songImage = BitmapFactory.decodeByteArray(art, 0, art.length);
if(songImage != null){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
songImage.compress(Bitmap.CompressFormat.JPEG, 60, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
String pathtoImg = "";
byte[] imageByte = Base64.decode(encodedImage, Base64.DEFAULT);
try {
pathtoImg = Environment.getExternalStorageDirectory() + "/" + thisId + ".jpg";
File filePath = new File(pathtoImg);
FileOutputStream fos = new FileOutputStream(filePath, true);
encoded = pathtoImg;
fos.write(imageByte);
fos.flush();
fos.close();
} catch (FileNotFoundException fnfe) {
errorCallback.invoke(fnfe.getMessage());
} catch (IOException ioe) {
errorCallback.invoke(ioe.getMessage());
}
}
}
String str = String.valueOf(thisId);
items.putString("id", str);
items.putString("album", album);
items.putString("artist", artist);
items.putString("title", title);
items.putString("genre", genre);
if (encoded == "") {
items.putString("cover", "");
} else {
items.putString("cover", "file://" + encoded);
}
items.putString("duration", duration);
items.putString("path", thisPath);
jsonArray.pushMap(items);
}
} while (musicCursor.moveToNext());
successCallback.invoke(jsonArray);
mmr.release();
} catch (RuntimeException e) {
errorCallback.invoke(e.toString());
mmr.release();
} catch (Exception e) {
errorCallback.invoke(e.getMessage());
mmr.release();
}
}
}
当然,我已经看过了:
答案 0 :(得分:5)
经过调试和研究后,我发现了问题。
当文件出现问题时,mmr.setDataSource("path")
似乎会返回RuntimeException
。 这一点尤其重要,因为即使文件存在,也无法检索其元数据。
解决方案是将MediaMetadataRetriever
用于try/catch
,如下所示:
while(cursor.moveNext()){
try {
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource("Path to the file"); // /storage/337C 1C15/Music/Edguy/Speedhoven.mp3
} catch (RuntimeException ex) {
// something went wrong with the file, ignore it and continue
}
}