问题描述
我正在使用 androidhive 中的音频播放器教程.info 和 SongsManager.java 我试图将持续时间转换为分钟和秒.我已经设法做到了,但我不断收到一个 NumberFormatException
,我不知道如何处理.部分代码在这里:
I'm using the audio player tutorial from androidhive.info and in the SongsManager.java I'm trying to convert duration to minutes and seconds. I've managed to do it but I keep getting a NumberFormatException
which I don't know how to deal with. Part of the code is here:
public ArrayList<HashMap<String, String>> getPlayList(Context c) {
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
/*use content provider to get beginning of database query that queries for all audio by display name, path
and mimtype which i dont use but got it incase you want to scan for mp3 files only you can compare with RFC mimetype for mp3's
*/
final Cursor mCursor = c.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.MIME_TYPE,MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.ALBUM,MediaStore.Audio.Media.DURATION }, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
String songs_name = "";
String mAudioPath = "";
String songs_artist = "";
String songs_album = "";
String songs_duration = "";
/* run through all the columns we got back and save the data we need into the arraylist for our listview*/
if (mCursor.moveToFirst()) {
do {
String file_type = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE));
songs_name = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
mAudioPath = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
songs_artist = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
songs_album = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
songs_duration = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
HashMap<String, String> song = new HashMap<String, String>();
songs_duration = setCorrectDuration(songs_duration);
song.put("songTitle", songs_name);
song.put("songPath", mAudioPath);
song.put("songArtist", songs_artist);
song.put("songAlbum", songs_album);
song.put("songDuration", songs_duration);
songsList.add(song);
} while (mCursor.moveToNext());
}
mCursor.close(); //cursor has been consumed so close it
return songsList;
}
private String setCorrectDuration(String songs_duration) {
// TODO Auto-generated method stub
if(Integer.valueOf(songs_duration) != null) {
int time = Integer.valueOf(songs_duration);
int seconds = time/1000;
int minutes = seconds/60;
seconds = seconds % 60;
if(seconds<10) {
songs_duration = String.valueOf(minutes) + ":0" + String.valueOf(seconds);
} else {
songs_duration = String.valueOf(minutes) + ":" + String.valueOf(seconds);
}
return songs_duration;
}
return null;
}
这是 LogCat 的一部分:
And this is part of the LogCat:
10-28 23:39:13.047:W/dalvikvm(30524):threadid=1:线程退出未捕获异常(组=0x422f2700)10-28 23:39:13.047:E/AndroidRuntime(30524): 致命异常: main 10-28 23:39:13.047:E/AndroidRuntime(30524): java.lang.RuntimeException: 无法启动活动组件信息{com.vany.vansmusic/com.vany.vansmusic.AndroidBuildingMusicPlayerActivity}:java.lang.NumberFormatException: Invalid int: "null"
10-28 23:39:13.047:E/AndroidRuntime(30524):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)10-28 23:39:13.047:E/AndroidRuntime(30524):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)10-28 23:39:13.047:E/AndroidRuntime(30524):在android.app.ActivityThread.access$700(ActivityThread.java:165) 10-2823:39:13.047:E/AndroidRuntime(30524):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
10-28 23:39:13.047: E/AndroidRuntime(30524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305) 10-28 23:39:13.047: E/AndroidRuntime(30524): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359) 10-28 23:39:13.047: E/AndroidRuntime(30524): at android.app.ActivityThread.access$700(ActivityThread.java:165) 10-28 23:39:13.047: E/AndroidRuntime(30524): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
非常感谢您的帮助.
更新:为遇到相同或类似问题的任何人找出答案:
UPDATE:Figured it out for anyone having the same or similar problem:
if (mCursor.moveToFirst()) {
do {
String file_type = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE));
songs_name = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
mAudioPath = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
songs_artist = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
songs_album = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
songs_duration = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
HashMap<String, String> song = new HashMap<String, String>();
if(String.valueOf(songs_duration) != null) {
try {
Long time = Long.valueOf(songs_duration);
long seconds = time/1000;
long minutes = seconds/60;
seconds = seconds % 60;
if(seconds<10) {
String csongs_duration = String.valueOf(minutes) + ":0" + String.valueOf(seconds);
song.put("songDuration", csongs_duration);
} else {
String ccsongs_duration = String.valueOf(minutes) + ":" + String.valueOf(seconds);
song.put("songDuration", ccsongs_duration);
}
} catch(NumberFormatException e) {
song.put("songDuration", songs_duration);
}
} else {
String nothing = "0";
song.put("songDuration", nothing);
}
song.put("songTitle", songs_name);
song.put("songPath", mAudioPath);
song.put("songArtist", songs_artist);
song.put("songAlbum", songs_album);
songsList.add(song);
} while (mCursor.moveToNext());
}
mCursor.close(); //cursor has been consumed so close it
return songsList;
推荐答案
http://developer.android.com/reference/android/provider/MediaStore.Audio.AudioColumns.html您需要检查查询的返回类型.DURATION 字段不返回字符串,而是返回一个长字符串.
http://developer.android.com/reference/android/provider/MediaStore.Audio.AudioColumns.htmlYou need to check the return types of your queries. the DURATION field does not return a String it returns a long.
String songs_duration = "";
应该是
long songs_duration = "";
然后您就不必解析或使用空检查.除此之外,你的数学和一切都经过检查.
Then you won't have to parse or use a null check. Aside from that your math and everything checks out.
这篇关于如何将音频 Mediastore Duration 转换为分钟和秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!