Skip to content

Commit

Permalink
update example and gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro committed May 26, 2021
1 parent fa29a86 commit 00730d5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 68 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ dependencies {
implementation project(':rtspserver')
implementation 'com.github.pedroSG94.rtmp-rtsp-stream-client-java:rtplibrary:2.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
}
129 changes: 69 additions & 60 deletions app/src/main/java/com/pedro/sample/CameraDemoActivity.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.pedro.sample

import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.view.SurfaceHolder
import android.view.View
import android.view.WindowManager
Expand All @@ -20,23 +20,22 @@ import java.util.*
class CameraDemoActivity : AppCompatActivity(), ConnectCheckerRtsp, View.OnClickListener,
SurfaceHolder.Callback {

private var rtspServerCamera1: RtspServerCamera1? = null
private var button: Button? = null
private var bRecord: Button? = null
private lateinit var rtspServerCamera1: RtspServerCamera1
private lateinit var button: Button
private lateinit var bRecord: Button

private var currentDateAndTime = ""
private val folder =
File(Environment.getExternalStorageDirectory().absolutePath + "/rtmp-rtsp-stream-client-java")
private lateinit var folder: File

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
setContentView(R.layout.activity_camera_demo)

folder = File(getExternalFilesDir(null)!!.absolutePath + "/rtmp-rtsp-stream-client-java")
button = findViewById(R.id.b_start_stop)
button?.setOnClickListener(this)
button.setOnClickListener(this)
bRecord = findViewById(R.id.b_record)
bRecord?.setOnClickListener(this)
bRecord.setOnClickListener(this)
switch_camera.setOnClickListener(this)
rtspServerCamera1 = RtspServerCamera1(surfaceView, this, 1935)
surfaceView.holder.addCallback(this)
Expand All @@ -56,8 +55,8 @@ class CameraDemoActivity : AppCompatActivity(), ConnectCheckerRtsp, View.OnClick
runOnUiThread {
Toast.makeText(this@CameraDemoActivity, "Connection failed. $reason", Toast.LENGTH_SHORT)
.show()
rtspServerCamera1!!.stopStream()
button?.setText(R.string.start_button)
rtspServerCamera1.stopStream()
button.setText(R.string.start_button)
}
}

Expand All @@ -70,8 +69,8 @@ class CameraDemoActivity : AppCompatActivity(), ConnectCheckerRtsp, View.OnClick
override fun onAuthErrorRtsp() {
runOnUiThread {
Toast.makeText(this@CameraDemoActivity, "Auth error", Toast.LENGTH_SHORT).show()
rtspServerCamera1?.stopStream()
button!!.setText(R.string.start_button)
rtspServerCamera1.stopStream()
button.setText(R.string.start_button)
tv_url.text = ""
}
}
Expand All @@ -84,58 +83,67 @@ class CameraDemoActivity : AppCompatActivity(), ConnectCheckerRtsp, View.OnClick

override fun onClick(view: View) {
when (view.id) {
R.id.b_start_stop -> if (!rtspServerCamera1!!.isStreaming) {
if (rtspServerCamera1!!.isRecording || rtspServerCamera1!!.prepareAudio() && rtspServerCamera1!!.prepareVideo()) {
button!!.setText(R.string.stop_button)
rtspServerCamera1!!.startStream()
tv_url.text = rtspServerCamera1?.getEndPointConnection()
R.id.b_start_stop -> if (!rtspServerCamera1.isStreaming) {
if (rtspServerCamera1.isRecording || rtspServerCamera1.prepareAudio() && rtspServerCamera1.prepareVideo()) {
button.setText(R.string.stop_button)
rtspServerCamera1.startStream()
tv_url.text = rtspServerCamera1.getEndPointConnection()
} else {
Toast.makeText(this, "Error preparing stream, This device cant do it", Toast.LENGTH_SHORT)
.show()
}
} else {
button!!.setText(R.string.start_button)
rtspServerCamera1!!.stopStream()
button.setText(R.string.start_button)
rtspServerCamera1.stopStream()
tv_url.text = ""
}
R.id.switch_camera -> try {
rtspServerCamera1!!.switchCamera()
rtspServerCamera1.switchCamera()
} catch (e: CameraOpenException) {
Toast.makeText(this, e.message, Toast.LENGTH_SHORT).show()
}

R.id.b_record -> if (!rtspServerCamera1!!.isRecording) {
try {
if (!folder.exists()) {
folder.mkdir()
}
val sdf = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault())
currentDateAndTime = sdf.format(Date())
if (!rtspServerCamera1!!.isStreaming) {
if (rtspServerCamera1!!.prepareAudio() && rtspServerCamera1!!.prepareVideo()) {
rtspServerCamera1!!.startRecord(
folder.absolutePath + "/" + currentDateAndTime + ".mp4")
bRecord!!.setText(R.string.stop_record)
Toast.makeText(this, "Recording... ", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Error preparing stream, This device cant do it",
Toast.LENGTH_SHORT).show()
R.id.b_record -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (!rtspServerCamera1.isRecording) {
try {
if (!folder.exists()) {
folder.mkdir()
}
val sdf = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault())
currentDateAndTime = sdf.format(Date())
if (!rtspServerCamera1.isStreaming) {
if (rtspServerCamera1.prepareAudio() && rtspServerCamera1.prepareVideo()) {
rtspServerCamera1.startRecord(folder.absolutePath + "/" + currentDateAndTime + ".mp4")
bRecord.setText(R.string.stop_record)
Toast.makeText(this, "Recording... ", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(
this, "Error preparing stream, This device cant do it",
Toast.LENGTH_SHORT
).show()
}
} else {
rtspServerCamera1.startRecord(folder.absolutePath + "/" + currentDateAndTime + ".mp4")
bRecord.setText(R.string.stop_record)
Toast.makeText(this, "Recording... ", Toast.LENGTH_SHORT).show()
}
} catch (e: IOException) {
rtspServerCamera1.stopRecord()
bRecord.setText(R.string.start_record)
Toast.makeText(this, e.message, Toast.LENGTH_SHORT).show()
}
} else {
rtspServerCamera1!!.startRecord(folder.absolutePath + "/" + currentDateAndTime + ".mp4")
bRecord!!.setText(R.string.stop_record)
Toast.makeText(this, "Recording... ", Toast.LENGTH_SHORT).show()
rtspServerCamera1.stopRecord()
bRecord.setText(R.string.start_record)
Toast.makeText(
this, "file " + currentDateAndTime + ".mp4 saved in " + folder.absolutePath,
Toast.LENGTH_SHORT
).show()
}
} catch (e: IOException) {
rtspServerCamera1!!.stopRecord()
bRecord!!.setText(R.string.start_record)
Toast.makeText(this, e.message, Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "You need min JELLY_BEAN_MR2(API 18) for do it...", Toast.LENGTH_SHORT).show()
}
} else {
rtspServerCamera1!!.stopRecord()
bRecord!!.setText(R.string.start_record)
Toast.makeText(this, "file " + currentDateAndTime + ".mp4 saved in " + folder.absolutePath,
Toast.LENGTH_SHORT).show()
}
else -> {
}
Expand All @@ -146,22 +154,23 @@ class CameraDemoActivity : AppCompatActivity(), ConnectCheckerRtsp, View.OnClick
}

override fun surfaceChanged(surfaceHolder: SurfaceHolder, i: Int, i1: Int, i2: Int) {
rtspServerCamera1!!.startPreview()
rtspServerCamera1.startPreview()
}

override fun surfaceDestroyed(surfaceHolder: SurfaceHolder) {
if (rtspServerCamera1!!.isRecording) {
rtspServerCamera1!!.stopRecord()
bRecord!!.setText(R.string.start_record)
Toast.makeText(this, "file " + currentDateAndTime + ".mp4 saved in " + folder.absolutePath,
Toast.LENGTH_SHORT).show()
currentDateAndTime = ""
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (rtspServerCamera1.isRecording) {
rtspServerCamera1.stopRecord()
bRecord.setText(R.string.start_record)
Toast.makeText(this, "file " + currentDateAndTime + ".mp4 saved in " + folder.absolutePath, Toast.LENGTH_SHORT).show()
currentDateAndTime = ""
}
}
if (rtspServerCamera1!!.isStreaming) {
rtspServerCamera1!!.stopStream()
button!!.text = resources.getString(R.string.start_button)
if (rtspServerCamera1.isStreaming) {
rtspServerCamera1.stopStream()
button.text = resources.getString(R.string.start_button)
tv_url.text = ""
}
rtspServerCamera1!!.stopPreview()
rtspServerCamera1.stopPreview()
}
}
6 changes: 2 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.4.32'
ext.kotlin_version = '1.5.0'
repositories {
jcenter()
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.3'
classpath 'com.android.tools.build:gradle:4.2.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -19,7 +18,6 @@ buildscript {

allprojects {
repositories {
jcenter()
google()
mavenCentral()
maven { url 'https://jitpack.io' }
Expand Down
6 changes: 3 additions & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Jan 21 11:20:02 CET 2021
#Wed May 26 11:03:20 CEST 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
zipStoreBase=GRADLE_USER_HOME

0 comments on commit 00730d5

Please sign in to comment.