I want to display song title, song album in the ListView and use the song path to play the song by the use of MediaPlayer class. All the attributes has been fetch from the EXTERNAL_STORAGE by the getMusic(),whose return type is String_Array. and title, album and path of all the song has been stored in 3 array respectively.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="#FFFFFF"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview1" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
MainActivity.xml
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mMediaPlayer;
private String[] mMusicList;
String hey="2222";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMediaPlayer = new MediaPlayer();
ListView mListView = (ListView) findViewById(R.id.listview1);
mMusicList = getMusic();
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mMusicList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
try {
playSong(mMusicList[arg2]);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private String[] getMusic() {
final Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DATA ,MediaStore.Audio.Media.ALBUM}, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
String[] songs = new String[count];
String[] path=new String[count];
String[] album=new String[count];
int i = 0;
if (mCursor.moveToFirst()) {
do {
songs[i] = mCursor.getString(0);
path[i]=mCursor.getString(1);
album[i]=mCursor.getString(2);
i++;
} while (mCursor.moveToNext());
}
mCursor.close();
return path;
}
private void playSong(String path1) throws IllegalArgumentException,
IllegalStateException, IOException {
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path1);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
}
In the code you can see that how all things are going, plz help me out to display all attribute of a songs like this, current output which i am getting is this, if there is any way to display the image of song then plz tell that to.
If there is any other alternative to do this just tell me I am new to android development.
Thank You...