У меня есть вид поверхности, охватывающий весь экран, отображающий камеру, которая транслируется через RTMP. Кроме того, у меня есть кнопка «Пуск / Стоп», которую можно нажимать только после того, как вы нажмете где-нибудь на экране первым. Я хочу, чтобы он был мгновенно активирован.
Я попытался переместить поверхностный вид за экран, чтобы посмотреть, не мешает ли он кнопке. Когда я это сделал, кнопка работала нормально. Это намекает на то, что SurfaceView вызывает проблему, но я не знаю, как ее исправить. Я пробовал android:clickable="false"
на поверхности, но это не сработало. Я также попытался поиграться с android:focusable
и android: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();
}
}