简体   繁体   中英

When I am trying to access the song details it is not showing duration of a song

While I am accessing the songs details it is only showing artists name and song name but not showing the duration of a song.

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());
       }
    }
  }`

I am getting the following exception

Process: com.blogspot.hongthaiit.viewpagerwithtabs, PID: 15657 android.content.res.Resources$NotFoundException: String resource ID

// 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();

Thanks,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM