Silent Status Plugin

A Flutter plugin that checks if the device is in silent mode on Android and iOS.

Features

  • Detects silent mode status on Android devices.
  • Detects silent mode status on iOS devices.

Installation

Add this to your pubspec.yaml file:

dependencies:
  silent_status:
    path: ../silent_status  # Or use the package URL if published on pub.dev

Then run:

flutter pub get

Usage

Import the package

import 'package:silent_status/silent_status.dart';

Check Silent Mode Status

import 'package:flutter/material.dart';
import 'package:silent_status/silent_status.dart';

class CheckSilentModeScreen extends StatefulWidget {
  @override
  _CheckSilentModeScreenState createState() => _CheckSilentModeScreenState();
}

class _CheckSilentModeScreenState extends State<CheckSilentModeScreen> {
  bool? _isSilent;

  Future<void> _checkSilentMode() async {
    final isSilent = await SilentStatus.isSilentModeEnabled();
    setState(() {
      _isSilent = isSilent;
    });
  }

  @override
  void initState() {
    super.initState();
    _checkSilentMode();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Silent Mode Checker')),
      body: Center(
        child: Text(
          _isSilent == null
              ? 'Checking...'
              : _isSilent!
                  ? 'Silent Mode is ON'
                  : 'Silent Mode is OFF',
          style: TextStyle(fontSize: 24),
        ),
      ),
    );
  }
}

Android Setup

  1. Open android/app/src/main/kotlin/com/example/silent_status/MainActivity.kt and add the following:
import android.content.Context
import android.media.AudioManager
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugin.common.MethodChannel

class MainActivity : FlutterActivity() {
    private val CHANNEL = "com.example.silent_mode"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
            .setMethodCallHandler { call, result ->
                if (call.method == "isSilentModeEnabled") {
                    val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
                    val isSilent = audioManager.ringerMode == AudioManager.RINGER_MODE_SILENT
                    result.success(isSilent)
                } else {
                    result.notImplemented()
                }
            }
    }
}
  1. Clean and rebuild the project:
flutter clean
flutter pub get

iOS Setup

  1. Open your ios/Podfile and ensure it has:
target 'Runner' do
  use_frameworks!
  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end
  1. In ios/Classes/SilentStatusPlugin.swift, add:
import Flutter
import UIKit
import AVFoundation

public class SilentStatusPlugin: NSObject, FlutterPlugin {
    public static func register(with registrar: FlutterPluginRegistrar) {
        let channel = FlutterMethodChannel(name: "com.example.silent_mode", binaryMessenger: registrar.messenger())
        let instance = SilentStatusPlugin()
        registrar.addMethodCallDelegate(instance, channel: channel)
    }

    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        if call.method == "isSilentModeEnabled" {
            let audioSession = AVAudioSession.sharedInstance()
            let isSilent = audioSession.outputVolume == 0
            result(isSilent)
        } else {
            result(FlutterMethodNotImplemented)
        }
    }
}
  1. Run the following commands:
cd ios
pod install
cd ..
flutter run

Common Issues & Solutions

MissingPluginException

If you encounter the error:

Unhandled Exception: MissingPluginException(No implementation found for method isSilentModeEnabled on channel com.example.silent_mode)

Solution:

  • Run flutter clean and flutter pub get.
  • Ensure GeneratedPluginRegistrant is added in MainActivity and AppDelegate.

Xcode Error - SilentStatusPlugin Not Found

Solution:

  • Ensure SilentStatusPlugin.swift is correctly created and registered.
  • Add use_frameworks! in your Podfile.

License

This project is licensed under the MIT License.