Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
d89e5e4d
Commit
d89e5e4d
authored
Aug 13, 2015
by
Viktor Lidholt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #607 from vlidholt/master
Updates to game sound API
parents
bcf18623
e17f6b7b
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
101 additions
and
47 deletions
+101
-47
game_demo_world.dart
examples/game/lib/game_demo_world.dart
+1
-1
main.dart
examples/game/lib/main.dart
+4
-0
sound.dart
examples/game/lib/sound.dart
+96
-46
No files found.
examples/game/lib/game_demo_world.dart
View file @
d89e5e4d
...
...
@@ -30,7 +30,7 @@ class GameDemoWorld extends NodeWithSize {
SpriteSheet
_spriteSheetUI
;
Map
<
String
,
SoundEffect
>
_sounds
;
Sound
Pool
_soundPool
=
SoundPool
.
sharedInstance
();
Sound
EffectPlayer
_soundPool
=
SoundEffectPlayer
.
sharedInstance
();
Navigator
_navigator
;
...
...
examples/game/lib/main.dart
View file @
d89e5e4d
...
...
@@ -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
);
}
...
...
examples/game/lib/sound.dart
View file @
d89e5e4d
...
...
@@ -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
Sound
CompleteCallback
(
);
typedef
void
Sound
EffectStreamCallback
(
SoundEffectStream
);
class
SoundEffect
{
SoundEffect
(
this
.
_url
);
...
...
@@ -18,58 +18,54 @@ class SoundEffect {
Object
_data
;
}
class
SoundStream
{
SoundStream
(
class
Sound
Effect
Stream
{
Sound
Effect
Stream
(
this
.
sound
,
this
.
tag
,
this
.
loop
,
this
.
time
,
this
.
volume
,
this
.
pitch
,
this
.
pan
,
this
.
callback
this
.
onSoundComplete
);
// TODO: Make these properties work
SoundEffect
sound
;
bool
playing
=
false
;
bool
loop
=
false
;
double
time
=
0.0
;
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.
Sound
CompleteCallback
callback
;
Sound
EffectStreamCallback
onSoundComplete
;
MediaPlayerProxy
_player
;
}
Sound
Pool
_sharedSoundPool
;
Sound
EffectPlayer
_sharedSoundEffectPlayer
;
class
Sound
Pool
{
class
Sound
EffectPlayer
{
static
Sound
Pool
sharedInstance
()
{
if
(
_sharedSound
Pool
==
null
)
{
_sharedSound
Pool
=
new
SoundPool
();
static
Sound
EffectPlayer
sharedInstance
()
{
if
(
_sharedSound
EffectPlayer
==
null
)
{
_sharedSound
EffectPlayer
=
new
SoundEffectPlayer
();
}
return
_sharedSound
Pool
;
return
_sharedSound
EffectPlayer
;
}
Sound
Pool
()
{
Sound
EffectPlayer
()
{
_mediaService
=
new
MediaServiceProxy
.
unbound
();
shell
.
requestService
(
null
,
_mediaService
);
}
MediaServiceProxy
_mediaService
;
List
<
Sound
Stream
>
_playingSound
s
=
[];
List
<
Sound
EffectStream
>
_soundEffectStream
s
=
[];
// TODO: This should no longer be needed when moving to SoundPool backing
Map
<
SoundEffect
,
MediaPlayerProxy
>
_mediaPlayers
=
{};
Future
_prepare
(
SoundStream
playingSound
)
async
{
Future
_prepare
(
Sound
Effect
Stream
playingSound
)
async
{
await
playingSound
.
_player
.
ptr
.
prepare
(
playingSound
.
sound
.
_data
);
}
...
...
@@ -87,22 +83,18 @@ class SoundPool {
// TODO: Add paused property (should pause playback of all sounds)
bool
paused
;
SoundStream
play
(
Sound
Effect
Stream
play
(
SoundEffect
sound
,
[
Object
tag
,
bool
loop
=
false
,
[
bool
loop
=
false
,
double
volume
=
1.0
,
double
pitch
=
1.0
,
double
pan
=
0.0
,
double
startTime
=
0.0
,
SoundCompleteCallback
callback
=
null
])
{
SoundEffectStreamCallback
callback
=
null
])
{
// Create new PlayingSound object
Sound
Stream
playingSound
=
new
Sound
Stream
(
Sound
EffectStream
playingSound
=
new
SoundEffect
Stream
(
sound
,
tag
,
loop
,
startTime
,
volume
,
pitch
,
pan
,
...
...
@@ -117,46 +109,104 @@ 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
();
});
_
playingSound
s
.
add
(
playingSound
);
_
soundEffectStream
s
.
add
(
playingSound
);
_mediaPlayers
[
sound
]
=
playingSound
.
_player
;
}
else
{
// Reuse player
playingSound
.
_player
=
_mediaPlayers
[
sound
];
playingSound
.
_player
.
ptr
.
seekTo
(
(
startTime
*
1000.0
).
toInt
()
);
playingSound
.
_player
.
ptr
.
seekTo
(
0
);
playingSound
.
_player
.
ptr
.
start
();
}
return
playingSound
;
}
void
stop
(
Object
tag
)
{
for
(
int
i
=
_playingSounds
.
length
;
i
>=
0
;
i
--)
{
SoundStream
playingSound
=
_playingSounds
[
i
];
if
(
playingSound
.
tag
==
tag
)
{
void
stop
(
SoundEffectStream
stream
)
{
stream
.
_player
.
ptr
.
pause
();
_soundEffectStreams
.
remove
(
stream
);
}
void
stopAll
()
{
for
(
SoundEffectStream
playingSound
in
_soundEffectStreams
)
{
playingSound
.
_player
.
ptr
.
pause
();
_playingSounds
.
removeAt
(
i
);
}
_soundEffectStreams
=
[];
}
}
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
<
SoundTrack
>
_soundTracks
=
[];
static
sharedInstance
()
{
if
(
_sharedSoundTrackPlayer
==
null
)
{
_sharedSoundTrackPlayer
=
new
SoundTrackPlayer
();
}
return
_sharedSoundTrackPlayer
;
}
SoundTrackPlayer
()
{
_mediaService
=
new
MediaServiceProxy
.
unbound
();
shell
.
requestService
(
null
,
_mediaService
);
}
List
<
SoundStream
>
playingSoundsForTag
(
Object
tag
)
{
List
<
SoundStream
>
list
=
[];
for
(
SoundStream
playingSound
in
_playingSounds
)
{
if
(
playingSound
.
tag
==
tag
)
{
list
.
add
(
playingSound
);
MediaServiceProxy
_mediaService
;
Future
<
SoundTrack
>
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
();
}
return
list
;
void
stop
(
SoundTrack
track
)
{
track
.
_player
.
ptr
.
pause
();
}
void
stopAll
()
{
for
(
Sound
Stream
playingSound
in
_playingSound
s
)
{
playingSound
.
_player
.
ptr
.
pause
();
for
(
Sound
Track
soundTrack
in
_soundTrack
s
)
{
soundTrack
.
_player
.
ptr
.
pause
();
}
_playingSounds
=
[];
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment