Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

android - 如何删除播放视频之前出现的黑屏?(How to remove black screen that comes before the video is played?)

I am trying to play mp4 videos from SD card storage in a custom view that I have made.

(我正在尝试以自定义视图播放SD卡存储中的mp4视频。)

All is working fine.

(一切正常。)

However, just before the video is played, a black screen appears for 0.1 to 0.2 seconds.

(但是,就在播放视频之前,黑屏会出现0.1到0.2秒。)

I want to remove this black screen.

(我要删除此黑屏。)

Below is the code for the custom view.

(以下是自定义视图的代码。)

public class CustomVideoView extends TextureView implements TextureView.SurfaceTextureListener,
        MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {

    public interface OnMediaCompletionListener {
        void onCompletion(MediaPlayer mediaPlayer);
    }

    OnMediaCompletionListener onMediaCompletionListener = null;

    MediaPlayer mediaPlayer =  new MediaPlayer();

    public CustomVideoView(Context context) {
        super(context);
    }

    public CustomVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setOnMediaCompletionListener(OnMediaCompletionListener listener){
        this.onMediaCompletionListener = listener;
    }

    public void playMedia(String path){
        playMedia(Uri.parse(path));
    }

    public void playMedia(Uri uri) {

        try {
            Surface s = new Surface(this.getSurfaceTexture());
            mediaPlayer.setDataSource(getContext(), uri);
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnCompletionListener(this);
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.setOnErrorListener(this);
            mediaPlayer.setSurface(s);
            mediaPlayer.prepareAsync();
        } catch (Exception e){
            e.printStackTrace();
        }

    }

    public Boolean isPlaying(){

        if (mediaPlayer == null ){
            return false;
        }

        return mediaPlayer.isPlaying();
    }

    public void stopPlayback() {

        if (mediaPlayer != null) {
            mediaPlayer.stop();
        }
    }

    public void start() {
        if (mediaPlayer != null) {
            mediaPlayer.start();
        }
    }


    /***************************************************************************************
     *
     * MediaPlayer Listener Methods
     *
     ***************************************************************************************/

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {

        mediaPlayer.reset();
         clearSurface(this.getSurfaceTexture());

        if (onMediaCompletionListener != null){
            onMediaCompletionListener.onCompletion(mediaPlayer);
        }
    }

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mediaPlayer.start();
    }

    @Override
    public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {

        return false;
    }

    /***************************************************************************************
     *
     * TextureView.SurfaceTextureListener
     *
    ***************************************************************************************/

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {

    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {

    }

    /**
     * Clear the given surface Texture by attaching a GL context and clearing the surface.
     * @param texture a valid SurfaceTexture
     */
    private void clearSurface(SurfaceTexture texture) {

        if(texture == null){
            return;
        }

        EGL10 egl = (EGL10) EGLContext.getEGL();
        EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
        egl.eglInitialize(display, null);

        int[] attribList = {
                EGL10.EGL_RED_SIZE, 8,
                EGL10.EGL_GREEN_SIZE, 8,
                EGL10.EGL_BLUE_SIZE, 8,
                EGL10.EGL_ALPHA_SIZE, 8,
                EGL10.EGL_RENDERABLE_TYPE, EGL10.EGL_WINDOW_BIT,
                EGL10.EGL_NONE, 0,      // placeholder for recordable [@-3]
                EGL10.EGL_NONE
        };
        EGLConfig[] configs = new EGLConfig[1];
        int[] numConfigs = new int[1];
        egl.eglChooseConfig(display, attribList, configs, configs.length, numConfigs);
        EGLConfig config = configs[0];
        EGLContext context = egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, new int[]{
                12440, 2,
                EGL10.EGL_NONE
        });


        EGLSurface eglSurface = egl.eglCreateWindowSurface(display, config, texture,
                new int[]{
                        EGL10.EGL_NONE
                });

        egl.eglMakeCurrent(display, eglSurface, eglSurface, context);
        GLES20.glClearColor(0, 0, 0, 1);
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        egl.eglSwapBuffers(display, eglSurface);
        egl.eglDestroySurface(display, eglSurface);
        egl.eglMakeCurrent(display, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE,
                EGL10.EGL_NO_CONTEXT);
        egl.eglDestroyContext(display, context);
        egl.eglTerminate(display);
    }

}

Layout File

(布局文件)

<com.github.rongi.rotate_layout.layout.RotateLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:angle="270">

        <com.videoplayer.custom_views.CustomVideoView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/video_view"/>

</com.github.rongi.rotate_layout.layout.RotateLayout>

Any help or guidance will be highly appreciated.

(任何帮助或指导将不胜感激。)

  ask by Love Kumar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...