As I indicated in my previous post that I am currently learing Android to port my audiobook reader app to android.
I managed to create initial book list screen, and now trying to learn how to play audio file and fetch metadata file.
Playing audio file with android is quite easy, Audio and Video are played using MediaPlayer class and quite easy to use.
Following is sample code to play media file in android.
In above code we are setting data source and then preparing mediaplayer then calling start to play media file. It can be paused by pause API and stopped by calling stop.
Now that I am able to play audio file, I tried to fetch its metadata file to show detais of Audio file to user. Fetching metadata was not as easy as playing file as its requied to use ContentResolver API and at first glance it looks quite strange to use it. After spending some time I managed to fetch metadata using following code.
To fetch metadata first we need to decide which attributes we are instrested in. Following are quite frequently used attributes.
I managed to create initial book list screen, and now trying to learn how to play audio file and fetch metadata file.
Playing audio file with android is quite easy, Audio and Video are played using MediaPlayer class and quite easy to use.
Following is sample code to play media file in android.
private MediaPlayer mPlayer; mPlayer = new MediaPlayer(); mPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try { String path = "/sdcard/music/sample music.mp3"; mPlayer.setDataSource(path); mPlayer.prepare(); mPlayer.start(); } catch (Exception e) { e.printStackTrace(); }
In above code we are setting data source and then preparing mediaplayer then calling start to play media file. It can be paused by pause API and stopped by calling stop.
Now that I am able to play audio file, I tried to fetch its metadata file to show detais of Audio file to user. Fetching metadata was not as easy as playing file as its requied to use ContentResolver API and at first glance it looks quite strange to use it. After spending some time I managed to fetch metadata using following code.
To fetch metadata first we need to decide which attributes we are instrested in. Following are quite frequently used attributes.
String[] TRACK_COLUMNS = new String[] { MediaStore.Audio.AudioColumns.ALBUM, MediaStore.Audio.AudioColumns.ARTIST, MediaStore.Audio.AudioColumns.DURATION, MediaStore.Audio.AudioColumns.TRACK, MediaStore.Audio.AudioColumns.YEAR, MediaStore.MediaColumns.SIZE, MediaStore.MediaColumns.TITLE, MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME };Then we need to use ContentResolver to query audio content provider to fetch metadata. I am instrested in query metadata for perticular file. So I am using MediaStore.MediaColumns.DISPLAY_NAME (filename) as selection and then specifying instrested filename in values array.
ContentResolver resolver = getBaseContext().getContentResolver(); Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; //Uri uri = Media.getContentUriForPath(path); String selection = MediaStore.MediaColumns.DISPLAY_NAME + "=?"; String[] values = new String[] { new File(path).getName() };Finally calling query API using instrested attributes and specifying required condition, Using Cursor we can retrive required metadata.
Cursor cursor = resolver.query(uri, TRACK_COLUMNS, selection, values, null); while(cursor.moveToNext()) { for( int i = 0 ; i < cursor.getColumnCount(); ++i ) { Log.d("TEST", cursor.getString(i)); } }So that's all, Hope it will be helpful.
Nice Post Sir ... Is it Possible to get the Meta Data Of Currently Playing Song in Music Player...
ReplyDeleteThanks in Advance ......
I dont know, I never tried it
ReplyDeleteHello. I have a question - how to make that query:
ReplyDeleteAll artists in selected folder like /sdcard/Music/
How to get all songs I know. But how to get all artists from that folder and then songs by selected artist from custom folder ?