11-16-2010 11:59 AM
Hey everyone,
Just a quick question for everyone, I read earlier in this forum about audio not working in the simulator, so I'm just wondering if I have this currently implemented correctly:
var player:MediaPlayer = new MediaPlayer("song.mp3", null);
player.prepare();
player.play();Not sure if I'm supposed to use the flash libraries as well, anyways if someone could put me on the right track that'd be greatly appreciated ![]()
Cheers
Solved! Go to Solution.
11-16-2010 01:42 PM - edited 11-16-2010 01:43 PM
I think before you call prepare() you need to add an eventListener to wait for preparation to be complete - and only then call the play() method. Something like this:
player.addEventListener(MediaPlayerEvent.PREPARE_COMPLETE, onPrepareComplete); player.prepare(); private function onPrepareComplete(e:MediaPlayerEvent):void { player.play(); }
That's the theory, at least ... I'm currently trying to get a video to play (no audio) and it's not working either :-)
11-16-2010 03:09 PM
Oh completely forgot about that... thanks ![]()
still the same problem, no sound and getting an I/O error
11-16-2010 03:47 PM
Couple of things to try to help debug this:
Hopefully this will bring something to light.
11-16-2010 03:55 PM
11-16-2010 06:02 PM
I'm going to guess that the MP3 file is not in the directory that you think it is in. If the BB installer is similar to the AIR installer, the MP3 file is probably in the "applicationDirectory". Look at the File class definition at:
http://help.adobe.com/en_US/FlashPlatform/referenc
Your code might look like something like this (psedo code);
var file : File = File.applicationDirectory;
file = file.resolvePath( 'sound.mp3' );
if( file.exists )
{
player = new MediaPlayer( file.nativePath, null );
}If that does not work, try embedding the sound file into the application and then setting it to the MediaPlayer.
If that does not work, also try File.applicationStorageDirectory directory. It should not be there, but you never know.
Also, are you including the MP3 file in the blackberry-airpackager command?
11-16-2010 06:40 PM
Hey John,
I just ended up trying that actually, after reading some flash documentation, mind you still nothing. The MediaPlayer's url show's up as (in it's own sandbox) when I debug it, so that can't be the problem :
url "/mlc/sandboxes/guest/Air.testQWlyICAgICAgICAgICAgICA/app/air/song.mp3"
Which means it has the file loaded, just won't play, and ends up causing that error. I've tried several files now, all giving the same error:
Error #2044: Unhandled error:. text=play:5
I'm beginning to think that this may be a bug...
Anyways here's the rest of my implementation if this helps at all :
public class Player extends Sprite
{
var player:MediaPlayer = new MediaPlayer(null, null);
public function Player()
{
var title:TextField = new TextField();
title.text = "Play";
title.setTextFormat(myFormat);
var playButton:Button = new Button();
playButton.addChild(text);
playButton.addEventListener(MouseEvent.CLICK, playStream);
playButton.x = (stage.stageWidth - playButton.width)/2;
playButton.y = refButton.y - refButton.height;
addChild(playButton);
stage.nativeWindow.visible = true;
}
private function playStream(event:MouseEvent):void
{
var audioFile : File = File.applicationDirectory;
audioFile = audioFile.resolvePath( 'song.mp3' );
if( audioFile.exists )
{
player.url = file.nativePath;
player.addEventListener(MediaPlayerEvent.PREPARE_C OMPLETE, onPrepareComplete);
player.prepare();
}
}
private function onPrepareComplete(e:MediaPlayerEvent):void
{
try
{
streamer.play();
}
catch(e:Error)
{
trace(e.message);
}
}
}
Cheers and thanks again!
11-16-2010 06:48 PM
If someone figures this out, please share, since I am certain many applications will want to play a sound or 2.
BTW, did you try an external URL to an MP3 file (e.g http://www.mydomain.com/sound.mp3)? Then we will know that it is a sound player issue. Might need to look into URLRequest/URLLoader object to know you can start playing.
11-16-2010 06:56 PM
Yea I attempted that, put a couple files up to my own server and tried, same thing tho...
11-17-2010 03:11 AM - edited 11-17-2010 03:12 AM
I think you need to take a look at qnx.media.MediaServiceConnection. That's how you request permission to do audio playback and check if you are granted it. It's also how you listen to system media events like play/pause so you can react to the. I tried to develop an app that played music and I never got MediaServiceConnection to grant me audio access. Despite trying everything I could think of calls to hasAudioService always returned false. I concluded it wasn't implemented yet, but perhaps you'll have better luck.