问题
I have songs in the Music folder on the SD card.
And in my app I want to pick a song from SD card from the list of music files, and when I choose any song, then using the Play button it should be played. How can I do that?
I have already done it using content resolver and got a list of songs. Is there a way to do this using intents to pick from stored songs the SD card?
I have done this by both the ways. Just check it.
回答1:
Check these to get an Idea,
http://androidgenuine.com/?tag=play-sound-android
Setting Ringtone in Android
回答2:
Finally I got my answer.
Code using intents is:
@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();
}
}
}
回答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;
}
}
}
来源:https://stackoverflow.com/questions/7035999/pick-songs-from-sd-card-on-android-and-play-it