单击两次时,SurfaceView上的按钮才起作用

时间:2019-01-14 03:16:52

标签: java android xml android-layout

我有一个覆盖整个屏幕的表面视图,显示通过rtmp流式传输的摄像机。在此上方,我有一个“开始/停止”按钮,只有在您首先单击屏幕上的任何位置后,才能单击该按钮。我希望它可以立即被点击。

我尝试将Surfaceview移出屏幕以查看其是否干扰按钮。当我这样做时,按钮正常工作。这暗示Surfaceview引起了问题,但我不知道如何解决。我已经在表面视图上尝试过android:clickable="false",但这没有用。我还尝试摆弄android:focusableandroid:focusableInTouchMode

这是来自activity_live.xml的相关部分

    <SurfaceView
        android:id="@+id/cameraView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/bottomCoverView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/topCoverView"
        app:layout_constraintVertical_bias="0.0" />

    <ImageButton
        android:id="@+id/startStopButton"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_marginBottom="25dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/startStopButtonDesc"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toBottomOf="@+id/cameraView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/start_live_button" />

这是LiveActivity.java中的相关代码

public class LiveActivity
        extends AppCompatActivity
        implements ConnectCheckerRtmp, View.OnClickListener, SurfaceHolder.Callback {

    private RtmpCamera1 rtmpCamera1;
    private TextView SStext;
    private String rtmpURL;
    private TextView timerTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live);

        SurfaceView surfaceView = findViewById(R.id.cameraView);
        rtmpCamera1 = new RtmpCamera1(surfaceView, this);
        surfaceView.getHolder().addCallback(this);

        ImageButton SSbutton = findViewById(R.id.startStopButton);
        SSbutton.setOnClickListener(this);

        SStext = findViewById(R.id.startStopText);

    @Override
    public void onConnectionFailedRtmp(final String reason) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(LiveActivity.this, "Connection failed. " + reason, Toast.LENGTH_SHORT)
                        .show();
                rtmpCamera1.stopStream();
                SStext.setText(R.string.startButton);
            }
        });
    }

    //Button Logic:
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.startStopButton:
                if (!rtmpCamera1.isStreaming()) {
                    if (rtmpCamera1.isRecording() || rtmpCamera1.prepareAudio() && rtmpCamera1.prepareVideo()) {
                        SStext.setText(R.string.stopButton);
                        rtmpCamera1.startStream(rtmpURL);

                    } else {
                        Toast.makeText(this, "Error preparing stream, This device cant do it", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    SStext.setText(R.string.startButton);
                    rtmpCamera1.stopStream();
                }
                break;
                /* I have other buttons here too, but theyre irrelevant. These buttons also don't work unless anywhere on the screen is clicked first */
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        rtmpCamera1.startPreview();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        if (rtmpCamera1.isStreaming()) {
            rtmpCamera1.stopStream();
            SStext.setText(getResources().getString(R.string.startButton));
        }
        rtmpCamera1.stopPreview();
    }
}



0 个答案:

没有答案