2

I need to play .M3U urls using Media Player, but it is not working: Here is my code:

final MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource("http://xxxxxxxxx/1.m3u");
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        mediaPlayer.prepare(); // might take long! (for buffering, etc)
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            mediaPlayer.start();
        }
    });

I have read all related question in this regards on stackoverflow but it is not help me,

Also I have tried to extract the .M3U by using this method:

    public ArrayList<String> readURLs(String url) {
    if (url == null) return null;
    ArrayList<String> allURls = new ArrayList<String>();
    try {

        URL urls = new URL(url);
        BufferedReader in = new BufferedReader(new InputStreamReader(urls
                .openStream()));
        String str;
        while ((str = in.readLine()) != null) {
            allURls.add(str);

        }
        Toast.makeText(this, allURls + "", Toast.LENGTH_SHORT).show();
        Log.d("URL", String.valueOf(allURls));
        in.close();
        return allURls;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

But it was not working also when I call it

readURLs ("http://xxxxxxx.m3u");

So please help me.

Thanks in advance!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Leenah
  • 850
  • 3
  • 15
  • 36
  • What's the stacktrace? – Tiago Ornelas Jul 25 '18 at 09:11
  • @TiagoOrnelas Thank you for your care, kindly find the logcat error below: `E/MediaPlayer: error (1, -2147483648) 07-25 12:18:26.474 11277-11833/com.app.app E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present 07-25 12:18:26.513 11277-11833/com.app.app E/MediaPlayer: Should have subtitle controller already set` – Leenah Jul 25 '18 at 09:20
  • Please refer to this post: https://stackoverflow.com/questions/20087804/should-have-subtitle-controller-already-set-mediaplayer-error-android – Tiago Ornelas Jul 25 '18 at 09:36
  • Did you test .m3u link before for example: `http://109.94.2.96:8888/1.m3u`since the above post not solve my problem – Leenah Jul 25 '18 at 09:58

1 Answers1

-1

I would try to initialize the MediaPlayer differently:

MediaPlayer player = MediaPlayer.create(this, Uri.parse("http://xxxxxxxxx/1.m3u"));

Reference:

Why MediaPlayer throws NOT present error when creating instance of it?

Also don't forget to release the MediaPlayer once you are done with it.

player.release();

EDIT:

I explored the .m3u, but these contains other .m3u8playlists. I would recommend you to use Google's ExoPlayer instead which plays almost everything. https://github.com/google/ExoPlayer

gi097
  • 7,313
  • 3
  • 27
  • 49
  • Thanks for your answer, I just tried it but it throws the following error, `java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()'` Could you please test the code with actual M3U url like: `http://109.94.2.96:8888/1.m3u` – Leenah Jul 25 '18 at 09:56
  • unfortunately Google's ExoPlayer is not support .m3u as they mentioned in Github – Leenah Jul 25 '18 at 10:19
  • Exoplayer supports HLS (m3u8), in your m3u I see mostly m3u8 ones :) – gi097 Jul 25 '18 at 10:35
  • Thank you for your continues support, Can I play ExoPlayer as sound stream only? – Leenah Jul 25 '18 at 10:41
  • I believe ExoPlayer supports video as well – gi097 Jul 25 '18 at 10:42