当我尝试访问歌曲的详细信息时,它没有显示歌曲的持续时间

时间:2016-03-07 09:55:00

标签: android media

在我访问歌曲详情时,它只显示艺术家姓名和歌曲名称,但没有显示歌曲的持续时间。

public class ThirdFragment extends android.support.v4.app.Fragment {
//song list variables
private ArrayList<Song> songList;
private ListView songView;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.third_frag, container, false);

    //retrieve list view
    songView = (ListView)v.findViewById(R.id.song_list);

    //instantiate list
    songList = new ArrayList<Song>();


    getSongList();
    //sort alphabetically by title
    Collections.sort(songList, new Comparator<Song>(){
        public int compare(Song a, Song b){
            return a.getTitle().compareTo(b.getTitle());
        }
    });
    //create and set adapter
    SongAdapter songAdt = new SongAdapter(getActivity(), songList);
    songView.setAdapter(songAdt);
    return v;
}

public static ThirdFragment newInstance(String text) {

    ThirdFragment f = new ThirdFragment();


    return f;
}
//method to retrieve song info from device
public void getSongList(){
    //query external audio
    ContentResolver musicResolver =getActivity().getContentResolver();
    Uri musicUri =    android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicC`enter code here`ursor = musicResolver.query(musicUri, null,null, null, null);
    //iterate over results if valid
    if(musicCursor!=null && musicCursor.moveToFirst()){
        //get columns
        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);
        int durationColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.DURATION);

        //add songs to list
        do {
            long thisId = musicCursor.getLong(idColumn);
            String thisTitle = musicCursor.getString(titleColumn);
            String thisArtist = musicCursor.getString(artistColumn);
            String thisDuration =  musicCursor.getString(durationColumn);
            songList.add(new Song(thisId, thisTitle,     thisArtist,thisDuration));
        }
        while (musicCursor.moveToNext());
       }
    }
  }`

我收到以下异常

  

流程:com.blogspot.hongthaiit.viewpagerwithtabs,PID:15657   android.content.res.Resources $ NotFoundException:字符串资源ID

1 个答案:

答案 0 :(得分:0)

// load data file
    MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
    metaRetriever.setDataSource(filePath);
    String out = "";// get mp3 info
    String duration = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
    long dur = Long.parseLong(duration);
    String seconds = String.valueOf((dur % 60000) / 1000);
    String minutes = String.valueOf(dur / 60000);
    out = minutes + ":" + seconds;
    if (seconds.length() == 1) {
     txtTime.setText("0" + minutes + ":0" + seconds);
    }else {
     txtTime.setText("0" + minutes + ":" + seconds);
    }
    metaRetriever.release();

谢谢,