Track

Overview

The Track class holds track information as well as the audio media.
Sounds plays audio from a Track and records into a Track.
The Track class has the following constructors:
    Track.fromFile
    Track.fromAsset
    Track.fromBuffer
    Track.fromURL
Each constructor loads audio media from a different source.

fromAsset

To play audio from a project asset copy the file to your assets directory in the root of your Flutter project. (i.e. under the folder that contains your pubspec.yaml.)
assets/sample.acc
Add the asset to the 'assets' section of your pubspec.yaml
1
flutter:
2
assets:
3
- sample.acc
Copied!
Now play the file.
1
/// play the audio with no controls
2
QuickPlay.fromFile('beep.acc');
3
​
4
/// If you need to control/monitor the playback
5
var player = SoundPlayer.noUI();
6
player.onStopped = ({wasUser}) => player.release();
7
player.play(Track.fromFile('sample.aac'));
Copied!
To play a track from an asset use:
1
var track = Track.fromAsset('asset/somfile.aac');
2
QuickPlay.fromTrack(track);
Copied!

fromFile

To play a track from a file use:
1
var track = Track.fromFile('/path/to/media');
2
QuickPlay.fromTrack(track, volume: 1.0);
Copied!
When playing a file the path to the file MUST be a path on your Phone.
A common mistake is to provide a path to a file on your development PC. If you are looking to playback a static audio file ( a file that you will ship with your app ) then you should store it as an asset and use Track.fromAsset.
The fromFile constructor is used if you are dynamically loading audio files into the app. This might be as the result of recording on the app or you might retrieve the audio from a REST API or a URL (consider fromURL in this case).

fromBuffer

To play a track from a buffer use:
1
Uint8List buffer = ....
2
// Load a local audio file and get it as a buffer
3
Uint8List buffer = (await rootBundle.load('assets/samples/audio.mp3'))
4
.buffer
5
.asUint8List();
6
​
7
Track.fromBuffer(buffer)
8
QuickPlay.fromTrack(track, volume: 1.0);
Copied!

fromURL

To play a track from a url use:
1
Track.fromURL('https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3');
2
QuickPlay.fromTrack(track, volume: 1.0);
Copied!

Cacheing remote (URL) audio

When using Track.fromURL Sounds must first fully cache the audio file before it can start playing.
If you are using the SoundPlayerUI widget then the progress indicator will reflect the download progress.
If you need better control over the caching process of audio then I would recommend that you pre-cache the audio and then pass it to Sounds using Track.fromPath.
Sounds provides a convenience class Downloader to help download remote content.

MediaFormat

When recording to or transcoding from a Track class you need to pass the MediaFormat.
When doing playback you must pass an audio file that is encoded using a supported MediaFormat or an MediaFormatNotSupportedException will be thrown.
If you are looking to play an audio file that is encoded using a non native MediaFormat you must transcode the audio file to a supported MediaFormat. See the Sounds_codec package for tools for transcoding audio.

Meta Data

Tracks also allow you to store meta data about the track.
    title
    artist
    album art
You can specify just one field for the Album Art to display on the lock screen. Either :
    albumArtUrl
    albumArtFile
    albumArtFile
If no Album Art field is specified, Sounds will try to display the App icon.
1
var track = Track.fromFile('assets/rock.mp3');
2
track.title = 'Quarantine Jig';
3
track.artist = 'The Jiggy Kids';
Copied!

temporary file

The Track class also provides a convenience method to create a temporary file in the systems temp directory. This can be useful when using the recording api.
You are responsible for deleting the file once done.
The temp file name will be of the form<uuid>.<mediaformat>.
The MediaFormat has no affect on this file except to set the file's extension.
You could still be really stupid and save data in some other format into this file. But you're not that stupid are you :)
1
var file = Track.tempfile(MediaFormat.mp3)
2
print(file);
3
> /tmp/12asf7890a234.mp3
Copied!
Last modified 7mo ago