audioplayer 0.1.0+2

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 48

AudioPlayer #

A Flutter audio plugin.

Features #

  • [x] Android & iOS
    • [x] play (remote file)
    • [x] stop
    • [x] pause
    • [x] onComplete
    • [x] onDuration / onCurrentPosition

screenshot

Usage #

Example

To use this plugin :

  dependencies:
    flutter:
      sdk: flutter
    audioplayer:
  • instantiate an AudioPlayer instance
//...
AudioPlayer audioPlugin = new AudioPlayer();
//...

play, pause , stop #

Future play() async {
  final result = await audioPlayer.play(kUrl);
  if (result == 1) setState(() => playerState = PlayerState.playing);
}
  
Future pause() async {
  final result = await audioPlayer.pause();
  if (result == 1) setState(() => playerState = PlayerState.paused);
}

Future stop() async {
  final result = await audioPlayer.stop();
  if (result == 1)
    setState(() {
    playerState = PlayerState.stopped;
    position = new Duration();
  });
}

duration, position, complete, error (temporary api) #

The dart part of the plugin listen for platform calls :

//...
audioPlayer.setDurationHandler((d) => setState(() {
  duration = d;
}));

audioPlayer.setPositionHandler((p) => setState(() {
  position = p;
}));

audioPlayer.setCompletionHandler(() {
  onComplete();
  setState(() {
    position = duration;
  });
});

audioPlayer.setErrorHandler((msg) {
  print('audioPlayer error : $msg');
  setState(() {
    playerState = PlayerState.stopped;
    duration = new Duration(seconds: 0);
    position = new Duration(seconds: 0);
  });
});

iOS #

⚠️ Swift project #

  • this plugin is written in swift, so to use with in a Flutter/ObjC project, you need to convert the project to "Current swift syntax" ( Edit/Convert/current swift syntax)

⚠️ iOS App Transport Security #

By default iOS forbids loading from non-https url. To cancel this restriction edit your .plist and add :

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Getting Started #

For help getting started with Flutter, view our online documentation.

For help on editing plugin code, view the documentation.

Changelog #

0.0.2 #

Separated handlers for position, duration, completion and errors

  • setDurationHandler(TimeChangeHandler handler)

  • setPositionHandler(TimeChangeHandler handler)

  • setCompletionHandler(VoidCallback callback)

  • setErrorHandler(ErrorHandler handler)

  • new typedef

typedef void TimeChangeHandler(Duration duration);
typedef void ErrorHandler(String message);

0.0.1 #

  • first POC :
    • methods : play, pause, stop
    • a globalHandler for position, duration, completion and errors

example/lib/main.dart

import 'dart:async';

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

const kUrl = "http://www.rxlabz.com/labz/audio2.mp3";
const kUrl2 = "http://www.rxlabz.com/labz/audio.mp3";

void main() {
  runApp(new MaterialApp(home: new Scaffold(body: new AudioApp())));
}

enum PlayerState { stopped, playing, paused }

class AudioApp extends StatefulWidget {
  @override
  _AudioAppState createState() => new _AudioAppState();
}

class _AudioAppState extends State<AudioApp> {
  Duration duration;
  Duration position;

  AudioPlayer audioPlayer;

  PlayerState playerState = PlayerState.stopped;

  get isPlaying => playerState == PlayerState.playing;
  get isPaused => playerState == PlayerState.paused;

  get durationText =>
    duration != null ? duration.toString().split('.').first : '';
  get positionText =>
    position != null ? position.toString().split('.').first : '';

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

  void initAudioPlayer() {
    audioPlayer = new AudioPlayer();

    audioPlayer.setDurationHandler((d) => setState(() {
      print('_AudioAppState.initAudioPlayer => d ${d}');
      duration = d;
    }));

    audioPlayer.setPositionHandler((p) => setState(() {
      print('_AudioAppState.initAudioPlayer => p ${p}');
      position = p;
    }));

    audioPlayer.setCompletionHandler(() {
      onComplete();
      setState(() {
        position = duration;
      });
    });

    audioPlayer.setErrorHandler((msg) {
      print('audioPlayer error : $msg');
      setState(() {
        playerState = PlayerState.stopped;
        duration = new Duration(seconds: 0);
        position = new Duration(seconds: 0);
      });
    });
  }

  Future play() async {
    final result = await audioPlayer.play(kUrl);
    if (result == 1) setState(() => playerState = PlayerState.playing);
  }

  Future pause() async {
    final result = await audioPlayer.pause();
    if (result == 1) setState(() => playerState = PlayerState.paused);
  }

  Future stop() async {
    final result = await audioPlayer.stop();
    if (result == 1)
      setState(() {
        playerState = PlayerState.stopped;
        position = new Duration();
      });
  }

  void onComplete() {
    setState(() => playerState = PlayerState.stopped);
  }

  @override
  void dispose() {
    super.dispose();
    audioPlayer.stop();
  }

  @override
  Widget build(BuildContext context) {
    return new Center(
      child: new Material(
        elevation: 2.0,
        color: Colors.grey[200],
        child: new Container(
          padding: new EdgeInsets.all(16.0),
          child: new Column(mainAxisSize: MainAxisSize.min, children: [
            new Row(mainAxisSize: MainAxisSize.min, children: [
              new IconButton(
                onPressed: isPlaying ? null : () => play(),
                iconSize: 64.0,
                icon: new Icon(Icons.play_arrow),
                color: Colors.cyan),
              new IconButton(
                onPressed: isPlaying ? () => pause() : null,
                iconSize: 64.0,
                icon: new Icon(Icons.pause),
                color: Colors.cyan),
              new IconButton(
                onPressed: isPlaying || isPaused ? () => stop() : null,
                iconSize: 64.0,
                icon: new Icon(Icons.stop),
                color: Colors.cyan),
            ]),
            new Row(mainAxisSize: MainAxisSize.min, children: [
              new Padding(
                padding: new EdgeInsets.all(12.0),
                child: new Stack(children: [
                  new CircularProgressIndicator(
                    value: 1.0,
                    valueColor:
                    new AlwaysStoppedAnimation(Colors.grey[300])),
                  new CircularProgressIndicator(
                    value:
                    position != null && position.inMilliseconds > 0
                      ? position.inMilliseconds /
                      duration.inMilliseconds
                      : 0.0,
                    valueColor: new AlwaysStoppedAnimation(Colors.cyan),
                  ),
                ])),
              new Text(
                position != null
                  ? "${positionText ?? ''} / ${durationText ?? ''}"
                  : duration != null ? durationText : '',
                style: new TextStyle(fontSize: 24.0))
            ])
          ]))));
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  audioplayer: ^0.1.0+2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:audioplayer/audioplayer.dart';
  
Version Uploaded Documentation Archive
0.5.2 Nov 18, 2018 Go to the documentation of audioplayer 0.5.2 Download audioplayer 0.5.2 archive
0.5.0 Jun 12, 2018 Go to the documentation of audioplayer 0.5.0 Download audioplayer 0.5.0 archive
0.4.0 Mar 22, 2018 Go to the documentation of audioplayer 0.4.0 Download audioplayer 0.4.0 archive
0.3.0 Oct 23, 2017 Go to the documentation of audioplayer 0.3.0 Download audioplayer 0.3.0 archive
0.2.0 May 26, 2017 Go to the documentation of audioplayer 0.2.0 Download audioplayer 0.2.0 archive
0.1.0+2 May 25, 2017 Go to the documentation of audioplayer 0.1.0+2 Download audioplayer 0.1.0+2 archive
0.1.0+1 May 25, 2017 Go to the documentation of audioplayer 0.1.0+1 Download audioplayer 0.1.0+1 archive
0.1.0 May 25, 2017 Go to the documentation of audioplayer 0.1.0 Download audioplayer 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
96
Health:
Code health derived from static analysis. [more]
--
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
--
Overall:
Weighted score of the above. [more]
48
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://dart.dev/dart-2#migration.

Maintenance issues and suggestions

Make sure dartdoc successfully runs on your package's source files. (-10 points)

Dependencies were not resolved.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <2.0.0