ocarina 0.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 80

Ocarina #

Ocarina is a simple and easy to usade audio package for Flutter. Its goal is to support audio play from local file (from assets, or filesystem), And to support it across all platforms that Flutter runs.

Right now, we only support mobile (Android and iOS) and will eventually supporting Web and Desktop (Linux, MacOS and Windows).

How to use #

Using a file on your assets

final player = OcarinaPlayer(
  asset: 'assets/Loop-Menu.wav',
  loop: true,
  volume: 0.8,
);

await player.load();

Using a file on the device filesystem

final player = OcarinaPlayer(
  filePath: '/SomeWhere/On/The/Device/Loop-Menu.wav',
  loop: true,
  volume: 0.8,
);

await player.load();

Docs #

List of all available methods on the player instance

play

Starts playing

pause

Pauses playback

resume

Resume when playback if it was previously paused

stop

Stops the playback, it can be started again by calling play again.

seek(Duration)

Moves the playback postion to the passed Duration

updateVolume(double)

Updates the volume, must be a value between 0 and 1

dispose

Clears the loaded resources in memory, to use the instance again a subsequent call on the load method is required

0.0.2 #

  • Adding isLoaded method
  • Adding dispose method
  • Fixing iOS issues

0.0.1 #

  • Adding initial implementation

example/lib/main.dart

import 'dart:io';
import 'dart:async';

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

import 'package:ocarina/ocarina.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Ocarina Example'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

const staticFileUrl = 'https://luan.xyz/files/audio/ambient_c_motion.mp3';

class _MyHomePageState extends State<MyHomePage> {
  OcarinaPlayer _player;
  String _localFilePath;
  bool _loop = true;
  bool _fetchingFile = false;

  Future _loadFile() async {
    final bytes = await readBytes(staticFileUrl);
    final dir = await getApplicationDocumentsDirectory();
    final file = File('${dir.path}/audio.mp3');

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: (_player != null
            ? PlayerWidget(
                player: _player,
                onBack: () {
                  setState(() {
                    _localFilePath = null;
                    _fetchingFile = false;
                    _player = null;
                  });
                })
            : Column(
                children: [
                  RaisedButton(
                      child: Text(_loop ? "Loop mode" : "Single play mode"),
                      onPressed: () async {
                        setState(() {
                          _loop = !_loop;
                        });
                      }),
                  RaisedButton(
                      child: Text("Play asset audio"),
                      onPressed: () async {
                        final player = OcarinaPlayer(
                            asset: 'assets/Loop-Menu.wav', loop: _loop);

                        setState(() {
                          _player = player;
                        });
                      }),
                  RaisedButton(
                      child: Text(_fetchingFile
                          ? "Fetching file..."
                          : "Download file to Device, and play it"),
                      onPressed: () async {
                        if (_fetchingFile) return;
                        setState(() {
                          _fetchingFile = true;
                        });
                        await _loadFile();

                        final player = OcarinaPlayer(filePath: _localFilePath);
                        await player.load();

                        setState(() {
                          _player = player;
                        });
                      })
                ],
              )),
      ),
    );
  }
}

class PlayerWidget extends StatelessWidget {
  final OcarinaPlayer player;
  final VoidCallback onBack;

  PlayerWidget({this.player, this.onBack});

  @override
  Widget build(_) {
    return FutureBuilder(
        future: player.load(),
        builder: (ctx, snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.waiting:
            case ConnectionState.none:
            case ConnectionState.active:
              return Text("Loading player");
            case ConnectionState.done:
              if (snapshot.hasError) {
                return Text("Error loading player");
              }
              return Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    RaisedButton(
                        child: Text("Play"),
                        onPressed: () async {
                          await player.play();
                        }),
                    RaisedButton(
                        child: Text("Stop"),
                        onPressed: () {
                          player.stop();
                        }),
                    RaisedButton(
                        child: Text("Pause"),
                        onPressed: () {
                          player.pause();
                        }),
                    RaisedButton(
                        child: Text("Resume"),
                        onPressed: () {
                          player.resume();
                        }),
                    RaisedButton(
                        child: Text("Seek to 5 secs"),
                        onPressed: () {
                          player.seek(Duration(seconds: 5));
                        }),
                    Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                      Text("Volume"),
                      RaisedButton(
                          child: Text("0.2"),
                          onPressed: () {
                            player.updateVolume(0.2);
                          }),
                      RaisedButton(
                          child: Text("0.5"),
                          onPressed: () {
                            player.updateVolume(0.5);
                          }),
                      RaisedButton(
                          child: Text("1.0"),
                          onPressed: () {
                            player.updateVolume(1.0);
                          }),
                    ]),
                    RaisedButton(
                        child: Text("Dispose"),
                        onPressed: () async {
                          await player.dispose();
                        }),
                    RaisedButton(
                        child: Text("Go Back"),
                        onPressed: () async {
                          onBack?.call();
                        }),
                  ]);
          }
          return Container();
        });
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  ocarina: ^0.0.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:ocarina/ocarina.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
63
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
80
Learn more about scoring.

We analyzed this package on Jul 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because:

  • package:ocarina/ocarina.dart that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because:

  • package:ocarina/ocarina.dart that declares support for platforms: android, ios

Package does not support Flutter platform web

Because:

  • package:ocarina/ocarina.dart that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because:

  • package:ocarina/ocarina.dart that declares support for platforms: android, ios

Package not compatible with SDK dart

Because:

  • ocarina that is a package requiring null.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test