重新启动活动重新启动表面视图

时间:2016-12-27 14:53:56

标签: android surfaceview

抱歉,我正在努力了解我做错了什么。

我有一个使用SurfaceView从MediaDecoder播放一些内容的活动。这一切都在第一次运行时正常,但在第二次运行时没有。因此,如果用户启动活动,它显示正确,而不是用户离开活动(但不是应用程序),但当它返回到讨论中的活动时,我无法再次播放Mediacodec。

这就是我所拥有的:

//LifeCycle Function
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.d("HU-SERVICE","Prevent onDestroy being called due to orientation and other changes....");
    }
    @Override
    protected  void onRestart(){
        super.onRestart();
        Log.d("HU-SERVICE","Player on Restart");
    }
    @Override
    protected  void onStop(){
        super.onStop();
        Log.d("HU-SERVICE","Player on Stop");
    }
    @Override
    protected  void onDestroy(){
        super.onDestroy ();
        Log.d("HU-SERVICE","Player on Destory");
    }
    @Override
    protected  void onPause(){
        super.onPause ();
        m_codec.flush();
        Log.d("HU-SERVICE","Player on Pause");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);
        getWindow ().addFlags (android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
         mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);

        mSurfaceView.getHolder().addCallback(this);
        mSurfaceView.setOnTouchListener (new View.OnTouchListener () {
            @Override
            public boolean onTouch (View v, MotionEvent event) {
                touch_send (event);
                return (true);
            }
        });

    }
//SurfaceView
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    mSurfaceView.setVisibility(View.VISIBLE);
    }


    @Override
    public void surfaceChanged(SurfaceHolder holder, int format_2, int width, int height) {

        if (m_codec != null) {    //Clear previos codec, should not happen!
            m_codec.stop();
            m_codec=null;
        }

            Log.d("HU-SERVICE","Setting up media player");
     if (!PreferenceManager.getDefaultSharedPreferences(this).getBoolean("stretch_full",true))
            {

                float aspect_ratio=(float)1.66;
                if (m_virt_vid_wid!=800f)
                    aspect_ratio=(float)1.77;
                height=(int) (width/aspect_ratio);
                mSurfaceView.setLayoutParams(new FrameLayout.LayoutParams(width, height, 17));
            }

            try {
                if (! PreferenceManager.getDefaultSharedPreferences(this).getBoolean("h264",false))
                { m_codec = MediaCodec.createDecoderByType ("video/avc");       // Create video codec
                    Log.d("HU-SERVICE","Using Hardware decoing");
                }
                else {
                    Log.d("HU-SERVICE","Using Software decoing");
                    m_codec = MediaCodec.createByCodecName("OMX.google.h264.decoder");       // Use Google OMX (software) deocding.
                    h264_wait=10000;
                    }
                }
                catch (Throwable t) {
                    hu_uti.loge ("Throwable creating video/avc decoder: " + t);
                }
            try {
                m_codec_buf_info = new MediaCodec.BufferInfo();                       
                MediaFormat format = MediaFormat.createVideoFormat ("video/avc", width, height);
                m_codec.configure (format, holder.getSurface(), null, 0);              
                m_codec.start ();                                           
                }
                catch (Throwable t) {
                    hu_uti.loge ("Throwable: " + t);
                }

            }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        holder.removeCallback(this);
    holder.getSurface().release();

    }

这是布局文件:

<?xml version="1.0" encoding="utf-8"?>


    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ll_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_gravity="center_vertical"
            android:background="#000000">

            <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            />
    </FrameLayout>

这是我的变量(它们都不是静态的)

private SurfaceView mSurfaceView;
private double m_virt_vid_wid=800f;
private double m_virt_vid_hei=480f;
private PowerManager m_pwr_mgr;
private PowerManager.WakeLock m_wakelock;
private MediaCodec m_codec;
private MediaCodec.BufferInfo m_codec_buf_info;
private int h264_wait;

在活动的第一次运行中,我在第二次运行时有音频+视频我听到了音频,但屏幕是纯黑色的,我缺少什么?

修改

我做了一些挖掘,我发现在相对较短的一段时间后,在活动的第二次运行中,我发现以下错误:

E/Surface: queueBuffer: error queuing buffer to SurfaceTexture, -19
E/ACodec: queueBuffer failed in onOutputBufferDrained: -19
E/ACodec: signalError(omxError 0x80001001, internalError -19)
E/MediaCodec: Codec reported err 0xffffffed, actionCode 0, while in state 6

就像MediaCode试图以某种方式输出到不同的Surface ... 我真的迷路了,找不到能解释这种行为的任何东西......

0 个答案:

没有答案