I made a simple app that displays the list of songs and was planning to make each list items play songs when clicked. I've set everything up however, the music wont start playing . How will I be able to play songs from the ContentResolver with MediaPlayer ? I had set up a sample code for you to see. If possible it would be a honor to have some samples or examples.
public class MainActivity extends ActionBarActivity {
List tracks;
String[] ColId;
List<Track>tack;
private MediaPlayer mMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
tracks = Track.getItems(this);
ColId = Track.COLUMNS;
ListView trackList = (ListView)findViewById(R.id.listView);
ListTrackAdapter adapter = new ListTrackAdapter(this, tracks);
trackList.setAdapter(adapter);
trackList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//long //id = 0;/* retrieve it from somewhere */;
Uri contentUri = ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, position);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(MainActivity.this, contentUri);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
String pos = String.valueOf(tracks.get(position));
Toast.makeText(MainActivity.this,position+" = "+ColId[0],Toast.LENGTH_SHORT).show();
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
super.onPause();
if(mMediaPlayer.isPlaying()) {
mMediaPlayer.release();
mMediaPlayer.stop();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mMediaPlayer.isPlaying()) {
mMediaPlayer.release();
mMediaPlayer.stop();
}
}
}
The class for the Track
public class Track {
public long id;
public long albumId;
public long artistId;
public String path;
public String title;
public String album;
public String artist;
public Uri uri; // URI
public long duration;
public int trackNo;
public static final String[] COLUMNS = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.ARTIST_ID,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.TRACK,
};
public Track(Cursor cursor)
{
id = cursor.getLong( cursor.getColumnIndex( MediaStore.Audio.Media._ID ));
path = cursor.getString( cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
title = cursor.getString( cursor.getColumnIndex( MediaStore.Audio.Media.TITLE ));
album = cursor.getString( cursor.getColumnIndex( MediaStore.Audio.Media.ALBUM ));
artist = cursor.getString( cursor.getColumnIndex( MediaStore.Audio.Media.ARTIST ));
albumId = cursor.getLong( cursor.getColumnIndex( MediaStore.Audio.Media.ALBUM_ID ));
artistId = cursor.getLong( cursor.getColumnIndex( MediaStore.Audio.Media.ARTIST_ID ));
duration = cursor.getLong( cursor.getColumnIndex( MediaStore.Audio.Media.DURATION ));
trackNo = cursor.getInt( cursor.getColumnIndex( MediaStore.Audio.Media.TRACK ));
uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
}
public static List getItems(Context activity) {
List tracks = new ArrayList();
ContentResolver resolver = activity.getContentResolver();
Cursor cursor = resolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
Track.COLUMNS,
null,
null,
null
);
while( cursor.moveToNext() ){
if( cursor.getLong(cursor.getColumnIndex( MediaStore.Audio.Media.DURATION)) < 3000 ){continue;}
tracks.add(new Track(cursor));
}
cursor.close();
return tracks;
}
}
try this,
replace with
then it works fine.