diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5cba72ab..6b8afe45 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -21,14 +21,18 @@ jobs: Android-Test: # Linux machine doesn't support running Android emulator due to lack of nested virtualization runs-on: macos-latest - timeout-minutes: 30 + timeout-minutes: 20 steps: - uses: actions/checkout@v2 - uses: subosito/flutter-action@v1 - run: $ANDROID_HOME/tools/bin/sdkmanager "system-images;android-27;google_apis_playstore;x86" + # Make sure adb server is started and the private key is generated to be picked up by the + # emulator + - run: adb devices - run: flutter emulators --create - run: flutter emulators --launch flutter_emulator - - run: adb wait-for-device + # Use this instead of wait-for-device to see errors (e.g. Unauthorized) + - run: until adb shell true; do sleep 1; done - run: flutter drive --target=test_driver/app.dart working-directory: example diff --git a/android/src/main/kotlin/com/example/video_compress/VideoCompressPlugin.kt b/android/src/main/kotlin/com/example/video_compress/VideoCompressPlugin.kt index 70f3839c..72102324 100644 --- a/android/src/main/kotlin/com/example/video_compress/VideoCompressPlugin.kt +++ b/android/src/main/kotlin/com/example/video_compress/VideoCompressPlugin.kt @@ -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 @@ -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" -> { @@ -59,7 +71,7 @@ class VideoCompressPlugin private constructor(private val activity: Activity, pr val includeAudio = call.argument("includeAudio") val frameRate = if (call.argument("frameRate")==null) 30 else call.argument("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" @@ -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()) } } diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index ecb88cf1..a75b29dd 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -12,7 +12,7 @@ android:label="video_compress_example" android:icon="@mipmap/ic_launcher"> + + diff --git a/example/android/app/src/main/kotlin/com/example/flutter_video_compress_example/MainActivity.kt b/example/android/app/src/main/kotlin/com/example/flutter_video_compress_example/MainActivity.kt deleted file mode 100644 index 5d961b19..00000000 --- a/example/android/app/src/main/kotlin/com/example/flutter_video_compress_example/MainActivity.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.example.video_compress_example - -import android.os.Bundle - -import io.flutter.app.FlutterActivity -import io.flutter.plugins.GeneratedPluginRegistrant - -class MainActivity: FlutterActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - GeneratedPluginRegistrant.registerWith(this) - } -}