我的SD卡上的音乐文件夹中有歌曲。
在我的应用程序中,我想从音乐文件列表从SD卡中选择一首歌,当我选择任何一首歌时,然后使用播放按钮就可以播放。我怎么能这样做?
我已经使用内容解析器完成了它并获得了歌曲列表。有没有办法做到这一点使用意图从存储的歌曲中挑选SD卡?
我已经通过两种方式完成了这项工作。检查一下。
答案 0 :(得分: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 :(得分:3)
答案 2 :(得分: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;
}
}
}