From a3e1a16a203dcdee6950f0cc377d82a41f3a8154 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sat, 26 Feb 2022 17:27:26 +0500 Subject: [PATCH] Uploading compressed video into db. Compressed video stores in internal app dir at now. --- app/build.gradle | 10 ++++- .../question/AddOwnQuestionPresenter.kt | 38 +++++++++++++++++-- .../socialquiz/utils/VideoCompressor.kt | 34 +++++++++++++++++ 3 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/com/desiredsoftware/socialquiz/utils/VideoCompressor.kt diff --git a/app/build.gradle b/app/build.gradle index a032ef4..345e3cc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,8 +20,8 @@ android { applicationId "com.desiredsoftware.socialquiz" minSdkVersion 21 targetSdkVersion 30 - versionCode 7 - versionName "1.0.7" + versionCode 11 + versionName "1.0.11" multiDexEnabled true resConfigs "ru" @@ -62,6 +62,7 @@ dependencies { implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5' implementation 'androidx.navigation:navigation-ui-ktx:2.3.5' implementation 'androidx.legacy:legacy-support-v4:1.0.0' + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2' // Import the Firebase services implementation platform('com.google.firebase:firebase-bom:28.3.1') @@ -92,6 +93,11 @@ dependencies { // Video compressing implementation 'com.iceteck.silicompressorr:silicompressor:2.2.4' + //implementation 'com.googlecode.mp4parser:isoparser:1.1.22' + + implementation ('com.googlecode.mp4parser:isoparser:1.0.6') { + exclude group: 'org.aspectj', module: 'aspectjrt' + } def exoPlayerVersion = "2.14.1" implementation "com.google.android.exoplayer:exoplayer:$exoPlayerVersion" diff --git a/app/src/main/java/com/desiredsoftware/socialquiz/presenter/question/AddOwnQuestionPresenter.kt b/app/src/main/java/com/desiredsoftware/socialquiz/presenter/question/AddOwnQuestionPresenter.kt index c192c75..5db7fc0 100644 --- a/app/src/main/java/com/desiredsoftware/socialquiz/presenter/question/AddOwnQuestionPresenter.kt +++ b/app/src/main/java/com/desiredsoftware/socialquiz/presenter/question/AddOwnQuestionPresenter.kt @@ -1,12 +1,15 @@ package com.desiredsoftware.socialquiz.presenter.question import android.content.Context +import android.net.Uri +import android.os.Environment import android.util.Log import com.desiredsoftware.socialquiz.R import com.desiredsoftware.socialquiz.data.model.category.Category import com.desiredsoftware.socialquiz.data.model.question.Answer import com.desiredsoftware.socialquiz.data.model.question.Question import com.desiredsoftware.socialquiz.data.repository.FirebaseRepository +import com.desiredsoftware.socialquiz.utils.VideoCompressor import com.desiredsoftware.socialquiz.view.IError import com.google.firebase.auth.FirebaseUser import com.google.firebase.storage.StorageReference @@ -66,16 +69,45 @@ class AddOwnQuestionPresenter @Inject constructor( val videoAbsolutePath: String = videoAbsolutePath if (videoAbsolutePath.isNotEmpty()) { - var videoFullRef: StorageReference? = null + var videoFullRef: StorageReference? + + var compressor: VideoCompressor? = VideoCompressor(context) + + val destinationDir = Environment.DIRECTORY_MOVIES + + val compressedFilePath = compressor?.compressVideo( + videoAbsolutePath, + destinationDir) firebaseUser?.let { user -> videoFullRef = firebaseStorageRef.child(VIDEO_PATH + user.uid + "/" + videoFileName) - val stream = FileInputStream(File(videoAbsolutePath)) + var stream : FileInputStream? = null + + try{ + Log.d("compressVideo", "Try to create stream") + compressedFilePath?.let{ + val file = File(it) + stream = FileInputStream(file) + Log.d("compressVideo", "stream created from compressed file") + } ?: kotlin.run { + stream = FileInputStream(File(videoAbsolutePath)) + Log.d("compressVideo", "stream created from non-compressed file") + } + } + catch (e: Exception){ + Log.e("compressVideo", "can't create stream: message = $e") + viewState.showError(context.resources.getString(R.string.upload_video_error)) + } + + compressor = null + viewState.showError(context.resources.getString(R.string.uploading_question)) viewState.enableQuestionButton(false) - val uploadTask = videoFullRef?.putStream(stream) + val uploadTask = stream?.let { + videoFullRef?.putStream(it) + } uploadTask?.addOnFailureListener { viewState.showError(context.resources.getString(R.string.upload_video_error)) viewState.enableQuestionButton(true) diff --git a/app/src/main/java/com/desiredsoftware/socialquiz/utils/VideoCompressor.kt b/app/src/main/java/com/desiredsoftware/socialquiz/utils/VideoCompressor.kt new file mode 100644 index 0000000..a757ecd --- /dev/null +++ b/app/src/main/java/com/desiredsoftware/socialquiz/utils/VideoCompressor.kt @@ -0,0 +1,34 @@ +package com.desiredsoftware.socialquiz.utils + +import android.content.Context +import android.util.Log +import com.iceteck.silicompressorr.SiliCompressor +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import java.io.File + +class VideoCompressor constructor( + val context: Context +) { + suspend fun compressVideo(videoUriString: String, destinationDirectory: String): String? { + var compressedFilePath : String? = null + Log.d("compressVideo", "compressVideo start") + withContext(Dispatchers.IO) { + try { + val f: File = context.getDir(destinationDirectory, Context.MODE_PRIVATE) + if (f.mkdirs() || f.isDirectory){ + compressedFilePath = SiliCompressor.with(context) + .compressVideo(videoUriString, f.absolutePath) + } + else{ + Log.e("compressVideo", "compressVideo error: can't create dir") + } + + } catch (e: Exception) { + Log.e("compressVideo", "compressVideo error: ${e.message}, cause: ${e.cause}") + } + } + Log.d("compressVideo", "compressVideo after await: compressedFilePath=$compressedFilePath") + return compressedFilePath + } +} \ No newline at end of file