Android MediaPlayer returns an error (1, -1004) when trying to play some streams

As the header says when trying to stream from some threads like this:

 http://173.192.137.34:8050

LogCat throws an error (1, -1004), which translates to:

MEDIA_ERROR_IO: Errors associated with a file or network.

At the same time, this small sample can reproduce many other streams. Is it because of the type of stream or something else?

package com.example.strm;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;

public class EntryActivity extends Activity {

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

        try {
            InitializeStream();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void InitializeStream() throws Exception{
        String url = "http://173.192.137.34:8050"; 
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(url);

        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
        mediaPlayer.prepareAsync();
    }
}
+4
source share
3 answers

replace

mediaPlayer.prepareAsync(); 

with

mediaPlayer.prepare();
-2
source

I had a similar problem. For me, this was that for some streams, basic HTTP authentication was required (streams over https). Although I used:

SharedPreferences shared_prefs = getApplicationContext().getSharedPreferences("***_prefs", 0);
final String username = shared_prefs.getString("username", null);
final String password = shared_prefs.getString("password", null);

Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password.toCharArray());
    }
});

video_view.setVideoURI(Uri.parse(enclosure_url));

setVideoURI. HTTP- :

// in this app, only https urls required authentication, your logic may differ
if (enclosure_url.contains("https")) {
    SharedPreferences shared_prefs = getApplicationContext().getSharedPreferences("***_prefs", 0);
    final String username = shared_prefs.getString("username", null);
    final String password = shared_prefs.getString("password", null);

    enclosure_url = enclosure_url.replaceFirst("https://", "https://"+username+":"+password+"@");
}
video_view.setVideoURI(Uri.parse(enclosure_url));
0

I hope this code helps you

MediaPlayer mediaPlayer = new MediaPlayer();
      player.setAudenter code hereioStreamType(AudioManager.STREAM_MUSIC);
            player.setOnCompletionListener(this);
            player.setOnErrorListener(this);
            player.setOnInfoListener(this);
            player.setOnPreparedListener(this);
            player.setOnSeekCompleteListener(this);
            player.setOnVideoSizeChangedListener(this);/*
                player.setWakeMode(getApplicationContext(),PowerManager.PARTIAL_WAKE_LOCK);*/

            try {

                player.setDataSource(this, Uri.parse(url));
                player.setOnPreparedListener(myContext);

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
0
source

Source: https://habr.com/ru/post/1546736/


All Articles