音频播放但视频未在VideoView Android中显示(低互联网中的黑屏)

时间:2017-05-16 05:59:36

标签: android video-streaming media-player android-videoview hls

我正在开发Android中的视频流。当我想在低互联网速度下播放视频视频时,VideoView显示黑屏并没有出现,但音频播放正常。我看到了这些链接link1link2。但这些联系对我没有帮助。这是我的代码快照: -

private void playvideo(String url){
    final MediaController mediacontroller;
    try {
        mediacontroller = new MediaController(MainActivity.this);
        mediacontroller.setAnchorView(videoView);
        final Uri video = Uri.parse(url);
        videoView.setMediaController(mediacontroller);
        videoView.setVideoURI(video);
        videoView.requestFocus();
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                mp.start();
                mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
                        if (extra == MediaPlayer.MEDIA_ERROR_SERVER_DIED
                                || extra == MediaPlayer.MEDIA_ERROR_MALFORMED) {
                            Log.e(TAG , "MEDIA_ERROR_SERVER_DIED && MEDIA_ERROR_MALFORMED");
                            Toast.makeText(MainActivity.this , "Server Died or Malformed" , Toast.LENGTH_LONG).show();
                            return true;
                        } else if (extra == MediaPlayer.MEDIA_ERROR_IO) {
                            Log.e(TAG , "MediaPlayer.MEDIA_ERROR_IO");
                            Toast.makeText(MainActivity.this , "Media Error IO" , Toast.LENGTH_LONG).show();
                            return true;
                        }else if (extra == MediaPlayer.MEDIA_ERROR_UNKNOWN){
                            Log.e(TAG , ".MEDIA_ERROR_UNKNOWN");
                            Toast.makeText(MainActivity.this , "MEDIA ERROR UNKNOWN" , Toast.LENGTH_LONG).show();
                            return true;
                        }
                        return false;
                    }
                });
                mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
                    @Override
                    public void onBufferingUpdate(MediaPlayer mediaPlayer, int percentage) {
                        Log.e(TAG , "onBufferingUpdate :- " + percentage);
                    }
                });
                progressBar.setVisibility(View.GONE);
                videoView.start();
            }
        });
    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        progressBar.setVisibility(View.GONE);
        Toast.makeText(getApplicationContext() ,"URL is Wrong Test with other URL" , Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

因为音频和视频都是不同的流,所以在低互联网上某种程度上它能够下载音频流而不是视频流,是吗?所以在这种情况下,可以向用户显示一条消息,例如"你有低互联网再试一次"而不是播放音频??

感谢

2 个答案:

答案 0 :(得分:0)

尝试使用Exoplayer,它是Android的MediaPlayer API的替代品,用于在本地和通过互联网播放音频和视频。

https://github.com/google/ExoPlayer

答案 1 :(得分:0)

我建议你使用SurfaceView。

surfaceView = (SurfavceView) findView.....;
holder = surfaceView.getHolder();
holder.addCallback(this); // implement SurfaceHolder.Callback methods

然后在surfaceCreated方法中:

mediaPlayer.setDisplay(holder);
mediaPlayer.setDataSource(mVideoUrl);
mediaPlayer.prepareAsync();

您也可以使用https://github.com/anthorlop/EasyExoPlayer

它使用ExoPlayer for Android SDK> = 16和MediaPlayer for Android< 16。

我希望它适合你。