androidurluriandroid-mediaplayer

Play Media using URL in Android using Media


I am trying to play music from URL with Media Player but getting error in playing it

        final MediaPlayer mp = new MediaPlayer();
    try {

        mp.setDataSource(url[0]);
        mp.prepare();
        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mp.start();
            }
        });
    } catch(Exception e){
        System.out.println(e);
    }

i have also tried to use with URI

        Uri uri = Uri.parse(url[0]);
        mp.setDataSource(context,uri);
        mp.prepare();

And also tried using mp.prepareAsync(); method

But then also getting this type of error and media not playing

W/MediaPlayer: Couldn't open 
java.io.FileNotFoundException: No content provider: 
    at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1675)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1505)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1422)
    at android.media.MediaPlayer.attemptDataSource(MediaPlayer.java:1101)
    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1073)
    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1001)
    at com.tuwy.Dailog.DialogClass.showAudioDialog(DialogClass.java:272)

and getting this also

W/AudioTrack: Use of stream types is deprecated for operations other than volume control
See the documentation of AudioTrack() for what to use instead with android.media.AudioAttributes to qualify your playback use case
V/AudioTrack: pause(2131): prior state:STATE_ACTIVE
I/System.out: java.io.FileNotFoundException: : open failed: ENOENT (No such file or directory)

Solution

  • I have the same problem and this code solved my problem

    i have used another thread to play music from url...

        final Handler handler =new Handler();
    
        final Thread audioThread = new Thread(new Runnable() {
            @Override
            public void run() {
                final MediaPlayer mediaPlayer = new MediaPlayer();
    
                Uri myUri = Uri.parse(url[0]);
                try {
                    mediaPlayer.setDataSource(context, myUri);
                    mediaPlayer.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        btnPlayPause.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (mediaPlayer.isPlaying()) {
                                    mediaPlayer.pause();
                                } else {
                                    if (mediaPlayer.getCurrentPosition() > 0)
                                        mediaPlayer.seekTo(0);
                                    mediaPlayer.start();
                                }
                            }
                        });
                    }
                });
            }
        });
    

    ** url[0] means your music url and btnplaypause is the button to play and pause audio

    Hope this will solve your problem too ...