Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Flutter Plugin V2 #4

Merged
merged 2 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.