How To Add Plugin Ringtone Using Flutter Android App

admin_img Posted By Bajarangi soft , Posted On 13-10-2020

I'm trying to play ringtones (and specifically a device's default one) with Flutter. It seems there isn't an flutter internal way to do it so I'm trying to use the audioplayer plugin, using this code:

How To Add Plugin Ringtone Using Flutter Android App

 Plugin Ringtone

Step 1:  We cannot directly remove the time stamp from  Plugin Ringtone but using the intl.dart package we can easily filter the date stamp from time stamp. So open your flutter project’s pubspec.yaml in code

dependencies:
  flutter:
    sdk: flutter
  ringtone: ^0.0.2


Step 2: After done saving the pubspec.yaml file, Open your flutter project root folder in Command Prompt or Terminal and execute flutter pub get command. 

flutter pub get

Step 3: Open your project’s main.dart file and import material.dart and ringtone: ^0.0.2. dart package.

Complete Code For Plugin Ringtone In Flutter
main.dart
import 'package:flutter/material.dart';
import 'package:ringtone/ringtone.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      home: new Scaffold(
        backgroundColor: Colors.black,
        appBar: new AppBar(
          centerTitle: true,
          backgroundColor: Colors.green,
          title: const Text('Plugin ringtone app'),
        ),
        body: new Center(
          child: new Text('Ringtone',style: TextStyle(fontSize: 30,color: Colors.white),),
        ),
        persistentFooterButtons: <Widget>[
          new FlatButton(
              onPressed: () {
                Ringtone.stop();
              },
              child: const Text('Stop', style: TextStyle(color: Colors.green,fontSize: 20),)
          ),
          new FlatButton(
              onPressed: () {
                Ringtone.play();
              },
              child: const Text('Play',style: TextStyle(color: Colors.green,fontSize: 20),)
          ),
        ],
      ),
    );
  }
}

Related Post