Cannot set AudioElement.currentTime

1,462 views
Skip to first unread message

Marcel Batista

unread,
Apr 26, 2012, 4:38:45 PM4/26/12
to General Dart Discussion
Hey guys, how u doing?

So I'm playing with DART and I want to make a sound to be played and
then rewinded to the beggining so when it's played again it starts
from the beggining. Here's some code as example. Update is called
every frame and when 'enter' is pressed explode() is called. What
happens is that the sound plays the first time but never plays again
when explode() is called and when I print explosionSound.currentTime
it is not set to 0, but to 9223372013568. Even if I try to set it to
any other number in any other time I can't do it.

AudioElement explosionSound;

PlayerShip(): super(getShipAnimSpec())
{
explosionSound = new AudioElement('sfx/explosion.ogg');
}

void update()
{
if(explosionSound.ended)
{
explosionSound.currentTime = 0;
}
}

void explode()
{
explosionSound.play();
}

Bob Nystrom

unread,
Apr 26, 2012, 4:51:23 PM4/26/12
to Marcel Batista, General Dart Discussion
On Thu, Apr 26, 2012 at 1:38 PM, Marcel Batista <batista...@gmail.com> wrote:
Hey guys, how u doing?

Pretty good, you? ;)
 

So I'm playing with DART and I want to make a sound to be played and
then rewinded to the beggining so when it's played again it starts
from the beggining. Here's some code as example. Update is called
every frame and when 'enter' is pressed explode() is called. What
happens is that the sound plays the first time but never plays again
when explode() is called and when I print explosionSound.currentTime
it is not set to 0, but to 9223372013568. Even if I try to set it to
any other number in any other time I can't do it.

Caveat: I know almost nothing about the audio API. But basic questions...
 

AudioElement explosionSound;

PlayerShip(): super(getShipAnimSpec())
{
   explosionSound = new AudioElement('sfx/explosion.ogg');
}

void update()
{
   if(explosionSound.ended)
   {
       explosionSound.currentTime = 0;
   }
}

Have you put some debug prints() in here to make sure that you are actually getting to the code that resets the current time?
Instead of setting it to zero, have you tried setting it to explosionSound.startTime?
 

void explode()
{
    explosionSound.play();
}

Another option would to just always reset the currentTime here before you start playing. That would mean that calling explode() will interrupt an ongoing sound, but that may be what you want anyway.

Cheers!

- bob

Marcel Batista

unread,
Apr 26, 2012, 5:35:41 PM4/26/12
to Bob Nystrom, General Dart Discussion
Yes I tried all that stuff...the ended actually never returns true...other thing I noticed is that the networkState never leaves the 2 (NETWORK_LOADING) state. Ready state is always on 4 (HAVE_ENOUGH_DATA). No idea why it is not finishing the loading but I guess maybe this is why I can't set the currentTime. Any ideas of how to fix this?

2012/4/26 Bob Nystrom <rnys...@google.com>

Marcel Batista

unread,
Apr 26, 2012, 5:39:56 PM4/26/12
to Bob Nystrom, General Dart Discussion
For some reason now the states are 
readyState: 4 ( HAVE_ENOUGH_DATA )
networkState: 1 ( NETWORK_IDLE )

guess it's loading alright now but even so the .ended is never true =(

2012/4/26 Marcel Batista <batista...@gmail.com>

Marcel Batista

unread,
Apr 26, 2012, 5:42:02 PM4/26/12
to Bob Nystrom, General Dart Discussion
the duration attribute is set to infinite as well =|

2012/4/26 Marcel Batista <batista...@gmail.com>

Seth Ladd

unread,
Apr 26, 2012, 8:10:24 PM4/26/12
to Marcel Batista, Bob Nystrom, General Dart Discussion
Hi Marcel,

This is side stepping the issue, but you almost certainly want the Web Audio API. The current <audio> tag (AudioElement) is not built for fine grained control over audio. The new API was built for games and other demanding audio applications.

Here's a good Web Audio API tutorial (JavaScript, but should be easy to extrapolate to Dart) to get started. Now, you may run into a different set of bugs, but at least you'll be on the right path.

Let me know if this is interesting and we can take it from there.

Hope that helps,
Seth

Marcel Batista

unread,
Apr 26, 2012, 8:29:51 PM4/26/12
to Seth Ladd, Bob Nystrom, General Dart Discussion
Thanks, gonna try this right now and answer back! ;D

2012/4/26 Seth Ladd <seth...@google.com>

Marcel Batista

unread,
Apr 26, 2012, 11:41:34 PM4/26/12
to Seth Ladd, Bob Nystrom, General Dart Discussion
Oh GOD. I tried this solution but now the problem is that XMLHttpRequest is bugged. I can't access the response property of it (neither DART:HTML nor DART:DOM classes work). Did anyone ever used XMLHttpRequest response property?

class Sound {
  
  DOM.AudioBuffer buffer;
  String source;
  
  Sound(String this.source)
  {
    loadSound(source);
  }
  
  void loadSound(String url) 
  {
    DOM.XMLHttpRequest request = new DOM.XMLHttpRequest();
    request.open('GET', url, true);
    request.responseType = 'arraybuffer';
 
    request.addEventListener('load', onLoaded);
    
    request.send();
  }
  
  
  void onLoaded(DOM.XMLHttpRequestProgressEvent e)
  {
    DOM.XMLHttpRequest request = e.currentTarget;
    SoundManager manager = new SoundManager();
    manager.onSoundLoaded(request.response);
  }
}

The error: 
Exception: UnsupportedOperationException: [info: ..\bindings\dart\custom\DartXMLHttpRequestCustom.cpp:223]
Stack Trace:  0. Function: 'XMLHttpRequestImplementation.get:response'


2012/4/26 Marcel Batista <batista...@gmail.com>

Seth Ladd

unread,
Apr 27, 2012, 12:14:10 AM4/27/12
to Marcel Batista, Bob Nystrom, General Dart Discussion
Hi Marcel,

Here's an example of Dart w/ Web Audio API (although it's using dart:dom, I sugget you rework it to use dart:html): https://github.com/sethladd/dart-webaudioapi-simple  (also, this was compiled a long time ago, I don't think this code will compile with today's SDK as is, but this should be more than enough to get going)

Let me know how it goes, thanks,
Seth

Seth Ladd

unread,
Apr 27, 2012, 1:37:34 AM4/27/12
to Marcel Batista, Bob Nystrom, General Dart Discussion
Hi Marcel,

I've save you the trouble and just let you know that dart:html doesn't work with Web Audio API yet. I just tried it, and ran into this bug: http://code.google.com/p/dart/issues/detail?id=2517

Please star the bug so you'll get notified when we fix it.

Thanks,
Seth

Marcel Batista

unread,
Apr 27, 2012, 2:26:15 AM4/27/12
to Seth Ladd, Bob Nystrom, General Dart Discussion
Hey Seth!

I noticed that and even though didn't report the error. I'm sorry for that...I though I was just being dumb in some way. 
Anyways here's what I 'found out'.

That XMLHttpRequest.response error happens only if I don't compile the code to javascript and try to run the dart code. If I compile it to javascript it works. Since everything till now worked just fine using the dart code I didn't bother compiling my sources and running the .js instead of the .dart and I don't know how stupid I was for doing it...anyways.

In the end my code (accessing the .response) was right. 

Now the only problem left (for now) is that:

* running the javascript on chrome i get this for wav, mp3 and ogg files:
XMLHttpRequest cannot load file:///C:/Users/Marcel/Documents/Projects/Dart/DartType/dart-type/sfx/explosion.wav. Cross origin requests are only supported for HTTP.
DartTypeLib.dart.js:4979Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101

*running the javascript on chromium: the ogg file triggers the  onSoundDecodeSuccess callback while wav and mp3 trigger  onSoundDecodeFail trigger (se code bellow together with the one in a past e-mail). I'm not sure if the only ogg file is being loaded and there for is the only one successfully decoded or if all three are being successfully loaded but only ogg is successfully decoded. Any thoughts?

class SoundManager
.
.
void onSoundLoaded(DOM.AudioBuffer buffer)
{
    audioContext.decodeAudioData(buffer, onSoundDecodeSuccess, onSoundDecodeFail);
}

Marcel

2012/4/27 Seth Ladd <seth...@google.com>

Seth Ladd

unread,
Apr 27, 2012, 2:33:45 AM4/27/12
to Marcel Batista, Bob Nystrom, General Dart Discussion
Hi Marcel,

Best to err on the side of "Dart has a bug" for now, don't be shy.

Two notes that should help:

- Dartium/Chromium can't handle .mp3. Stick with .ogg for now if you want to play with Dartium. This probably won't change due to licensing issues.

- You'll need to serve your test app from a web server. In a pinch, the following works for me: python -m SimpleHTTPServer  (which makes me think that Dart should ship with a one-line web server :)

Hope that helps,
Seth

John Messerly

unread,
Apr 27, 2012, 2:35:52 AM4/27/12
to Marcel Batista, Seth Ladd, Bob Nystrom, General Dart Discussion
On Thu, Apr 26, 2012 at 11:26 PM, Marcel Batista <batista...@gmail.com> wrote:
That XMLHttpRequest.response error happens only if I don't compile the code to javascript and try to run the dart code. If I compile it to javascript it works. Since everything till now worked just fine using the dart code I didn't bother compiling my sources and running the .js instead of the .dart and I don't know how stupid I was for doing it...anyways.


Yeah, this one sounds like a Dartium bug of some sort. I don't have DartXMLHttpRequestCustom.cpp handy but it sounds like something isn't implemented, or maybe it's the same cross origin error as below but with a bad error message (which is still a bug in Dartium, IMHO).
 
Now the only problem left (for now) is that:

* running the javascript on chrome i get this for wav, mp3 and ogg files:
XMLHttpRequest cannot load file:///C:/Users/Marcel/Documents/Projects/Dart/DartType/dart-type/sfx/explosion.wav. Cross origin requests are only supported for HTTP.
DartTypeLib.dart.js:4979Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101


This error happens because you can't do XHR requests if your page was loaded from the file:/// URL. You need to get the JS running on a localhost server, or you can try starting Chrome with the –allow-file-access-from-files command line argument.

 
*running the javascript on chromium: the ogg file triggers the  onSoundDecodeSuccess callback while wav and mp3 trigger  onSoundDecodeFail trigger (se code bellow together with the one in a past e-mail). I'm not sure if the only ogg file is being loaded and there for is the only one successfully decoded or if all three are being successfully loaded but only ogg is successfully decoded. Any thoughts?


Just guessing, but I bet Chromium doesn't have all of the media codecs that Chrome does for licensing reasons.

- John

Seth Ladd

unread,
Apr 27, 2012, 2:38:00 AM4/27/12
to John Messerly, Marcel Batista, Bob Nystrom, General Dart Discussion, Anton Muhin
+ Anton for DartXMLHttpRequestCustom.cpp and XHR.response in Dartium

John Messerly

unread,
Apr 27, 2012, 2:40:49 AM4/27/12
to Seth Ladd, Marcel Batista, Bob Nystrom, General Dart Discussion, Anton Muhin
It looks like he just fixed it. That was fast! :)

- John

John Messerly

unread,
Apr 27, 2012, 2:42:33 AM4/27/12
to Seth Ladd, Marcel Batista, Bob Nystrom, General Dart Discussion, Anton Muhin
Also, he can see it into the future, since the fix was actually 12 hours ago ... :)

jako

unread,
Apr 27, 2012, 4:02:12 AM4/27/12
to mi...@dartlang.org, Seth Ladd, Marcel Batista, Bob Nystrom, Anton Muhin
The cross Origin problem is by design, and if you dont want to push it on a external/local webserver, you could just copy the url from dartium in debug mode in chrome. (after you compiled to js of course.)

It would be nice if you compile to javascript that chrome starts with the local host instead of just open it with file://.

Marcel Batista

unread,
Apr 27, 2012, 7:38:55 AM4/27/12
to jako, mi...@dartlang.org, Seth Ladd, Bob Nystrom, Anton Muhin
Thank you guys...that fix was fast indeed!! 

2012/4/27 jako <p4j...@googlemail.com>

Anton Muhin

unread,
Apr 27, 2012, 8:02:16 AM4/27/12
to Marcel Batista, jako, mi...@dartlang.org, Seth Ladd, Bob Nystrom
Just a good timing---another guy asked for a fix.

A question: do we have any more bug takeaways from this thread?

yours,
anton.

Seth Ladd

unread,
Apr 27, 2012, 8:06:50 AM4/27/12
to Anton Muhin, Marcel Batista, jako, mi...@dartlang.org, Bob Nystrom
We need to get a constructor for AudioContext into dart:html: http://code.google.com/p/dart/issues/detail?id=2517

Anton Muhin

unread,
Apr 27, 2012, 8:37:17 AM4/27/12
to Seth Ladd, Marcel Batista, jako, mi...@dartlang.org, Bob Nystrom
Thanks, Seth.

yours,
anton.

Marcel Batista

unread,
Apr 27, 2012, 12:06:15 PM4/27/12
to Anton Muhin, Seth Ladd, jako, mi...@dartlang.org, Bob Nystrom
So now I should wait for the next SDK release or is it possible to get the fixed version already?

2012/4/27 Anton Muhin <ant...@google.com>

Seth Ladd

unread,
Apr 27, 2012, 12:09:27 PM4/27/12
to Marcel Batista, Anton Muhin, jako, mi...@dartlang.org, Bob Nystrom
Hi Marcel,

If you've starred the bug, you'll know when it gets fixed (hopefully very soon). Then, pull down a new Editor continuous build which has the daily build of the Dart SDK inside.

thanks,
Seth
Reply all
Reply to author
Forward
0 new messages