提问人:SpaceXS 提问时间:11/7/2023 更新时间:11/7/2023 访问量:35
原生于 Flutter 数据交换:Flutter 似乎没有从 MethodChannel 接收数据
Native to Flutter data exchange: Flutter seems not receiving data from MethodChannel
问:
我正在构建一个播放视频的应用程序,我可以从 Flutter 打开数据(链接视频)并将其传递给 Native,但我需要在 VideoActivity 末尾传递播放器的位置
其实我有这个代码
主要活动
class MainActivity: FlutterActivity() {
private val CHANNEL = "ankit.flutter.dev/video"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegister.registerGeneratedPlugins(FlutterEngine(this))
flutterEngine?.dartExecutor?.let {
MethodChannel(it, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "playVideo") {
goToVideoActivity()
result.success(null)
} else if (call.method == "updateVideoPosition") {
val position = call.arguments as Int
result.success(position)
}
}
}
}
视频活动
private lateinit var flutterEngine: FlutterEngine
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = VideoLayoutBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
// Inizializza il FlutterEngine all'avvio dell'app
flutterEngine = FlutterEngine(this)
flutterEngine.dartExecutor.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
FlutterEngineCache.getInstance().put("1", flutterEngine)
}
override fun onDestroy() {
super.onDestroy()
releasePlayer()
val methodChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "ankit.flutter.dev/video")
Log.d("posizione 1", "$currentPosition")
methodChannel.invokeMethod("updateVideoPosition", currentPosition)
}
$currentPosition使用正在工作的另一部分代码正确更新(末尾的日志显示正确的值)
在颤振方面,我有这个
@override
void initState() {
super.initState();
MethodChannel platform = MethodChannel("ankit.flutter.dev/video");
platform.setMethodCallHandler((call) async {
print("Metodo ricevuto: ${call.method} argomenti: ${call.arguments}");
if (call.method == 'updateVideoPosition') {
final position = call.arguments as int;
print('Current video position: $position milliseconds');
}
});
}
Flutter 似乎没有从原生活动中获得任何价值,我无法弄清楚原因(我不擅长 kotlin,我正在使用许多教程/GPT 来弄清楚我需要什么
谢谢大家的帮助
答: 暂无答案
评论
methodChannel.invokeMethod
onDestroy