2 years ago
#73571
Dominik Zagrušovcem
media store gets the path to a song but the media player is unable to play it(Android studio)
I am making an app in android studio that requires playing music from the phone's storage the music is within this class: public class AudioModel {
String aPath;
String aName;
String aAlbum;
String aArtist;
public String getaPath() {
return aPath;
}
public void setaPath(String aPath) {
this.aPath = aPath;
}
public String getaName() {
return aName;
}
public void setaName(String aName) {
this.aName = aName;
}
public String getaAlbum() {
return aAlbum;
}
public void setaAlbum(String aAlbum) {
this.aAlbum = aAlbum;
}
public String getaArtist() {
return aArtist;
}
public void setaArtist(String aArtist) {
this.aArtist = aArtist;
}
} ```
and I get it from this method:
public List<AudioModel> getAllAudioFromDevice(final Context context) {
final List<AudioModel> tempAudioList = new ArrayList<>();
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Audio.AudioColumns.DATA, MediaStore.Audio.AudioColumns.TITLE, MediaStore.Audio.AudioColumns.ALBUM, MediaStore.Audio.ArtistColumns.ARTIST,};
Cursor c = context.getContentResolver().query(uri,
projection,
null,
null,
null);
if (c != null) {
while (c.moveToNext()) {
AudioModel audioModel = new AudioModel();
String path = c.getString(0);
String name = c.getString(1);
String album = c.getString(2);
String artist = c.getString(3);
audioModel.setaName(name);
audioModel.setaAlbum(album);
audioModel.setaArtist(artist);
audioModel.setaPath(path);
Log.e("Name :" + name, " Album :" + album);
Log.e("Path :" + path, " Artist :" + artist);
tempAudioList.add(audioModel);
}
c.close();
}
return tempAudioList;
} ```
**//and that is used to get the path to the song like this**
``` @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView fileView = (TextView)findViewById(R.id.work);
play=(Button)findViewById(R.id.play);
Uri myUri = Uri.parse(getAllAudioFromDevice(this).get(2).aPath);
fileView.setText(getAllAudioFromDevice(this).get(2).aPath);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MediaPlayer mp=new MediaPlayer();
try{
mp.setDataSource(MainActivity.this,myUri);
mp.prepare();
mp.start();
mp.setVolume(0.5f,0.5f);
}catch(Exception e){e.printStackTrace();
Toast.makeText(getApplicationContext(), "wrong address", Toast.LENGTH_SHORT).show();}
}
});
} ``
when the program is executed gets the address of the song and displays it but when the play button is clicked it does not register as a valid path I know that the should be valid because it's displayed on the text view and matches up with the one I know exists on the phone
I have tried many things including:
converting the URI to a string,
converting the URI into a string and adding"file:///"or"file://" to the beginning,
attempting to set the media player in a separate thread,
searching and finding the address manually and inputting it into the code as a string,
I think that the problem is somewhere in the "getAllAudioFromDevice()" method
I am desperate please help!
java
android
uri
media-player
mediastore
0 Answers
Your Answer