urveshpatel50
urveshpatel50

Reputation: 1685

MediaPlayer Error(-38,0) in state 0

I am working on live video streaming and i can not streaming video and get below error

12-19 10:54:38.860: ERROR/MediaPlayer(483): Attempt to call getDuration without a valid mediaplayer
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): stop called in state 0
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): stop called in state 0
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): stop called in state 0
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): stop called in state 0
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): stop called in state 0
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): stop called in state 0
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): stop called in state 0
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)

my code is below

public class VideoSample extends Activity implements OnSeekBarChangeListener, Callback, OnPreparedListener, OnCompletionListener, OnBufferingUpdateListener,
    OnClickListener, OnSeekCompleteListener, AnimationListener {
private TextView textViewPlayed;
private TextView textViewLength;
private SeekBar seekBarProgress;
private SurfaceView surfaceViewFrame;
private ImageView imageViewPauseIndicator;
private MediaPlayer player;
private SurfaceHolder holder;
private ProgressBar progressBarWait;
private Timer updateTimer;
private Bundle extras;
private Animation hideMediaController;
private LinearLayout linearLayoutMediaController;
private static final String TAG = "log_tag";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.videosample);
    extras = getIntent().getExtras();

    linearLayoutMediaController = (LinearLayout) findViewById(R.id.linearLayoutMediaController);
    linearLayoutMediaController.setVisibility(View.GONE);

    hideMediaController = AnimationUtils.loadAnimation(this, R.anim.disapearing);
    hideMediaController.setAnimationListener(this);

    imageViewPauseIndicator = (ImageView) findViewById(R.id.imageViewPauseIndicator);
    imageViewPauseIndicator.setVisibility(View.GONE);
    if (player != null) {
        if (!player.isPlaying()) {
            imageViewPauseIndicator.setVisibility(View.VISIBLE);
        }
    }

    textViewPlayed = (TextView) findViewById(R.id.textViewPlayed);
    textViewLength = (TextView) findViewById(R.id.textViewLength);

    surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);
    surfaceViewFrame.setOnClickListener(this);
    surfaceViewFrame.setClickable(false);

    seekBarProgress = (SeekBar) findViewById(R.id.seekBarProgress);
    seekBarProgress.setOnSeekBarChangeListener(this);
    seekBarProgress.setProgress(0);

    progressBarWait = (ProgressBar) findViewById(R.id.progressBarWait);

    holder = surfaceViewFrame.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    player = new MediaPlayer();
    player.setOnPreparedListener(this);
    player.setOnCompletionListener(this);
    player.setOnBufferingUpdateListener(this);
    player.setOnSeekCompleteListener(this);
    player.setScreenOnWhilePlaying(true);
    player.setDisplay(holder);
}

private void playVideo() {
    if (extras.getString("video_path").equals("VIDEO_URI")) {
        showToast("Please, set the video URI in HelloAndroidActivity.java in onClick(View v) method");
    } else {
        new Thread(new Runnable() {
            public void run() {
                try {
                    player.setDataSource(VideoSample.this, Uri.parse("rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov"));
                    player.prepareAsync();
                } catch (IllegalArgumentException e) {
                    showToast("Error while playing video");
                    Log.i(TAG, e.getMessage());
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    showToast("Error while playing video");
                    Log.i(TAG, e.getMessage());
                    e.printStackTrace();
                } catch (IOException e) {
                    showToast("Error while playing video. Please, check your network connection.");
                    Log.i(TAG, e.getMessage());
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

private void showToast(final String string) {
    runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(VideoSample.this, string, Toast.LENGTH_LONG).show();
            finish();
        }
    });
}

private void hideMediaController() {
    new Thread(new Runnable() {
        public void run() {
            try {
                Thread.sleep(5000);
                runOnUiThread(new Runnable() {
                    public void run() {
                        linearLayoutMediaController.startAnimation(hideMediaController);
                    }
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    Log.i(TAG, "========== onProgressChanged : " + progress + " from user: " + fromUser);
    if (!fromUser) {
        textViewPlayed.setText(Utils.durationInSecondsToString(progress));
    }
}

public void onStartTrackingTouch(SeekBar seekBar) {
}

public void onStopTrackingTouch(SeekBar seekBar) {
    if (player.isPlaying()) {
        progressBarWait.setVisibility(View.VISIBLE);
        player.seekTo(seekBar.getProgress() * 1000);
        Log.i(TAG, "========== SeekTo : " + seekBar.getProgress());
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

public void surfaceCreated(SurfaceHolder holder) {
    playVideo();
}

public void surfaceDestroyed(SurfaceHolder holder) {

}

public void onPrepared(MediaPlayer mp) {
    if (!player.isPlaying()) {
        player.start();
        updateMediaProgress();
        linearLayoutMediaController.setVisibility(View.VISIBLE);
        hideMediaController();
    }
    Log.i(TAG, "========== onPrepared ===========");
    int duration = mp.getDuration() / 1000; // duration in seconds
    seekBarProgress.setMax(duration);
    textViewLength.setText(Utils.durationInSecondsToString(duration));
    progressBarWait.setVisibility(View.GONE);

    // Get the dimensions of the video
    int videoWidth = player.getVideoWidth();
    int videoHeight = player.getVideoHeight();
    float videoProportion = (float) videoWidth / (float) videoHeight;
    Log.i(TAG, "VIDEO SIZES: W: " + videoWidth + " H: " + videoHeight + " PROP: " + videoProportion);

    // Get the width of the screen
    int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
    int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
    float screenProportion = (float) screenWidth / (float) screenHeight;
    Log.i(TAG, "VIDEO SIZES: W: " + screenWidth + " H: " + screenHeight + " PROP: " + screenProportion);

    // Get the SurfaceView layout parameters
    android.view.ViewGroup.LayoutParams lp = surfaceViewFrame.getLayoutParams();

    if (videoProportion > screenProportion) {
        lp.width = screenWidth;
        lp.height = (int) ((float) screenWidth / videoProportion);
    } else {
        lp.width = (int) (videoProportion * (float) screenHeight);
        lp.height = screenHeight;
    }

    // Commit the layout parameters
    surfaceViewFrame.setLayoutParams(lp);

    // Start video
    surfaceViewFrame.setClickable(true);
}

public void onCompletion(MediaPlayer mp) {
    mp.stop();
    if (updateTimer != null) {
        updateTimer.cancel();
    }
    //finish();
}

/**
 * Change progress of mediaController
 * */
private void updateMediaProgress() {
    updateTimer = new Timer("progress Updater");
    updateTimer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                public void run() {
                    seekBarProgress.setProgress(player.getCurrentPosition() / 1000);
                }
            });
        }
    }, 0, 1000);
}

public void onBufferingUpdate(MediaPlayer mp, int percent) {
    int progress = (int) ((float) mp.getDuration() * ((float) percent / (float) 100));
    seekBarProgress.setSecondaryProgress(progress / 1000);
}

public void onClick(View v) {
    if (v.getId() == R.id.surfaceViewFrame) {
        if (linearLayoutMediaController.getVisibility() == View.GONE) {
            linearLayoutMediaController.setVisibility(View.VISIBLE);
            hideMediaController();
        } else if (player != null) {
            if (player.isPlaying()) {
                player.pause();
                imageViewPauseIndicator.setVisibility(View.VISIBLE);
            } else {
                player.start();
                imageViewPauseIndicator.setVisibility(View.GONE);
            }
        }
    }
}

public void onSeekComplete(MediaPlayer mp) {
    progressBarWait.setVisibility(View.GONE);
}

public void onAnimationEnd(Animation animation) {

}

public void onAnimationRepeat(Animation animation) {

}

public void onAnimationStart(Animation animation) {
    linearLayoutMediaController.setVisibility(View.GONE);
  }
}

Upvotes: 2

Views: 3673

Answers (1)

Michael
Michael

Reputation: 58517

I suspect that you're getting a call to onBufferingUpdate before the asynchronous prepare has finished. As far as I know, AwesomePlayer can notify you of buffering updates before prepare has finished if prepare is done asynchronously and the buffering hasn't yet filled up the buffer cache or reached the EOS.
This would cause you to call getDuration on the MediaPlayer object while it's in IDLE state, which isn't allowed (see https://developer.android.com/reference/android/media/MediaPlayer.html).

My suggestion is to have a boolean flag that you initially set to FALSE, and change to TRUE when you get onPrepared. Until this flag is TRUE you should not make any calls to the MediaPlayer object that requires it to be in (at least) prepared state, e.g. getDuration.

Upvotes: 1

Related Questions