2011-08-12 65 views
2

我在SD卡的Music文件夾中播放歌曲。在Android上從SD卡中挑選歌曲並播放它

而在我的應用程序中,我想從SD卡中從音樂文件列表中選擇一首歌曲,當我選擇任何歌曲時,然後使用播放按鈕它應該播放。我怎樣才能做到這一點?

我已經使用內容解析器完成了它並獲得了歌曲列表。有沒有辦法做到這一點使用意圖從存儲的歌曲中選擇SD卡

我已經通過這兩種方式做到了這一點。只需檢查它。

回答

3
@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 
    super.onOptionsItemSelected(item); 
    System.gc(); 
    String[] listtype = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Video.Media.SIZE }; 
    musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,listtype, null, null, null); 
    count = musiccursor.getCount(); 
    musiclist = (ListView) findViewById(R.id.PhoneMusicList); 
    musiclist.setAdapter(new MusicAdapter(this)); 
    musiclist.setOnItemClickListener(musicgridlistener); 
    mMediaPlayer = new MediaPlayer(); 
    return true; 
} 
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(); 
       mMediaPlayer.setOnCompletionListener(new OnCompletionListener() 
       { 
        @Override 
        public void onCompletion(MediaPlayer mp) 
        { 
         mp.release(); 
         mp = null;         
        } 
       });  
      } 
      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; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
      System.gc(); 
      TextView tv = new TextView(mContext.getApplicationContext()); 
      String id = null; 
      if (convertView == null) 
      { 
       music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME); 
       musiccursor.moveToPosition(position); 
       id = musiccursor.getString(music_column_index); 
       music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE); 
       musiccursor.moveToPosition(position); 
       id += " Size(KB):" + musiccursor.getString(music_column_index); 
       tv.setText(id); 
      } 
      else 
      tv = (TextView) convertView; 
      return tv; 
    }  
} 
} 
+0

它的作品....... – Geetanjali

6

最後我得到了我的答案。使用意圖

代碼是:

@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 
    super.onOptionsItemSelected(item); 
    System.gc(); 
     Intent intent = new Intent(); 
     intent.setAction(android.content.Intent.ACTION_VIEW); 
     Uri data = Uri.parse("file:///sdcard/Music"); 
     String type = "audio/mp3"; 
     intent.setDataAndType(data, type); 
     startActivityForResult(intent, Pick_song); 
    return true; 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 

    switch(requestCode) 
    { 
     case Pick_song : if (resultCode == RESULT_OK) 
     { 
      muri = getIntent().getData(); 
      //String ringTonePath = muri.toString(); 

      if (muri != null) 
      { 
       try 
       { 
        mMediaPlayer.start(); 
        mMediaPlayer.setOnCompletionListener(new OnCompletionListener() 
        { 
          @Override 
         public void onCompletion(MediaPlayer mp) 
          { 
           mp.release(); 
          mp = null; 
         } 
        }); 
       } 
       catch (Exception exception) 
       { 
        exception.printStackTrace(); 
       } 
      } 
     } 
+1

這裏是什麼?請定義正確的代碼.. –

+0

我們應該在哪裏將音樂文件Uri /路徑傳遞給MediaPlayer對象? 不幸的是,我無法在上面的代碼中找到它。 – NarendraJi