MediaStore & ContentProviders in Android
useful videos & blogs :
https://youtu.be/U5yDjBUSAic
ContentProvider & ContentResolver :
https://youtu.be/-4GgzqMVrYc
https://youtu.be/eNW1d8tiXmQ
For API 23+ you need to request the read/write permissions even if they are already in your manifest.
while using mediaplayer to play audio, before we change the datasource using .setDataSource , we need to always reset the cursor.
//IMP TO RESET THE CURSOR , ELSE WONT PLAY ANOTHER AUDIOS.
mediaPlayer.reset();
----------------------------------------------------------------------------------------------------
GET ALL THE AUDIO FILES ON THE EXTERNAL STORAGE
useful blog -https://medium.com/@sriramaripirala/android-10-open-failed-eacces-permission-denied-da8b630a89df
Add this if you are getting ERROR : open failed: EACCES (Permission denied)
<application android:requestLegacyExternalStorage="true" ... >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
Get permissions on the runTime.
// asks a permission to the user.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
String[] permissions ={Manifest.permission.READ_EXTERNAL_STORAGE};
ActivityCompat.requestPermissions(this,permissions,111);
}else{
loadSongs();
}
}
// Called when a request permission is denied or accepted
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==111 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
loadSongs();
}
}
Song.java (Modal class )
package com.deepesh.mediaapp;
public class Song{
String title;
String author;
String song_url;
public Song(String title, String author, String song_url) {
this.title = title;
this.author = author;
this.song_url = song_url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getSong_url() {
return song_url;
}
public void setSong_url(String song_url) {
this.song_url = song_url;
}
}
Loads all the Songs into a arraylist to display on a recyclerview.
// Gets all the audio files on the external storage & fills them inside the array
@RequiresApi(api = Build.VERSION_CODES.O)
private void loadSongs() {
//songsList = new ArrayList<>();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC+"!=0";
Cursor cursor = getApplicationContext().getContentResolver().query(uri,null,selection,null,null);
if (cursor!=null){
while (cursor.moveToNext()){
String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
Song temp_song = new Song(title,artist,url);
songsList.add(temp_song);
}
}
assert cursor != null;
// Dont Forget to close the cursor.
cursor.close();
}
-----------------------------------------------------------------------------------------

Comments
Post a Comment