├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── idv │ │ └── luchafang │ │ └── videotrimmerexample │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── idv │ │ │ └── luchafang │ │ │ └── videotrimmerexample │ │ │ └── MainActivity.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── idv │ └── luchafang │ └── videotrimmerexample │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── screenshots ├── screenshot_1.png └── screenshot_2.png ├── settings.gradle └── videotrimmer ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src ├── androidTest └── java │ └── idv │ └── luchafang │ └── videotrimmer │ └── ExampleInstrumentedTest.java ├── main ├── AndroidManifest.xml ├── java │ └── idv │ │ └── luchafang │ │ └── videotrimmer │ │ ├── Injection.kt │ │ ├── VideoTrimmerContract.kt │ │ ├── VideoTrimmerPresenter.kt │ │ ├── VideoTrimmerView.kt │ │ ├── data │ │ └── TrimmerDraft.kt │ │ ├── slidingwindow │ │ └── SlidingWindowView.kt │ │ ├── tools │ │ ├── SetVideoThumbnailAsyncTask.kt │ │ └── Utils.kt │ │ └── videoframe │ │ ├── VideoFramesAdaptor.kt │ │ ├── VideoFramesDecoration.kt │ │ └── VideoFramesScrollListener.kt └── res │ ├── drawable │ ├── trimmer_left_bar.xml │ └── trimmer_right_bar.xml │ ├── layout │ └── layout_video_trimmer.xml │ └── values │ ├── attrs.xml │ └── strings.xml └── test └── java └── idv └── luchafang └── videotrimmer └── ExampleUnitTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | app/.DS_Store 3 | app/release/* 4 | app/release/output.json 5 | # Built application files 6 | *.apk 7 | *.ap_ 8 | *.aab 9 | 10 | # Files for the ART/Dalvik VM 11 | *.dex 12 | 13 | # Java class files 14 | *.class 15 | 16 | # Generated files 17 | bin/ 18 | gen/ 19 | out/ 20 | 21 | # Gradle files 22 | .gradle/ 23 | build/ 24 | 25 | # Local configuration file (sdk path, etc) 26 | local.properties 27 | 28 | # Proguard folder generated by Eclipse 29 | proguard/ 30 | 31 | # Log Files 32 | *.log 33 | 34 | # Android Studio Navigation editor temp files 35 | .navigation/ 36 | 37 | # Android Studio captures folder 38 | captures/ 39 | 40 | # IntelliJ 41 | .idea/* 42 | *.iml 43 | 44 | # Keystore files 45 | # Uncomment the following lines if you do not want to check your keystore files in. 46 | #*.jks 47 | #*.keystore 48 | 49 | # External native build folder generated in Android Studio 2.2 and later 50 | .externalNativeBuild 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android Video Trimmer View 2 | 3 | 4 | 5 | Note that this is only the PURE android view. Consider using ffmpeg to actually trim the video. 6 | 7 | ## Import to your project 8 | Add this to project level build.gradle 9 | ``` 10 | allprojects { 11 | repositories { 12 | maven { url 'https://jitpack.io' } 13 | } 14 | } 15 | ``` 16 | 17 | And then in app level build.gradle 18 | ``` 19 | implementation 'com.github.freddyfang:android-video-trimmer:v1.0.0' 20 | ``` 21 | 22 | ## How to Use 23 | ***Layout xml*** 24 | ``` 25 | 34 | ``` 35 | 36 | ***Display it*** 37 | ``` 38 | videoTrimmerView 39 | .setVideo(File(path)) 40 | .setMaxDuration(30_000) // millis 41 | .setMinDuration(3_000) // millis 42 | .setFrameCountInWindow(8) 43 | .setExtraDragSpace(10) // pixels 44 | .setOnSelectedRangeChangedListener(this) 45 | .show() 46 | ``` 47 | 48 | ***OnSelectedRangeChangedListener*** 49 | ``` 50 | override fun onSelectRangeStart() { 51 | // Start to drag range bar or start to scroll the video frame list 52 | } 53 | 54 | override fun onSelectRange(startMillis: Long, endMillis: Long) { 55 | // Range is changing 56 | } 57 | 58 | override fun onSelectRangeEnd(startMillis: Long, endMillis: Long) { 59 | // Range selected, play the video here 60 | } 61 | ``` 62 | 63 | Also, you can save the current UI state by calling 64 | ``` 65 | val draft = videoTrimmerView.getTrimmerDraft() 66 | // draft is a parcelable object 67 | ``` 68 | and restore it when necessary. 69 | ``` 70 | videoTrimmerView.restoreTrimmer(draft) 71 | ``` 72 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 28 7 | 8 | defaultConfig { 9 | applicationId "idv.luchafang.videotrimmerexample" 10 | minSdkVersion 19 11 | targetSdkVersion 28 12 | versionCode 1 13 | versionName "1.0" 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | 24 | compileOptions { 25 | sourceCompatibility JavaVersion.VERSION_1_8 26 | targetCompatibility JavaVersion.VERSION_1_8 27 | } 28 | } 29 | 30 | dependencies { 31 | implementation fileTree(dir: 'libs', include: ['*.jar']) 32 | 33 | testImplementation 'junit:junit:4.12' 34 | androidTestImplementation 'androidx.test:runner:1.2.0' 35 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 36 | 37 | implementation 'androidx.appcompat:appcompat:1.0.2' 38 | implementation 'androidx.core:core-ktx:1.0.2' 39 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 40 | implementation "androidx.recyclerview:recyclerview:1.1.0-beta01" 41 | 42 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 43 | 44 | implementation 'com.google.android.exoplayer:exoplayer:2.10.1' 45 | 46 | implementation project(':videotrimmer') 47 | } 48 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/idv/luchafang/videotrimmerexample/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package idv.luchafang.videotrimmerexample 2 | 3 | import androidx.test.InstrumentationRegistry 4 | import androidx.test.runner.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getTargetContext() 22 | assertEquals("idv.luchafang.videotrimmerexample", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 7 | 8 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/idv/luchafang/videotrimmerexample/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package idv.luchafang.videotrimmerexample 2 | 3 | import android.Manifest 4 | import android.app.Activity 5 | import android.content.Intent 6 | import android.content.pm.PackageManager 7 | import android.database.Cursor 8 | import android.net.Uri 9 | import android.os.Bundle 10 | import android.provider.MediaStore 11 | import androidx.appcompat.app.AppCompatActivity 12 | import androidx.core.app.ActivityCompat 13 | import com.google.android.exoplayer2.ExoPlayerFactory 14 | import com.google.android.exoplayer2.SimpleExoPlayer 15 | import com.google.android.exoplayer2.source.ClippingMediaSource 16 | import com.google.android.exoplayer2.source.ProgressiveMediaSource 17 | import com.google.android.exoplayer2.upstream.DataSource 18 | import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory 19 | import idv.luchafang.videotrimmer.VideoTrimmerView 20 | import kotlinx.android.synthetic.main.activity_main.* 21 | import java.io.File 22 | 23 | 24 | class MainActivity : AppCompatActivity(), VideoTrimmerView.OnSelectedRangeChangedListener { 25 | 26 | private val REQ_PICK_VIDEO = 100 27 | private val REQ_PERMISSION = 200 28 | 29 | private val player: SimpleExoPlayer by lazy { 30 | ExoPlayerFactory.newSimpleInstance(this).also { 31 | it.repeatMode = SimpleExoPlayer.REPEAT_MODE_ALL 32 | playerView.player = it 33 | } 34 | } 35 | 36 | private val dataSourceFactory: DataSource.Factory by lazy { 37 | DefaultDataSourceFactory(this, "VideoTrimmer") 38 | } 39 | 40 | private var videoPath: String = "" 41 | 42 | /* -------------------------------------------------------------------------------------------*/ 43 | /* Activity */ 44 | override fun onCreate(savedInstanceState: Bundle?) { 45 | super.onCreate(savedInstanceState) 46 | setContentView(R.layout.activity_main) 47 | 48 | pickVideoBtn.setOnClickListener { 49 | Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI) 50 | .apply { 51 | type = "video/*" 52 | } 53 | .also { startActivityForResult(it, REQ_PICK_VIDEO) } 54 | } 55 | } 56 | 57 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 58 | super.onActivityResult(requestCode, resultCode, data) 59 | 60 | when (requestCode) { 61 | REQ_PICK_VIDEO -> { 62 | if (resultCode == Activity.RESULT_OK) { 63 | videoPath = getRealPathFromMediaData(data?.data) 64 | displayTrimmerView(videoPath) 65 | } 66 | } 67 | } 68 | } 69 | 70 | override fun onStart() { 71 | super.onStart() 72 | 73 | ActivityCompat.requestPermissions( 74 | this, 75 | arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 76 | REQ_PERMISSION 77 | ) 78 | } 79 | 80 | override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { 81 | super.onRequestPermissionsResult(requestCode, permissions, grantResults) 82 | 83 | if (requestCode == REQ_PERMISSION && grantResults.firstOrNull() != PackageManager.PERMISSION_GRANTED) { 84 | finish() 85 | } 86 | } 87 | 88 | /* -------------------------------------------------------------------------------------------*/ 89 | /* VideoTrimmerView.OnSelectedRangeChangedListener */ 90 | override fun onSelectRangeStart() { 91 | player.playWhenReady = false 92 | } 93 | 94 | override fun onSelectRange(startMillis: Long, endMillis: Long) { 95 | showDuration(startMillis, endMillis) 96 | } 97 | 98 | override fun onSelectRangeEnd(startMillis: Long, endMillis: Long) { 99 | showDuration(startMillis, endMillis) 100 | playVideo(videoPath, startMillis, endMillis) 101 | } 102 | 103 | /* -------------------------------------------------------------------------------------------*/ 104 | /* VideoTrimmer */ 105 | private fun displayTrimmerView(path: String) { 106 | videoTrimmerView 107 | .setVideo(File(path)) 108 | .setMaxDuration(30_000) 109 | .setMinDuration(3_000) 110 | .setFrameCountInWindow(8) 111 | .setExtraDragSpace(dpToPx(2f)) 112 | .setOnSelectedRangeChangedListener(this) 113 | .show() 114 | } 115 | 116 | /* -------------------------------------------------------------------------------------------*/ 117 | /* ExoPlayer2 */ 118 | private fun playVideo(path: String, startMillis: Long, endMillis: Long) { 119 | if (path.isBlank()) return 120 | 121 | val source = ProgressiveMediaSource.Factory(dataSourceFactory) 122 | .createMediaSource(Uri.parse(path)) 123 | .let { 124 | ClippingMediaSource( 125 | it, 126 | startMillis * 1000L, 127 | endMillis * 1000L 128 | ) 129 | } 130 | 131 | player.playWhenReady = true 132 | player.prepare(source) 133 | } 134 | 135 | /* -------------------------------------------------------------------------------------------*/ 136 | /* Internal helpers */ 137 | private fun getRealPathFromMediaData(data: Uri?): String { 138 | data ?: return "" 139 | 140 | var cursor: Cursor? = null 141 | try { 142 | cursor = contentResolver.query( 143 | data, 144 | arrayOf(MediaStore.Video.Media.DATA), 145 | null, null, null 146 | ) 147 | 148 | val col = cursor.getColumnIndex(MediaStore.Video.Media.DATA) 149 | cursor.moveToFirst() 150 | 151 | return cursor.getString(col) 152 | } finally { 153 | cursor?.close() 154 | } 155 | } 156 | 157 | private fun showDuration(startMillis: Long, endMillis: Long) { 158 | val duration = (endMillis - startMillis) / 1000L 159 | durationView.text = "$duration seconds selected" 160 | } 161 | 162 | private fun dpToPx(dp: Float): Float { 163 | val density = resources.displayMetrics.density 164 | return dp * density 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 | 60 | 62 | 64 | 66 | 68 | 70 | 72 | 74 | 75 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 21 | 22 |