E/MediaPlayer: Error (1,-2147483648) when using emulator

I’m making a online radio streaming app for a friend. When I run it on a physical device the stream plays no problem. But if I run it on Android Studio’s built-in emulator it gives the following error:

E/MediaPlayer: Error (1,-2147483648) 

From the research I’ve done it means just a general error? I’ve tried adding:

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        player.setAudioAttributes(new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build());
    } else {
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }

I’ve also added the right permissions in the manifest for access to internet. I’m sure this has been answered already but I can’t find any answers that fix the problem. Here is the full code I’m using to play the stream:

 private void initializeUIElements(View root) {

    playSeekBar = root.findViewById(R.id.progressBar1);
    playSeekBar.setMax(100);
    playSeekBar.setVisibility(View.INVISIBLE);

    buttonPlay =  root.findViewById(R.id.buttonPlay);
    buttonPlay.setOnClickListener(this);

    buttonStopPlay = root.findViewById(R.id.buttonStopPlay);
    buttonStopPlay.setEnabled(false);
    buttonStopPlay.setOnClickListener(this);

}
public void onClick(View v) {
    if (v == buttonPlay) {
        startPlaying();
    } else if (v == buttonStopPlay) {
        stopPlaying();
    }
}

private void startPlaying() {
    buttonStopPlay.setEnabled(true);
    buttonPlay.setEnabled(false);

    playSeekBar.setVisibility(View.VISIBLE);



    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

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

private void stopPlaying() {
    if (player.isPlaying()) {
        player.stop();
        player.release();
        initializeMediaPlayer();
    }

    buttonPlay.setEnabled(true);
    buttonStopPlay.setEnabled(false);
    playSeekBar.setVisibility(View.INVISIBLE);
}

private void initializeMediaPlayer() {
    player = new MediaPlayer();
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        player.setAudioAttributes(new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build());
    } else {
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }
    try {
        player.setDataSource("http://s5.citrus3.com:8162/stream");
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {

        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            playSeekBar.setSecondaryProgress(percent);
            Log.i("Buffering", "" + percent);
        }
    });
}

@Override
public void onPause() {
    super.onPause();
    if (player.isPlaying()) {
        player.stop();
    }
}   

Answer

i went with a different approach n used exo player. loads much faster and the errror goes away lol

private void initializeUIElements(View root) {

    playSeekBar = root.findViewById(R.id.progressBar1);
    playSeekBar.setMax(100);
    playSeekBar.setVisibility(View.INVISIBLE);





}


@Override
public void onStop() {
    super.onStop();
    if (Util.SDK_INT >= 24) {
        initializeMediaPlayer(root);
    }
}


private void initializeMediaPlayer(View root) {

    playerView = root.findViewById(R.id.video_view);
    player = new SimpleExoPlayer.Builder(getContext()).build();
    playerView.setPlayer(player);

    MediaItem media = MediaItem.fromUri(revurl);
    player.setMediaItem(media);

    player.setPlayWhenReady(playWhenReady);
    player.seekTo(currentWindow, playbackPosition);
    player.prepare();
}

@Override
public void onPause() {
    super.onPause();
    if (Util.SDK_INT < 24) {
        releasePlayer();
    }
}

@Override
public void onStart() {
    super.onStart();
    if (Util.SDK_INT >= 24) {
        initializeMediaPlayer(root);
    }
}

@Override
public void onResume() {
    super.onResume();

    if ((Util.SDK_INT < 24 || player == null)) {
        initializeMediaPlayer(root);
    }
}

@SuppressLint("InlinedApi")
private void hideUi() {
    playerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
private boolean playWhenReady = true;
private int currentWindow = 0;
private long playbackPosition = 0;


private void releasePlayer() {
    if (player != null) {
        playWhenReady = player.getPlayWhenReady();
        playbackPosition = player.getCurrentPosition();
        currentWindow = player.getCurrentWindowIndex();
        player.release();
        player = null;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *