2 votes

Erreur MediaPlayer(-38,0) dans l'état 0

Je travaille sur le streaming vidéo en direct et je n'arrive pas à diffuser la vidéo et j'obtiens l'erreur suivante

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)

mon code est le suivant

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);
  }
}

1voto

Michael Points 18419

Je soupçonne que vous recevez un appel pour onBufferingUpdate avant que la préparation asynchrone ne soit terminée. Pour autant que je sache, AwesomePlayer peut vous avertir des mises à jour de la mise en mémoire tampon avant la fin de la préparation si celle-ci est effectuée de manière asynchrone et que la mise en mémoire tampon n'a pas encore rempli le cache de la mémoire tampon ou atteint l'EOS.
Cela vous amènerait à appeler getDuration sur l'objet MediaPlayer alors qu'il est dans l'état IDLE, ce qui n'est pas autorisé (cf. https://developer.Android.com/reference/Android/media/MediaPlayer.html ).

Ma suggestion est d'avoir un indicateur booléen que vous mettez initialement à FALSE, et que vous changez en VRAI lorsque vous obtenez onPrepared . Tant que ce drapeau n'est pas VRAI, vous ne devez pas faire d'appel à l'objet MediaPlayer qui nécessite qu'il soit (au moins) en état de préparation, par ex. getDuration .

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X