2016-03-07 56 views
-1

雖然我訪問歌曲的詳細信息,它只顯示藝術家的名字和歌曲的名稱,但沒有顯示歌曲的持續時間。當我試圖訪問歌曲的詳細信息,它不顯示歌曲的持續時間

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

+0

過程:com.blogspot.hongthaiit.vi ewpagerwithtabs,PID:15657 android.content.res.Resources $ NotFoundException:字符串資源ID#0x0 – user3770417

+0

它向我顯示這個異常 – user3770417

回答

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

謝謝,