I have been trying to play an mp3 audio file in the default media player. Copying the code from here I write my code like this
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog
.setCancelable(true)
.setMessage("File Path: " + path + "\n"
+ "Duration: " + duration + "\n"
+ "File Format: " + format + "\n"
+ "File Status: " + status)
.setTitle("File Information")
.setPositiveButton("Play", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = null;
uri = Uri.parse(toPlay);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(uri, "audio/mp3");
startActivity(intent);
}
})
.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
where path
and toPlay
are equal to /mnt/sdcard/MUSIC/Aey Nojwan.mp3
.
Now when I press the play button on the dialog
, VLC player open (without making a selection of player from the installed ones) and show a dialog with following error:
VLC encountered an error with this media. Please try refreshing the media library
I tried uninstalling the VLC, but after doing this the play button on my dialog
does nothing. What can be the problem.
I also encountered this issue and no luck with using this way and also some other ways that people mentioned to use ACTION_VIEW, so i have to handle myself rather to pass default player, i think VLC is not receiving the Uri path properly. You can use MediaPlayer class to directly play the audio files like below
or you can also use VideoView that can play both audio and videos. the implementation will go like this