mediaplayer play internet mp3 :java.io.FileNotFoundException: No content provider

This is my code:

public void playSound(String uri,int flag)
    {
        Uri uri1=Uri.parse(uri);
        ContestVoteFragment fragment = (ContestVoteFragment) fragments.get(0);
        if (fragment.playSound(flag))
        {
            if (mediaPlayer==null)
            {
                mediaPlayer=new MediaPlayer();
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
                    mediaPlayer.setDataSource(uri);
                    mediaPlayer.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer.start();

            }else {
                mediaPlayer.stop();
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer=new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
                    mediaPlayer.setDataSource(uri);
                    mediaPlayer.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer.start();
            }
        }else {
            if (mediaPlayer!=null)
            {
                if (mediaPlayer.isPlaying())
                {
                    mediaPlayer.stop();
                }
            }
        }
    }
}

But when i play a internet mp3 ,the error message is Couldn't open http://p9snltngh.bkt.clouddn.com/Fs3M3Cq_Mkex9AsunJt57N22JQp2: java.io.FileNotFoundException: No content provider i can’t understand because “http://p9snltngh.bkt.clouddn.com/FhEbUwGYcmQbXIVMVzd0GyPypDVC”this uri can play

2 answers

  • answered 2018-11-08 06:46 Dinithe Pieris

    both links are working

    public void playSound(String uri){
        MediaPlayer mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(uri);
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
    

    happy coding :)

  • answered 2018-11-08 06:52 Ümañg ßürmån

    Solution: You need to parse your String URI into the URI class. So,

    Instead of:

     try {
         mediaPlayer.setDataSource(uri);
         mediaPlayer.prepare();
     } catch (IOException e) {
         e.printStackTrace();
     }
    

    Try like this:

     try {
         mediaPlayer.setDataSource(this, Uri.parse(uri));
         mediaPlayer.prepare();
     } catch (IOException e) {
         e.printStackTrace();
     }
    

    Please comment if any issues.