'VLC encountered an error with this media Android

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.



Solution 1:[1]

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

MediaPlayer mediaPlayer;
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.m_song);
if(!mediaPlayer.isPlaying())
    mediaPlayer.start();

or you can also use VideoView that can play both audio and videos. the implementation will go like this

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle b = this.getIntent().getExtras();
    String filePath= b.getString("file_path");

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.videoview);

    video = (VideoView) findViewById(R.id.surface_view);

    video.setVideoURI(Uri.parse(filePath));

    MediaController mediaController = new MediaController(this);
    video.setMediaController(mediaController);
    video.requestFocus();
    video.start();
}



  <VideoView
        android:id="@+id/surface_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true" />

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 iBabur