Skip to content

Commit

Permalink
- Cancel compression job
Browse files Browse the repository at this point in the history
- Fixed a bug preventing video compression
  • Loading branch information
AbedElazizShe committed May 4, 2020
1 parent 535602d commit 0b178cd
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 26 deletions.
3 changes: 0 additions & 3 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.text.format.DateUtils
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
Expand Down Expand Up @@ -43,6 +44,10 @@ class MainActivity : AppCompatActivity() {
pickVideo()
}

cancel.setOnClickListener {
VideoCompressor.cancel()
}

videoLayout.setOnClickListener { VideoPlayerActivity.start(this, path) }
}

Expand Down Expand Up @@ -88,7 +93,7 @@ class MainActivity : AppCompatActivity() {

var time = 0L

VideoCompressor.doVideoCompression(
VideoCompressor.start(
path,
desFile.path,
object : CompressionListener {
Expand Down Expand Up @@ -121,6 +126,10 @@ class MainActivity : AppCompatActivity() {
progress.text = "This video cannot be compressed!"
}

override fun onCancelled() {
Log.wtf("TAG", "compression has been canclled")
// make UI changes, cleanup, etc
}
})
}
}
Expand Down
16 changes: 13 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<FrameLayout
<RelativeLayout
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
Expand All @@ -21,14 +21,24 @@
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:text="@string/home_title"
android:textColor="@color/colorWhite"
android:textSize="16sp" />
</FrameLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cancel"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:text="cancel"
/>
</RelativeLayout>

</com.google.android.material.appbar.AppBarLayout>


<RelativeLayout
android:visibility="gone"
android:id="@+id/mainContents"
Expand Down
7 changes: 6 additions & 1 deletion lightcompressor/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
/build
.idea/
.gradle/
build/
obj/
*.iml
local.properties
2 changes: 2 additions & 0 deletions lightcompressor/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ interface CompressionListener {
fun onSuccess()
fun onFailure()
fun onProgress(percent: Float)
fun onCancelled()
}

interface CompressionProgressListener {
fun onProgressChanged(percent: Float)
fun onProgressCancelled()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.util.Log
import java.io.File
import java.lang.Exception
import java.nio.ByteBuffer
import kotlin.math.roundToInt

/**
* Created by AbedElaziz Shehadeh on 27 Jan, 2020
Expand All @@ -16,10 +17,11 @@ import java.nio.ByteBuffer
object Compressor {

private const val MIN_BITRATE = 2000000
private const val MIN_HEIGHT = 640
private const val MIN_WIDTH = 360
private const val MIN_HEIGHT = 640.0
private const val MIN_WIDTH = 360.0
private const val MIME_TYPE = "video/avc"

var isRunning = true

fun compressVideo(
source: String,
Expand Down Expand Up @@ -54,7 +56,7 @@ object Compressor {
val newBitrate = getBitrate(bitrate)

//Handle new width and height values
var (newWidth, newHeight) = generateWidthAndHeight(width, height)
var (newWidth, newHeight) = generateWidthAndHeight(width.toDouble(), height.toDouble())

//Handle rotation values and swapping height and width if needed
rotation = when (rotation) {
Expand Down Expand Up @@ -190,6 +192,12 @@ object Compressor {
var encoderOutputAvailable = true

loop@ while (decoderOutputAvailable || encoderOutputAvailable) {

if(!isRunning){
listener.onProgressCancelled()
return false
}

//Encoder
val encoderStatus = encoder.dequeueOutputBuffer(bufferInfo, 0)

Expand Down Expand Up @@ -371,19 +379,19 @@ object Compressor {
* @param height file's original height
* @return new width and height pair
*/
private fun generateWidthAndHeight(width: Int, height: Int): Pair<Int, Int> {
private fun generateWidthAndHeight(width: Double, height: Double): Pair<Int, Int> {

val newWidth: Int
val newHeight: Int
val newWidth: Double
val newHeight: Double

when {
width >= 1920 || height >= 1920 -> {
newWidth = (width * 0.5).toInt()
newHeight = (height * 0.5).toInt()
newWidth = (width * 0.5)
newHeight = (height * 0.5)
}
width >= 1280 || height >= 1280 -> {
newWidth = (width * 0.75).toInt()
newHeight = (height * 0.75).toInt()
newWidth = (width * 0.75)
newHeight = (height * 0.75)
}
width >= 960 || height >= 960 -> {
newWidth = MIN_HEIGHT
Expand All @@ -395,7 +403,7 @@ object Compressor {
}
}

return Pair(newWidth, newHeight)
return Pair(2 * ((newWidth / 2).roundToInt()), 2 * ((newHeight / 2).roundToInt()))
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.abedelazizshe.lightcompressorlibrary

import com.abedelazizshe.lightcompressorlibrary.Compressor.compressVideo
import com.abedelazizshe.lightcompressorlibrary.Compressor.isRunning
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext

object VideoCompressor : CoroutineScope {
object VideoCompressor : CoroutineScope by MainScope() {

private var job: Job = Job()

override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job // to run code in Main(UI) Thread

fun doVideoCompression(srcPath: String, destPath: String, listener: CompressionListener) = launch {
private fun doVideoCompression(srcPath: String, destPath: String, listener: CompressionListener) = launch {
isRunning = true
listener.onStart()
val result = startCompression(srcPath, destPath, listener)

Expand All @@ -24,13 +22,26 @@ object VideoCompressor : CoroutineScope {

}

fun start(srcPath: String, destPath: String, listener: CompressionListener){
job = doVideoCompression(srcPath, destPath, listener)
}

fun cancel(){
job.cancel()
isRunning = false
}

// To run code in Background Thread
private suspend fun startCompression(srcPath: String, destPath: String, listener: CompressionListener) : Boolean = withContext(Dispatchers.IO){

return@withContext compressVideo(srcPath, destPath, object : CompressionProgressListener {
override fun onProgressChanged(percent: Float) {
listener.onProgress(percent)
}

override fun onProgressCancelled() {
listener.onCancelled()
}
})
}

Expand Down

0 comments on commit 0b178cd

Please sign in to comment.