搜尋套件、儲存庫與 APP 分類

套件詳情v4.1.0

headset_connection_event

用於耳機事件的 Flutter 插件。偵測耳機是否已插入和拔出。取得目前耳機狀態。

AndroidiOS
近 30 天下載
2190
喜歡數
28
Pub 分數
145

安裝套件

flutter pub add headset_connection_event

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter 插件,可在 Android 和 iOS 裝置上偵測耳機是否已插入或拔除。它提供即時更新目前的耳機連接狀態,讓應用程式能動態回應音訊配件的變更。非常適合媒體播放器、語音應用程式,或任何需要掌握音訊輸出設定的應用程式。此插件透過平台通道與原生裝置 API 無縫整合。

適用情境

  • 媒體播放器音訊路由
  • 語音助理輸入切換
  • 即時音訊裝置監控
  • 基於頭戴裝置存在的使用者體驗適應
  • 可存取性功能觸發

主要特色

  • 即時耳機插入/拔出偵測
  • 跨平台支援(Android 與 iOS)
  • 簡單的事件流 API
  • 目前狀態取得
  • 輕量且依賴極少

適合對象

  • 以音訊為重點的應用程式
  • 需要硬體互動的移動應用
  • 需要響應式UI以應對音頻設備變更的應用程式
  • 開發無障礙工具的開發者
  • Flutter 項目同時針對 Android 和 iOS

使用建議

  • Android 上需要適當的權限
  • 僅限頭戴式裝置連接事件
  • 不支援藍牙耳機的進階功能,僅支援基本偵測
  • 某些平台可能需要背景執行處理
  • 依賴於原生平台功能

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Headset Connection Event Flutter Plugin

A Flutter plugin to get a headset event.

Flutter

This is a clone of headset_event, but with fix on swift errors and bluetooth connection events for Android and iOS

Migrated to AndroidX

Current Status

Platform Physical Headset Bluetooth
iOS ✅ ✅
Android ✅ ✅

Usage

To use this plugin, add headset_connection_event as a dependency in your pubspec.yaml file.

Example
    // Import package
    import 'package:headset_connection_event/headset_event.dart';

    // Instantiate it
    HeadsetEvent headsetPlugin = new HeadsetEvent();
    HeadsetState headsetEvent;

    /// if headset is plugged
    headsetPlugin.getCurrentState.then((_val){
      setState(() {
        headsetEvent = _val;
      });
    });

    /// Detect the moment headset is plugged or unplugged
    headsetPlugin.setListener((_val) {
      setState(() {
        headsetEvent = _val;
      });
    });
Android setup

Update compileSdkVersion to 33 in your build.properties under app directory

Make the following changes to your project's main AndroidManifest.xml file:

<activity...
 <!-- ADD THIS BELOW "ACTIVITY" -->
 android:exported="true"





    <!-- ADD THIS "RECEIVER" Element Before </application> -->
  <receiver android:name="flutter.moum.headset_event.HeadsetBroadcastReceiver"
        android:exported="true">
           <intent-filter>
               <action android:name="android.intent.action.HEADSET_PLUG" />
               <action android:name="android.intent.action.MEDIA_BUTTON" />
           </intent-filter>
       </receiver>


  </application>
</manifest>

Please see example project AndroidManifest.xml for more information.

Android 12 requires bluetoothConnect permission. You may request it using the permission_handler:

Add permission in your manifest

<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
import 'package:permission_handler/permission_handler.dart';

Future<bool> requestPermission() async {
  if (!Platform.isAndroid) return true;
  return Permission.bluetoothConnect.request().isGranted;
}

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。