2012-09-03 63 views
1

因此,在我正在處理的應用程序中,我試圖合併一個在用戶手機上顯示所有歌曲的列表視圖。我現在已經設置好了顯示所有音樂文件(如下面的代碼所示),但是我需要一種方法來從隨機媒體文件中過濾出實際的歌曲(例如,應用程序中使用的錄製或其他音頻文件,如Facebook或谷歌+)。我已經搜索並嘗試了多種方法,但沒有一種方法證明是成功的。正如你在我的代碼中看到的那樣,我正在嘗試一些獲得歌曲持續時間的東西,並確保它至少在一分鐘左右。任何和所有的幫助將不勝感激!從其他音樂文件中過濾歌曲 - Android

感謝, 歐文

附:只要問我是否需要查看更多我的代碼,但我認爲這應該足夠了。

private void init_phone_music_grid() { 
    System.gc(); 
    String[] proj = { MediaStore.Audio.Media.DURATION, 
      MediaStore.Audio.Media.DATA, 
      MediaStore.Audio.Media.TITLE, 
      MediaStore.Video.Media.ARTIST }; 

    musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
      proj, null, null, null); 
    count = musiccursor.getCount(); 
    musiclist = (ListView) findViewById(R.id.lvTrackList); 
    musiclist.setAdapter(new MusicAdapter(getApplicationContext())); 

    musiclist.setOnItemClickListener(musicgridlistener); 
    mMediaPlayer = new MediaPlayer(); 
} 

private OnItemClickListener musicgridlistener = new OnItemClickListener() { 
    public void onItemClick(AdapterView parent, View v, int position, 
      long id) { 
     System.gc(); 
     music_column_index = musiccursor 
       .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA); 
     musiccursor.moveToPosition(position); 
     String filename = musiccursor.getString(music_column_index); 

     try { 
      if (mMediaPlayer.isPlaying()) { 
       mMediaPlayer.reset(); 
      } 
      mMediaPlayer.setDataSource(filename); 
      mMediaPlayer.prepare(); 
      mMediaPlayer.start(); 

      if(mMediaPlayer.isPlaying()) 
       chrono.start(); 

     } catch (Exception e) { 

     } 
    } 
}; 

public class MusicAdapter extends BaseAdapter { 
    private Context mContext; 

    public MusicAdapter(Context c) { 
     mContext = c; 
    } 

    public int getCount() { 
     return count; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     System.gc(); 
     TextView tv = new TextView(mContext.getApplicationContext()); 
     String id = null; 
     if (convertView == null) { 
      long size = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION); 

      if(size > 60000) { 
      music_column_index = musiccursor 
        .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE); 


      musiccursor.moveToPosition(position); 
      id = musiccursor.getString(music_column_index); 


      music_column_index = musiccursor 
        .getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION); 

      musiccursor.moveToPosition(position); 
      if(!(musiccursor.getString(music_column_index).equalsIgnoreCase("<unknown>"))) 
       id += " - " + musiccursor.getString(music_column_index); 

      tv.setText(id); 
      } 
     } else 
      tv = (TextView) convertView; 

     return tv; 

    } 
} 

回答

4

使用下面的查詢來獲取所有的媒體與持續時間超過1分鐘

musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
      proj, MediaStore.Audio.Media.DURATION + ">= 60000", null, null); 
+0

謝謝,我得到這個工作! – Owen2014

相關問題