简体   繁体   中英

MissingPluginException on try of use custom EventChannel in flutter

Recently I decided to create flutter application I have only problem with one thing.

I wanted to create custom stream to detect unplug of headset to stop music playing.

This is my MainActivity.

package com.example.radio24

import android.content.BroadcastReceiver
import android.content.IntentFilter
import android.content.Context
import android.content.Intent
import android.media.AudioManager
import android.os.Bundle
import android.os.PersistableBundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugin.common.EventChannel

class MainActivity: FlutterActivity() {
    private val HEADPHONES_LISTENER_CHANNEL = "com.radioanime24.dev/headphones"
    lateinit var broadCastReceiver: BroadcastReceiver;
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        EventChannel(flutterEngine?.dartExecutor?.binaryMessenger, HEADPHONES_LISTENER_CHANNEL).setStreamHandler(
                object: EventChannel.StreamHandler {
                    override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
                        broadCastReceiver = object : BroadcastReceiver() {
                            override fun onReceive(contxt: Context?, intent: Intent?) {
                                when (intent?.action) {
                                    AudioManager.ACTION_AUDIO_BECOMING_NOISY -> {
                                        events?.success("HEADPHONES_UNPLUGGED")
                                    }
                                }
                            }
                        }
                        val filter = IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY)
                        registerReceiver(broadCastReceiver, filter);
                    }
                    override fun onCancel(arguments: Any?) {
                        unregisterReceiver(broadCastReceiver)
                    }
                }
        )
    }
}

In dart I am trying to consume this stream with:


    Stream headPhonesStateStream = EventChannel('com.radioanime24.dev/headphones').receiveBroadcastStream();
    headPhonesStateStream.listen((event) {
      if (event == 'HEADPHONES_UNPLUGGED') {
        if (AudioService.playbackState.playing) {
          AudioService.pause();
        }
      }
    });

But I am receiving error:

MissingPluginException(No implementation found for method listen on channel com.radioanime24.dev/headphones)

How should I register eventchannel to omit this error?

I believe you should override the onCreate(savedInstanceState: Bundle?) method instead of onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) of the Activity .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM