Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Posts: 73
Registered: ‎11-16-2010
My Device: Playbook
Accepted Solution

Playing an Audio File with MediaPlayer

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 :smileyhappy:

 

Cheers

 

----------------------------
Let's turn things up...
Developer
Posts: 194
Registered: ‎11-12-2010
My Device: Not Specified

Re: Playing an Audio File with MediaPlayer

[ Edited ]

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 :-)

 

Developer
Posts: 73
Registered: ‎11-16-2010
My Device: Playbook

Re: Playing an Audio File with MediaPlayer

Oh completely forgot about that... thanks :smileyhappy:

 

still the same problem, no sound and getting an I/O error

----------------------------
Let's turn things up...
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: Playing an Audio File with MediaPlayer

Couple of things to try to help debug this:

  • How is the file placed in relation to the application?  Same directory as the .SWF?
  • Does it work as a basic AIR application?
  • Have you tried embedding the audio file and trying that to narrow down if it is the player or the file location?
  • Does the audio file work in another desktop audio player?
  • If it works as an AIR application and not as a BB application,how are you getting the file onto the simulator (or the installer)?

 

Hopefully this will bring something to light.

Developer
Posts: 73
Registered: ‎11-16-2010
My Device: Playbook

Re: Playing an Audio File with MediaPlayer

Hey jtegen I simply added the mp3 (works fine with other players) in the root dir with the swf and added it to the project. I'd test it as an Air application however MediaPlayer is part of the qnx backend, and therefore won't run :/

As far as getting it onto the simulator I'm just packaging it up into it's .bar and installing it through flash builder, I know for a fact the mp3 is also in the .bar, but it still gives me a 2044 error :/
----------------------------
Let's turn things up...
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: Playing an Audio File with MediaPlayer

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/reference/actionscript/3/flash/filesystem/File.html

 

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?

Developer
Posts: 73
Registered: ‎11-16-2010
My Device: Playbook

Re: Playing an Audio File with MediaPlayer

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_COMPLETE, onPrepareComplete);
    player.prepare();
  }
			
}

private function onPrepareComplete(e:MediaPlayerEvent):void
{
  try 
  {
    streamer.play();
  }
  
  catch(e:Error)
  {
    trace(e.message);
  }
}

}

 

Cheers and thanks again!

 

 

----------------------------
Let's turn things up...
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: Playing an Audio File with MediaPlayer

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.

Developer
Posts: 73
Registered: ‎11-16-2010
My Device: Playbook

Re: Playing an Audio File with MediaPlayer

Yea I attempted that, put a couple files up to my own server and tried, same thing tho...

----------------------------
Let's turn things up...
Developer
Posts: 137
Registered: ‎10-26-2010
My Device: Bold 9900 + PlayBook
My Carrier: Rogers

Re: Playing an Audio File with MediaPlayer

[ Edited ]

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.

--------
Taylor Byrnes