Skip to content

Commit

Permalink
Upgrade to Flutter Plugin V2
Browse files Browse the repository at this point in the history
  • Loading branch information
ppornkitpras-agoda committed Sep 28, 2020
1 parent 6d54cfd commit 41132a0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.example.video_compress

import android.app.Activity
import android.content.Context
import android.net.Uri
import android.util.Log
import com.otaliastudios.transcoder.Transcoder
import com.otaliastudios.transcoder.TranscoderListener
import com.otaliastudios.transcoder.strategy.DefaultVideoStrategy
import com.otaliastudios.transcoder.strategy.PassThroughTrackStrategy
import com.otaliastudios.transcoder.strategy.TrackStrategy
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
Expand All @@ -20,10 +22,20 @@ import java.util.*
/**
* VideoCompressPlugin
*/
class VideoCompressPlugin private constructor(private val activity: Activity, private val context: Context, private val channel: MethodChannel) : MethodCallHandler {
class VideoCompressPlugin : MethodCallHandler, FlutterPlugin {

var channelName = "video_compress"
private var _context: Context? = null
private var _channel: MethodChannel? = null

val channelName = "video_compress"
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
val context = _context;
val channel = _channel;

if (context == null || channel == null) {
Log.w(TAG, "Calling VideoCompress plugin before initialization")
return
}

when (call.method) {
"getByteThumbnail" -> {
Expand Down Expand Up @@ -59,7 +71,7 @@ class VideoCompressPlugin private constructor(private val activity: Activity, pr
val includeAudio = call.argument<Boolean>("includeAudio")
val frameRate = if (call.argument<Int>("frameRate")==null) 30 else call.argument<Int>("frameRate")

val tempDir: String = this.context.getExternalFilesDir("video_compress")!!.absolutePath
val tempDir: String = context.getExternalFilesDir("video_compress")!!.absolutePath
val out = SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(Date())
val destPath: String = tempDir + File.separator + "VID_" + out + ".mp4"

Expand Down Expand Up @@ -124,14 +136,30 @@ class VideoCompressPlugin private constructor(private val activity: Activity, pr
}
}

override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
init(binding.applicationContext, binding.binaryMessenger)
}

override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
_channel?.setMethodCallHandler(null)
_context = null
_channel = null
}

private fun init(context: Context, messenger: BinaryMessenger) {
val channel = MethodChannel(messenger, channelName)
channel.setMethodCallHandler(this)
_context = context
_channel = channel
}

companion object {
const val ACTIVITY_2_REQUEST = 999
private const val TAG = "video_compress"

@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "video_compress")
val instance = VideoCompressPlugin(registrar.activity(), registrar.context(), channel)
channel.setMethodCallHandler(instance)
val instance = VideoCompressPlugin()
instance.init(registrar.context(), registrar.messenger())
}
}

Expand Down
6 changes: 5 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
android:label="video_compress_example"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:name="io.flutter.embedding.android.FlutterActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
Expand All @@ -30,5 +30,9 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>

This file was deleted.

0 comments on commit 41132a0

Please sign in to comment.