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
Contributor
Posts: 13
Registered: ‎07-27-2011
My Device: playbook
My Carrier: none
Accepted Solution

loadBytes fails silently

Hi all,

 

I have an flash/as3 project that prompts the user to choose an mp3 file on its playbook an simply play it.

i have set the file permission in blackberry-tablet.xml

 

It works fine when running on the desktop, but silently fails when running on the device !

So I have set some alerts in my code but can't figure out why it stops at the try / catch statement (see code)

 

Let 's say the bytes length is 300 000 bytes;

the ProgressEvent is triggered twice, once displaying 0 / 300 000, then 0 /300 000.

On the desktop the difference is that the 2nd time it shows 300 000 / 300 000.

 

 

I have no clue why it does not read the bytes array only on the device and why it works fine on my desktop. 

 

Any additional permission to set may be ?

 

Thanks for any advice

Loic

btw,the code that prompts mp3 files and get them as Sound objects comes from here  where it's far more readable. I have just set the loader as a global class variable and changed the event handler so that I can see the intermediate steps. I could not indent properly here:

 

 

	
public class MP3FileReferenceLoader extends EventDispatcher
{
private var mp3Parser:MP3Parser;
private var m:MovieClip;
private var swfBytesLoader;

public function MP3FileReferenceLoader(loader:Loader)
{
	swfBytesLoader = loader;
	mp3Parser=new MP3Parser();
	mp3Parser.addEventListener(Event.COMPLETE,parserCompleteHandler);			
}

public function getSound(fr:FileReference, mc:MovieClip):void
{
m = mc;
	mp3Parser.loadFileRef(fr);
}
private function parserCompleteHandler(ev:Event):void
{
	var parser:MP3Parser=ev.currentTarget as MP3Parser;
	generateSound(parser);
}
private function generateSound(mp3Source:MP3Parser):Boolean
{
	var swfBytes:ByteArray=new ByteArray();
	swfBytes.endian=Endian.LITTLE_ENDIAN;
	for(var i:uint=0;i<SoundClassSwfByteCode.soundClassSwfBytes1.length;++i)
	{
		swfBytes.writeByte(SoundClassSwfByteCode.soundClassSwfBytes1[i]);
	}
			var swfSizePosition:uint=swfBytes.position;
			swfBytes.writeInt(0); //swf size will go here
			for(i=0;i<SoundClassSwfByteCode.soundClassSwfBytes2.length;++i)
			{
				swfBytes.writeByte(SoundClassSwfByteCode.soundClassSwfBytes2[i]);
			}
			var audioSizePosition:uint=swfBytes.position;
			swfBytes.writeInt(0); //audiodatasize+7 to go here
			swfBytes.writeByte(1);
			swfBytes.writeByte(0);
			mp3Source.writeSwfFormatByte(swfBytes);
			
			var sampleSizePosition:uint=swfBytes.position;
			swfBytes.writeInt(0); //number of samples goes here
			
			swfBytes.writeByte(0); //seeksamples
			swfBytes.writeByte(0);
						
			var frameCount:uint=0;
			
			var byteCount:uint=0; //this includes the seeksamples written earlier
			for(;;)
			{
			
				var seg:ByteArraySegment=mp3Source.getNextFrame();
				if(seg==null)break;
				swfBytes.writeBytes(seg.byteArray,seg.start,seg.length);
				byteCount+=seg.length;
				frameCount++;
			}
			if(byteCount==0)
			{
				return false;
			}
			byteCount+=2;

			var currentPos:uint=swfBytes.position;
			swfBytes.position=audioSizePosition;
			swfBytes.writeInt(byteCount+7);
			swfBytes.position=sampleSizePosition;
			swfBytes.writeInt(frameCount*1152);
			swfBytes.position=currentPos;
			//AlertManager.createAlert(m, "boucle for 4..");
			for(i=0;i<SoundClassSwfByteCode.soundClassSwfBytes3.length;++i)
			{
				swfBytes.writeByte(SoundClassSwfByteCode.soundClassSwfBytes3[i]);
			}
			//AlertManager.createAlert(m, "after 4");
			swfBytes.position=swfSizePosition;
			swfBytes.writeInt(swfBytes.length);
			swfBytes.position=0;
			//AlertManager.createAlert(m, "registering event");
			//swfBytesLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,swfCreated);
			configureListeners(swfBytesLoader.contentLoaderInfo);
			AlertManager.createAlert(m, "loading bytes... "+swfBytes.length);
			try {
				
				swfBytesLoader.loadBytes(swfBytes);
			}
			catch (error:Error) {
				AlertManager.createAlert(m, error.getStackTrace());
			}
			AlertManager.createAlert(m, "done..");
			return true;
		}
		private function swfCreated(ev:Event):void
		{
			AlertManager.createAlert(m, "swfCreated begin");
			var loaderInfo:LoaderInfo=ev.currentTarget as LoaderInfo;
			var soundClass:Class=loaderInfo.applicationDomain.getDefinition("SoundClass") as Class;
			var sound:Sound=new soundClass();
			AlertManager.createAlert(m, "dispatching complete");
			dispatchEvent(new MP3SoundEvent(MP3SoundEvent.COMPLETE,sound));
			
		}
        private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(Event.INIT, initHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(Event.UNLOAD, unLoadHandler);
        }		
        private function completeHandler(event:Event):void {
            AlertManager.createAlert(m,"completeHandler: " + event);
            swfCreated(event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
            AlertManager.createAlert(m,"httpStatusHandler: " + event);
        }

        private function initHandler(event:Event):void {
            AlertManager.createAlert(m,"initHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            AlertManager.createAlert(m,"ioErrorHandler: " + event);
        }

        private function openHandler(event:Event):void {
            AlertManager.createAlert(m,"openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            AlertManager.createAlert(m,"progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
        }

        private function unLoadHandler(event:Event):void {
            AlertManager.createAlert(m,"unLoadHandler: " + event);
        }
	}

 

Contributor
Posts: 13
Registered: ‎07-27-2011
My Device: playbook
My Carrier: none

Re: loadBytes fails silently

I have created a simplified project. Here it is:

 

simple mp3 player

 

It is a zip which contains a .fla file + the code for choosing the mp3 file. Once choosen it simply plays it.

 

Will package this as a BAR file and deploy it on my playbook to see it it works.

 

I'll post if it still fails...

 

L

Contributor
Posts: 13
Registered: ‎07-27-2011
My Device: playbook
My Carrier: none

Re: loadBytes fails silently

I have reproduced the problem on the desktop, it copes with security issues (defaults have changed depending on the targeted runtime : air , flash version...)

It is explained here 

 

When I run the AIR file generated with the above example it does not work. When changes are made according to this post it works on the desktop.

I'll try on the playbook this evening and let you know

 

L

 

Contributor
Posts: 13
Registered: ‎07-27-2011
My Device: playbook
My Carrier: none

Re: loadBytes fails silently

it works !  

 

So I hope it helps someone else:

 

I wanted to load mp3 from a FileReference: I used this

 

But some security defaults are not the same in Flash player and AIR so here is how you change the code in the MP3FileReferenceLoader class.

 

Finally I have a self signed AIR app that works on the desktop, an SWF that does NOT work on the desktop, but once package as a BAR file and deployed on the Playbook it works.

 

Great! now I am close to finish this app hopefully,

 

Happy coding !

Loic

 

 

 

 

Highlighted
Retired
Posts: 427
Registered: ‎02-22-2012
My Device: BlackBerry Z10, BlackBerry Dev Alpha C, BlackBerry PlayBook
My Carrier: Bell

Re: loadBytes fails silently

Glad to hear you got it working Smiley Happy

Follow me on Twitter: @dustinmalik
-----------------------------------------------------------------------------------------------------
Keep up to date on BlackBerry development: http://devblog.blackberry.com/