Skip to content

Commit

Permalink
Uploading compressed video into db.
Browse files Browse the repository at this point in the history
Compressed video stores in internal app dir at now.
  • Loading branch information
kirkaDev committed Feb 26, 2022
1 parent 9ba45d0 commit a3e1a16
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
10 changes: 8 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit a3e1a16

Please sign in to comment.