audioplayers 0.5.0

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

AudioPlayers

This is a fork of rxlabz's audioplayer, with the difference that it supports playing multiple audios at the same time, and exposes volume controls.

It has the exact same API, but now you can create several new AudioPlayers, each will be handled individually.

Before you could only ever have one instance of the player, otherwise one would override the other.

Just import the fork, which is named audioplayers (mind the 's'), instead of the original:

  dependencies:
    ...
    audioplayers: ^0.5.0

Also, in 0.2.0, I've added the ability to disable logs with:

    AudioPlayer.logEnabled = false;

In 0.3.0, it supports iOS as well (thanks, @feroult)

In 0.4.0, volume control support was added (thanks, @pauldemarco)

In 0.4.1, a bug in iOS regard the seek functionality was fixed (thanks, @cosmok)

In 0.5.0, there was a huge change on Android code to improve performance (thanks, @the4thfloor)

Original Readme

A Flutter audio plugin.

Features

  • [x] Android & iOS

    • [x] play (remote and local file)
    • [x] stop
    • [x] pause
    • [x] seek
    • [x] onComplete
    • [x] onDuration / onCurrentPosition
  • Supported formats

screenshot

Usage

Example

To use this plugin :

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

play, pause , stop, seek

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

// add a isLocal parameter to play a local file
playLocal() async {
  final result = await audioPlayer.play(kUrl);
  if (result == 1) setState(() => playerState = PlayerState.playing);
}


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

stop() async {
  final result = await audioPlayer.stop();
  if (result == 1) setState(() => playerState = PlayerState.stopped);
}

// seek 5 seconds from the beginning
audioPlayer.seek(5.0);

duration, position, complete, error (temporary api)

The Dart part of the plugin listen for platform calls :

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

audioPlayer.setPositionHandler((Duration  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

audioplayers 0.5.0

  • improves Android performance by not calling prepare on the main thread

audioplayers 0.4.1

  • fix seek for iOS

audioplayers 0.4.0

  • volume controls

audioplayers 0.3.0

  • working on iOS (thanks @feroult <3)

audioplayers 0.2.0

  • adding disable log option

audioplayers 0.1.0

  • support for multiple audios simultaneously

0.2.0

  • support for local files

0.1.0

  • update to the current Plugin API
  • move to https://github.com/rxlabz/audioplayer

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 'dart:io';
import 'dart:typed_data';

import 'package:audioplayers/audioplayer.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:path_provider/path_provider.dart';

typedef void OnError(Exception exception);

const kUrl1 = "http://www.rxlabz.com/labz/audio.mp3";
const kUrl2 = "http://www.rxlabz.com/labz/audio2.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> {
  AudioPlayer audioPlayer;

  String localFilePath;

  PlayerState playerState = PlayerState.stopped;

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

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

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

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

    audioPlayer.setErrorHandler((msg) {
      print('audioPlayer error : $msg');
      setState(() {
        playerState = PlayerState.stopped;
      });
    });
  }

  Future _playLocal() async {
    final result = await audioPlayer.play(localFilePath, isLocal: true);
    if (result == 1) setState(() => playerState = PlayerState.playing);
  }

  Future<Uint8List> _loadFileBytes(String url, {OnError onError}) async {
    Uint8List bytes;
    try {
      bytes = await readBytes(url);
    } on ClientException {
      rethrow;
    }
    return bytes;
  }

  Future _loadFile() async {
    final bytes = await _loadFileBytes(kUrl1,
        onError: (Exception exception) => print('_MyHomePageState._loadVideo => exception ${exception}'));

    final dir = await getApplicationDocumentsDirectory();
    final file = new File('${dir.path}/audio.mp3');

    await file.writeAsBytes(bytes);
    if (await file.exists())
      setState(() {
        localFilePath = file.path;
      });
  }

  @override
  Widget build(BuildContext context) {
    return new Center(
      child: new Column(
        children: [
          new _PlayerUiWidget(url: kUrl1),
          new _PlayerUiWidget(url: kUrl2),
          localFilePath != null ? new Text(localFilePath) : new Container(),
          new Row(
            children: [
              new RaisedButton(
                onPressed: () => _loadFile(),
                child: new Text('Download'),
              ),
              new RaisedButton(
                onPressed: () => _playLocal(),
                child: new Text('play local'),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

class _PlayerUiWidget extends StatefulWidget {
  final String url;

  _PlayerUiWidget({@required this.url});

  @override
  State<StatefulWidget> createState() {
    return new _PlayerUiWidgetState(url: url);
  }
}

class _PlayerUiWidgetState extends State<_PlayerUiWidget> {
  String url;
  AudioPlayer _audioPlayer;
  Duration _duration;
  Duration _position;

  PlayerState _playerState = PlayerState.stopped;

  get _isPlaying => _playerState == PlayerState.playing;
  get _isPaused => _playerState == PlayerState.paused;
  get _durationText => _duration?.toString()?.split('.')?.first ??= '';
  get _positionText => _position?.toString()?.split('.')?.first ??= '';

  _PlayerUiWidgetState({@required this.url});

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

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

  @override
  Widget build(BuildContext context) {
    return new Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        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),
            ),
          ],
        ),
      ],
    );
  }

  void _initAudioPlayer() {
    _audioPlayer = new AudioPlayer();

    _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);
      });
    });
  }

  Future _play() async {
    final result = await _audioPlayer.play(url);
    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);
  }
}

1. Depend on it

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


dependencies:
  audioplayers: "^0.5.0"

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:audioplayers/audioplayer.dart';
        
Version Uploaded Documentation Archive
0.5.0 Feb 28, 2018 Go to the documentation of audioplayers 0.5.0 Download audioplayers 0.5.0 archive
0.4.1 Jan 3, 2018 Go to the documentation of audioplayers 0.4.1 Download audioplayers 0.4.1 archive
0.4.0 Nov 18, 2017 Go to the documentation of audioplayers 0.4.0 Download audioplayers 0.4.0 archive
0.3.0 Nov 9, 2017 Go to the documentation of audioplayers 0.3.0 Download audioplayers 0.3.0 archive
0.2.0 Oct 22, 2017 Go to the documentation of audioplayers 0.2.0 Download audioplayers 0.2.0 archive
0.1.0 Oct 22, 2017 Go to the documentation of audioplayers 0.1.0 Download audioplayers 0.1.0 archive

Analysis

We analyzed this package on Apr 23, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.49.0
  • pana: 0.10.6
  • Flutter: 0.3.2

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
88 / 100
Health:
Code health derived from static analysis. [more]
98 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
94
Learn more about scoring.

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Suggestions

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 1 hint.

    Run flutter format to format lib/audioplayer.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <2.0.0
flutter 0.0.0
uuid ^0.5.3 0.5.3 1.0.0
Transitive dependencies
charcode 1.1.1
collection 1.14.6 1.14.9
convert 2.0.1
crypto 2.0.2+1
meta 1.1.2
sky_engine 0.0.99
typed_data 1.1.5
vector_math 2.0.6