SoundPlayerUI

Overview

The SoundPlayerUI provides a Playback widget styled after the HTML 5 audio player.
​
The player displays a loading indicator and allows the user to pause/resume/skip via the progress bar.
You can also pause/resume the player via an api call to SoundPlayerUI's state using a GlobalKey.
The SoundPlayerUI widget allows you to playback audio from multiple sources:
    File
    Asset
    URL
    Buffer

MediaFormat

When using the SoundPlayerUI you MUST pass a Track that has been initialised with a supported MediaFormat.
The Widget needs to obtain the duration of the audio that it is play and that can only be done if we know the MediaFormat of the Widget.
If you pass a Track that wasn't constructed with a MediaFormat then a MediaFormatException will be thrown.
The MediaFormat must also be natively supported by the OS. See MediaFormat for additional details on checking for a supported format.

Example:

1
Track track;
2
​
3
/// global key so we can pause/resume the player via the api.
4
var playerStateKey = GlobalKey<SoundPlayerUIState>();
5
​
6
void initState()
7
{
8
track = Track.fromAsset('assets/rock.mp3', mediaFormat: Mp3MediaFormat());
9
}
10
​
11
Widget build(BuildContext build)
12
{
13
var player = SoundPlayerUI.fromTrack(track, key: playerStateKey);
14
return
15
Column(child: [
16
player,
17
RaisedButton("Pause", onPressed: () => playerState.currentState.pause()),
18
RaisedButton("Resume", onPressed: () => playerState.currentState.resume())
19
]);
20
}
Copied!
Sounds uses Track as the primary method of handing around audio data.
You can also dynamically load a Track when the user clicks the 'Play' button on the SoundPlayerUI widget. This allows you to delay the decision on what Track is going to be played until the user clicks the 'Play' button.
1
Track track;
2
​
3
​
4
void initState()
5
{
6
track = Track.fromAsset('assets/rock.mp3', mediaFormat: Mp3MediaFormat());
7
}
8
​
9
Widget build(BuildContext build)
10
{
11
return SoundPlayerUI.fromLoader((context) => loadTrack());
12
}
13
​
14
Future<Track> loadTrack()
15
{
16
Track track;
17
track = Track.fromAsset('assets/rock.mp3', mediaFormat: Mp3MediaFormat());
18
​
19
track.title = "Asset playback.";
20
track.artist = "By sounds";
21
}
Copied!
Last modified 7mo ago