BASIC AUDIO PLAYER  USING CROSS-PLATFORM FRAMEWORK FLUTTER.

BASIC AUDIO PLAYER USING CROSS-PLATFORM FRAMEWORK FLUTTER.

I have created a basic flutter app in which we can play audio.

Here I have used Assets for playing Audio.

Let's look into our project.

First, we want a project where we will be writing our code.

For this, we will use the command

flutter create Project_Name
No alt text provided for this image

This command will create project with the given Name with all the required files.

Now, we will directly jump into our code section.

first delete all the code writtern in main.dart file under lib folder. we will create things from very zero.

After that create a folder inside lib folder and create file with extension .dart. This is optional. This is just easiest way to manage our code and files.

Now, First write code into main.dart file.

import 'package:audioapp/interface/audiofile.dart';
import 'package:flutter/material.dart';



void main(List<String> args) {
  runApp(MyApp());
}


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return myapp();
  }
}

Here we will be calling function to run our app.

Now, we will write a file that is created inside the folder in the lib folder.

import 'package:audioplayers/audio_cache.dart';
import 'package:flutter/material.dart';
import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';
import 'package:audioplayers/audioplayers.dart';


AudioPlayer newPlayer = new AudioPlayer();
AudioCache audio = new AudioCache(fixedPlayer: newPlayer);
bool play = false;
bool stop = true;
playAudio() {
  if (play == false || stop == true) {
    audio.play("ab.mp3"); 
    play = true;
    stop = false;
  }
}


pauseAudio() {
  if (play == true) {
    newPlayer.pause();
    play = false;
  }
}


stopAudio() {
  if (play == true && stop == false) {
    newPlayer.stop();
    play = false;
    stop = true;
  }
}


myapp() {
  FlutterStatusbarcolor.setStatusBarColor(Colors.orangeAccent);
  FlutterStatusbarcolor.setNavigationBarColor(Colors.black);


  var body = Scaffold(
    appBar: AppBar(
      title: Text("FIRST TASK"),
      backgroundColor: Colors.blueGrey,
    ),
    backgroundColor: Colors.pinkAccent.shade200,
    body: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Container(
          width: 400,
          height: 400,
          child: Image.asset("images/sm.jpg"),
        ),
        Row(
      
          children: <Widget>[
            FlatButton(
              onPressed: playAudio,
              child: Icon(Icons.play_arrow),
            ),
            FlatButton(
              onPressed: pauseAudio,
              child: Icon(Icons.pause),
            ),
            FlatButton(
              onPressed: stopAudio,
              child: Icon(Icons.stop),
            ),
          ],
        ),
      ],
    ),
  );


  var design = MaterialApp(
    home: body,
    debugShowCheckedModeBanner: false,
  );
  return design;
}

Here, we will define all the required attributes with Play, Pause, and Stop buttons. also, we will use the Scaffold app.

After this, we have to make some changes to the pubspec.yaml file. We have to install some external dart packages. Also, here only we have defined assets.

After this, we will be running our app without debugging it.

It will take 2-3 minutes to build a complete app.


To view or add a comment, sign in

Others also viewed

Explore content categories