From 6895312218a90eb380b2dd1267043f3a0755c265 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Thu, 13 Aug 2015 14:12:47 -0700 Subject: [PATCH 1/6] Removes the ability to jump to different positions in sound effects (this isn't supported on Android) --- examples/game/lib/sound.dart | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/examples/game/lib/sound.dart b/examples/game/lib/sound.dart index 51c974043d..73f253a902 100644 --- a/examples/game/lib/sound.dart +++ b/examples/game/lib/sound.dart @@ -23,7 +23,6 @@ class SoundStream { this.sound, this.tag, this.loop, - this.time, this.volume, this.pitch, this.pan, @@ -34,7 +33,6 @@ class SoundStream { SoundEffect sound; bool playing = false; bool loop = false; - double time = 0.0; double volume = 1.0; double pitch = 1.0; double pan = 0.0; @@ -94,7 +92,6 @@ class SoundPool { double volume = 1.0, double pitch = 1.0, double pan = 0.0, - double startTime = 0.0, SoundCompleteCallback callback = null]) { // Create new PlayingSound object @@ -102,7 +99,6 @@ class SoundPool { sound, tag, loop, - startTime, volume, pitch, pan, @@ -117,7 +113,7 @@ class SoundPool { // Prepare sound, then play it _prepare(playingSound).then((_) { - playingSound._player.ptr.seekTo((startTime * 1000.0).toInt()); + playingSound._player.ptr.seekTo(0); playingSound._player.ptr.start(); }); @@ -126,7 +122,7 @@ class SoundPool { } else { // Reuse player playingSound._player = _mediaPlayers[sound]; - playingSound._player.ptr.seekTo((startTime * 1000.0).toInt()); + playingSound._player.ptr.seekTo(0); playingSound._player.ptr.start(); } From fa82f1a6e16ff31d37c2c125afab43ecd3d02ddd Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Thu, 13 Aug 2015 14:17:09 -0700 Subject: [PATCH 2/6] Refactors names of sound effect classes --- examples/game/lib/game_demo_world.dart | 2 +- examples/game/lib/sound.dart | 32 +++++++++++++------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/examples/game/lib/game_demo_world.dart b/examples/game/lib/game_demo_world.dart index cc683e67f1..6d0e6e5fde 100644 --- a/examples/game/lib/game_demo_world.dart +++ b/examples/game/lib/game_demo_world.dart @@ -30,7 +30,7 @@ class GameDemoWorld extends NodeWithSize { SpriteSheet _spriteSheetUI; Map _sounds; - SoundPool _soundPool = SoundPool.sharedInstance(); + SoundEffectPlayer _soundPool = SoundEffectPlayer.sharedInstance(); Navigator _navigator; diff --git a/examples/game/lib/sound.dart b/examples/game/lib/sound.dart index 73f253a902..8079869668 100644 --- a/examples/game/lib/sound.dart +++ b/examples/game/lib/sound.dart @@ -18,8 +18,8 @@ class SoundEffect { Object _data; } -class SoundStream { - SoundStream( +class SoundEffectStream { + SoundEffectStream( this.sound, this.tag, this.loop, @@ -45,29 +45,29 @@ class SoundStream { MediaPlayerProxy _player; } -SoundPool _sharedSoundPool; +SoundEffectPlayer _sharedSoundPool; -class SoundPool { +class SoundEffectPlayer { - static SoundPool sharedInstance() { + static SoundEffectPlayer sharedInstance() { if (_sharedSoundPool == null) { - _sharedSoundPool = new SoundPool(); + _sharedSoundPool = new SoundEffectPlayer(); } return _sharedSoundPool; } - SoundPool() { + SoundEffectPlayer() { _mediaService = new MediaServiceProxy.unbound(); shell.requestService(null, _mediaService); } MediaServiceProxy _mediaService; - List _playingSounds = []; + List _playingSounds = []; // TODO: This should no longer be needed when moving to SoundPool backing Map _mediaPlayers = {}; - Future _prepare(SoundStream playingSound) async { + Future _prepare(SoundEffectStream playingSound) async { await playingSound._player.ptr.prepare(playingSound.sound._data); } @@ -85,7 +85,7 @@ class SoundPool { // TODO: Add paused property (should pause playback of all sounds) bool paused; - SoundStream play( + SoundEffectStream play( SoundEffect sound, [Object tag, bool loop = false, @@ -95,7 +95,7 @@ class SoundPool { SoundCompleteCallback callback = null]) { // Create new PlayingSound object - SoundStream playingSound = new SoundStream( + SoundEffectStream playingSound = new SoundEffectStream( sound, tag, loop, @@ -131,7 +131,7 @@ class SoundPool { void stop(Object tag) { for (int i = _playingSounds.length; i >= 0; i--) { - SoundStream playingSound = _playingSounds[i]; + SoundEffectStream playingSound = _playingSounds[i]; if (playingSound.tag == tag) { playingSound._player.ptr.pause(); _playingSounds.removeAt(i); @@ -139,9 +139,9 @@ class SoundPool { } } - List playingSoundsForTag(Object tag) { - List list = []; - for (SoundStream playingSound in _playingSounds) { + List playingSoundsForTag(Object tag) { + List list = []; + for (SoundEffectStream playingSound in _playingSounds) { if (playingSound.tag == tag) { list.add(playingSound); } @@ -150,7 +150,7 @@ class SoundPool { } void stopAll() { - for (SoundStream playingSound in _playingSounds) { + for (SoundEffectStream playingSound in _playingSounds) { playingSound._player.ptr.pause(); } _playingSounds = []; From 3d31273e65e2c89260c9450b4512e91300e1efef Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Thu, 13 Aug 2015 15:48:13 -0700 Subject: [PATCH 3/6] Rename SoundCompleteCallback to SoundEffectStreamCallback --- examples/game/lib/sound.dart | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/game/lib/sound.dart b/examples/game/lib/sound.dart index 8079869668..5794837d7a 100644 --- a/examples/game/lib/sound.dart +++ b/examples/game/lib/sound.dart @@ -3,7 +3,7 @@ part of sprites; // TODO: The sound effects should probably use Android's SoundPool instead of // MediaPlayer as it is more efficient and flexible for playing back sound effects -typedef void SoundCompleteCallback(); +typedef void SoundEffectStreamCallback(SoundEffectStream); class SoundEffect { SoundEffect(this._url); @@ -26,7 +26,7 @@ class SoundEffectStream { this.volume, this.pitch, this.pan, - this.callback + this.onSoundComplete ); // TODO: Make these properties work @@ -40,20 +40,20 @@ class SoundEffectStream { // TODO: Implement completion callback. On completion, sounds should // also be removed from the list of playing sounds. - SoundCompleteCallback callback; + SoundEffectStreamCallback onSoundComplete; MediaPlayerProxy _player; } -SoundEffectPlayer _sharedSoundPool; +SoundEffectPlayer _sharedSoundEffectPlayer; class SoundEffectPlayer { static SoundEffectPlayer sharedInstance() { - if (_sharedSoundPool == null) { - _sharedSoundPool = new SoundEffectPlayer(); + if (_sharedSoundEffectPlayer == null) { + _sharedSoundEffectPlayer = new SoundEffectPlayer(); } - return _sharedSoundPool; + return _sharedSoundEffectPlayer; } SoundEffectPlayer() { @@ -92,7 +92,7 @@ class SoundEffectPlayer { double volume = 1.0, double pitch = 1.0, double pan = 0.0, - SoundCompleteCallback callback = null]) { + SoundEffectStreamCallback callback = null]) { // Create new PlayingSound object SoundEffectStream playingSound = new SoundEffectStream( From a750da56065327bd61422c9e716464d80b353939 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Thu, 13 Aug 2015 15:49:00 -0700 Subject: [PATCH 4/6] Adds a SoundTrackPlayer class --- examples/game/lib/sound.dart | 73 ++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/examples/game/lib/sound.dart b/examples/game/lib/sound.dart index 5794837d7a..4856a369e1 100644 --- a/examples/game/lib/sound.dart +++ b/examples/game/lib/sound.dart @@ -156,3 +156,76 @@ class SoundEffectPlayer { _playingSounds = []; } } + +typedef void SoundTrackCallback(SoundTrack); +typedef void SoundTrackBufferingCallback(SoundTrack, int); + +class SoundTrack { + MediaPlayerProxy _player; + + SoundTrackCallback onSoundComplete; + SoundTrackCallback onSeekComplete; + SoundTrackBufferingCallback onBufferingUpdate; + bool loop; + double time; +} + +SoundTrackPlayer _sharedSoundTrackPlayer; + +class SoundTrackPlayer { + List _soundTracks = []; + + static sharedInstance() { + if (_sharedSoundTrackPlayer == null) { + _sharedSoundTrackPlayer = new SoundTrackPlayer(); + } + return _sharedSoundTrackPlayer; + } + + SoundTrackPlayer() { + _mediaService = new MediaServiceProxy.unbound(); + shell.requestService(null, _mediaService); + } + + MediaServiceProxy _mediaService; + + Future load(String url) async { + // Create media player + SoundTrack soundTrack = new SoundTrack(); + soundTrack._player = new MediaPlayerProxy.unbound(); + _mediaService.ptr.createPlayer(soundTrack._player); + + // Load and prepare + UrlResponse response = await fetchUrl(url); + await soundTrack._player.ptr.prepare(response.body); + + return soundTrack; + } + + void unload(SoundTrack soundTrack) { + stop(soundTrack); + _soundTracks.remove(soundTrack); + } + + void play( + SoundTrack soundTrack, + [bool loop = false, + double volume, + double startTime = 0.0]) { + // TODO: Implement looping & volume + // soundTrack._player.ptr.setLooping(loop); + // soundTrack._player.ptr.setVolume(volume); + soundTrack._player.ptr.seekTo((startTime * 1000.0).toInt()); + soundTrack._player.ptr.start(); + } + + void stop(SoundTrack track) { + track._player.ptr.pause(); + } + + void stopAll() { + for (SoundTrack soundTrack in _soundTracks) { + soundTrack._player.ptr.pause(); + } + } +} From 4530e457106171992bc66df2800e5bff728df4a2 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Thu, 13 Aug 2015 15:59:44 -0700 Subject: [PATCH 5/6] Unifies interfaces for SoundTrackPlayer and SoundEffectPlayer --- examples/game/lib/sound.dart | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/examples/game/lib/sound.dart b/examples/game/lib/sound.dart index 4856a369e1..e502f6b31d 100644 --- a/examples/game/lib/sound.dart +++ b/examples/game/lib/sound.dart @@ -21,7 +21,6 @@ class SoundEffect { class SoundEffectStream { SoundEffectStream( this.sound, - this.tag, this.loop, this.volume, this.pitch, @@ -36,7 +35,6 @@ class SoundEffectStream { double volume = 1.0; double pitch = 1.0; double pan = 0.0; - Object tag; // TODO: Implement completion callback. On completion, sounds should // also be removed from the list of playing sounds. @@ -62,7 +60,7 @@ class SoundEffectPlayer { } MediaServiceProxy _mediaService; - List _playingSounds = []; + List _soundEffectStreams = []; // TODO: This should no longer be needed when moving to SoundPool backing Map _mediaPlayers = {}; @@ -87,8 +85,7 @@ class SoundEffectPlayer { SoundEffectStream play( SoundEffect sound, - [Object tag, - bool loop = false, + [bool loop = false, double volume = 1.0, double pitch = 1.0, double pan = 0.0, @@ -97,7 +94,6 @@ class SoundEffectPlayer { // Create new PlayingSound object SoundEffectStream playingSound = new SoundEffectStream( sound, - tag, loop, volume, pitch, @@ -117,7 +113,7 @@ class SoundEffectPlayer { playingSound._player.ptr.start(); }); - _playingSounds.add(playingSound); + _soundEffectStreams.add(playingSound); _mediaPlayers[sound] = playingSound._player; } else { // Reuse player @@ -129,31 +125,16 @@ class SoundEffectPlayer { return playingSound; } - void stop(Object tag) { - for (int i = _playingSounds.length; i >= 0; i--) { - SoundEffectStream playingSound = _playingSounds[i]; - if (playingSound.tag == tag) { - playingSound._player.ptr.pause(); - _playingSounds.removeAt(i); - } - } - } - - List playingSoundsForTag(Object tag) { - List list = []; - for (SoundEffectStream playingSound in _playingSounds) { - if (playingSound.tag == tag) { - list.add(playingSound); - } - } - return list; + void stop(SoundEffectStream stream) { + stream._player.ptr.pause(); + _soundEffectStreams.remove(stream); } void stopAll() { - for (SoundEffectStream playingSound in _playingSounds) { + for (SoundEffectStream playingSound in _soundEffectStreams) { playingSound._player.ptr.pause(); } - _playingSounds = []; + _soundEffectStreams = []; } } From e17f6b7b067d87e3ee4f635bb51bcf7d3fcc91b0 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Thu, 13 Aug 2015 16:00:06 -0700 Subject: [PATCH 6/6] Adds background music for demo game --- examples/game/lib/main.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/game/lib/main.dart b/examples/game/lib/main.dart index 68ca68dff6..bc78445735 100644 --- a/examples/game/lib/main.dart +++ b/examples/game/lib/main.dart @@ -54,6 +54,10 @@ main() async { await _sounds["explosion"].load(); await _sounds["laser"].load(); + SoundTrackPlayer stPlayer = SoundTrackPlayer.sharedInstance(); + SoundTrack music = await stPlayer.load('https://github.com/slembcke/GalacticGuardian.spritebuilder/raw/GDC/Source/Resources/TempMusic.aac'); + stPlayer.play(music); + runApp(_app); }