如何将音频Mediastore持续时间转换为分钟和秒? [英] How to convert audio Mediastore Duration to minutes and seconds?

查看:99
本文介绍了如何将音频Mediastore持续时间转换为分钟和秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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:线程退出未捕获的异常(group = 0x422f2700)10-28 23:39:13.047:E/AndroidRuntime(30524):致命异常:主要10-28 23:39:13.047:E/AndroidRuntime(30524):java.lang.RuntimeException:无法启动活动ComponentInfo {com.vany.vansmusic/com.vany.vansmusic.AndroidBuildingMusicPlayerActivity}:java.lang.NumberFormatException:无效的int:"null"

10-28 23:39:13.047: W/dalvikvm(30524): threadid=1: thread exiting with uncaught exception (group=0x422f2700) 10-28 23:39:13.047: E/AndroidRuntime(30524): FATAL EXCEPTION: main 10-28 23:39:13.047: E/AndroidRuntime(30524): java.lang.RuntimeException: Unable to start activity ComponentInfo{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)

我将非常感谢您的帮助.

I'll really appreciate the help.

更新:找出存在相同或相似问题的任何人:

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 You 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持续时间转换为分钟和秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆