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
wmatt
Posts: 125
Registered: ‎10-02-2011
My Device: Playbook
My Carrier: none

BB10: Download video for MediaPlayer

[ Edited ]

I'm trying to download a video and play it in the media player. But I'm getting:

Error #2044: Unhandled error:. text=MediaPlayer: Error during -(inputAttach)

 

package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import qnx.fuse.ui.media.VideoDisplay;
	import qnx.media.MediaPlayer;
	
	public class TestMediaPlayer extends Sprite
	{
		public function TestMediaPlayer()
		{
			super();
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			initializeUI();
			downloadVideo();
		}
		
		public var mediaPlayer:MediaPlayer;
		public var videoDisplay:VideoDisplay;
		public var videoFile:File;
		public var urlLoader:URLLoader
		
		private function initializeUI():void
		{
			mediaPlayer = new MediaPlayer();
			videoDisplay = new VideoDisplay();
			videoDisplay.setPosition(0,0);
			videoDisplay.width = stage.stageWidth;
			videoDisplay.height = stage.stageHeight;
			videoDisplay.backgroundColor = 0x000000;
			videoDisplay.allowOverlay = false;
			addChild(videoDisplay);
			mediaPlayer.videoDisplay = videoDisplay;
		}	
		
		private function downloadVideo():void
		{
			var url:String = "http://www.________.com/puppy.mp4";
			var urlRequest:URLRequest = new URLRequest(url);
			urlLoader = new URLLoader();
			urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			urlLoader.addEventListener(Event.COMPLETE, urlLoaderComplete);
			urlLoader.addEventListener(ProgressEvent.PROGRESS, urlLoaderProgress);
			urlLoader.load(urlRequest);
		}
		
		protected function urlLoaderProgress(event:ProgressEvent):void
		{
			trace("url bytesLoaded: " + event.bytesLoaded);
		}
		
		protected function urlLoaderComplete(event:Event):void
		{
			trace("proceed");
			var bytes:ByteArray = urlLoader.data;
			trace("bytes: "+bytes.length);
			videoFile = File.applicationStorageDirectory.resolvePath("puppy.mp4");
			var filestream:FileStream = new FileStream();
			filestream.addEventListener(Event.CLOSE, fileStreamClosed);
			filestream.addEventListener(IOErrorEvent.IO_ERROR, fileIOError);
			filestream.open(videoFile, FileMode.WRITE);
			filestream.writeBytes(bytes);
			filestream.close();
			mediaPlayer.url = videoFile.nativePath;
			mediaPlayer.play();
		}
		
		protected function fileStreamClosed(event:Event):void
		{
			//this event isn't called for some reason
			trace("filestreamClosed");
			mediaPlayer.url = videoFile.nativePath;
			mediaPlayer.play();
		}
		
		protected function fileIOError(event:IOErrorEvent):void
		{
			trace("fileIOError: "+event);
		}
		
	}
}

 

Please use plain text.
Developer
Innovatology
Posts: 1,280
Registered: ‎03-03-2011
My Device: Playbook, Z10, Q10, Z30 with Files & Folders and Orbit of course
My Carrier: Vodafone

Re: BB10: Download video for MediaPlayer

A FileStream opened in synchronous mode doesn't trigger a CLOSE event. It only triggers IOError events. There may be a slight delay while the file is being flushed-on-close, causing your first mediaPlayer.play() to fail because the file is still locked.

 

Use the FileStream.openAsync(...) method instead, and remove mediaPlayer.play() from your urlLoaderComplete function.

 

Did you know you can also just pass an http URL to the MediaPlayer too, and stream it directly from the webserver? Depending in your needs, this may be simpler...

Files & Folders, the unified file & cloud manager for PlayBook and BB10 with SkyDrive, SugarSync, Box, Dropbox, Google Drive, Google Docs. Free 3-day trial! - Jon Webb - Innovatology - Utrecht, Netherlands
Please use plain text.
Developer
wmatt
Posts: 125
Registered: ‎10-02-2011
My Device: Playbook
My Carrier: none

Re: BB10: Download video for MediaPlayer

Thanks I'll try the direct URL method, just want to figure out why this isn't working first. These changes are still giving the same error.

 

package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import qnx.events.MediaPlayerEvent;
	import qnx.fuse.ui.media.VideoDisplay;
	import qnx.media.MediaPlayer;
	
	public class TestMediaPlayer extends Sprite
	{
		public function TestMediaPlayer()
		{
			super();
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			initializeUI();
			downloadVideo();
		}
		
		public var mediaPlayer:MediaPlayer;
		public var videoDisplay:VideoDisplay;
		public var videoFile:File;
		public var urlLoader:URLLoader
		
		private function initializeUI():void
		{
			mediaPlayer = new MediaPlayer();
			videoDisplay = new VideoDisplay();
			videoDisplay.setPosition(0,0);
			videoDisplay.width = stage.stageWidth;
			videoDisplay.height = stage.stageHeight;
			videoDisplay.backgroundColor = 0x000000;
			videoDisplay.allowOverlay = false;
			addChild(videoDisplay);
			mediaPlayer.videoDisplay = videoDisplay;
			mediaPlayer.addEventListener(MediaPlayerEvent.PREPARE_COMPLETE, onPrepareComplete);
		}	
		
		private function downloadVideo():void
		{
			var url:String = "https://dl.dropbox.com/s/pjm8o47vws2x46z/puppy.mp4";
			var urlRequest:URLRequest = new URLRequest(url);
			urlLoader = new URLLoader();
			urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			urlLoader.addEventListener(Event.COMPLETE, urlLoaderComplete);
			urlLoader.addEventListener(ProgressEvent.PROGRESS, urlLoaderProgress);
			urlLoader.load(urlRequest);
		}
		
		protected function urlLoaderProgress(event:ProgressEvent):void
		{
			trace("url bytesLoaded: " + event.bytesLoaded);
		}
		
		protected function urlLoaderComplete(event:Event):void
		{
			trace("proceed");
			var bytes:ByteArray = urlLoader.data;
			trace("bytes: "+bytes.length);
			videoFile = File.applicationStorageDirectory.resolvePath("puppy.mp4");
			var filestream:FileStream = new FileStream();
			filestream.addEventListener(Event.CLOSE, fileStreamClosed);
			filestream.addEventListener(IOErrorEvent.IO_ERROR, fileIOError);
			filestream.openAsync(videoFile, FileMode.WRITE);
			filestream.writeBytes(bytes);
			filestream.close();
		}
		
		protected function fileStreamClosed(event:Event):void
		{
			//this event isn't called for some reason
			trace("filestreamClosed");
			mediaPlayer.url = videoFile.nativePath;
			mediaPlayer.prepare();
		}
		
		protected function fileIOError(event:IOErrorEvent):void
		{
			trace("fileIOError: "+event);
		}
		
		protected function onPrepareComplete(event:MediaPlayerEvent):void
		{
			mediaPlayer.play();
		}
		
	}
}

 

Please use plain text.
Developer
Innovatology
Posts: 1,280
Registered: ‎03-03-2011
My Device: Playbook, Z10, Q10, Z30 with Files & Folders and Orbit of course
My Carrier: Vodafone

Re: BB10: Download video for MediaPlayer

[ Edited ]

Works fine here, both on Dev Alpha with SDK 3.0.0 beta 3 and on PlayBook with SDK 2.1... What errors?

 

[SWF] qnx.fuse.ui.skins.QNXSkins - 603,349 bytes after decompression
[SWF] TestMediaPlayer.swf - 866,286 bytes after decompression
url bytesLoaded: 16156
...
url bytesLoaded: 607671 proceed bytes: 607671 filestreamClosed

 

Files & Folders, the unified file & cloud manager for PlayBook and BB10 with SkyDrive, SugarSync, Box, Dropbox, Google Drive, Google Docs. Free 3-day trial! - Jon Webb - Innovatology - Utrecht, Netherlands
Please use plain text.
Developer
wmatt
Posts: 125
Registered: ‎10-02-2011
My Device: Playbook
My Carrier: none

Re: BB10: Download video for MediaPlayer

[ Edited ]

That's good to know, maybe it's just something to do with the simulator. 

 

[SWF] qnx.system.QNXDevice - 65,665 bytes after decompression
[SWF] qnx.net.QNXNetwork.ane - 9,177 bytes after decompression
[SWF] qnx.sensors.QNXSensors - 10,291 bytes after decompression
url bytesLoaded: 16156
...
url bytesLoaded: 607671
proceed
bytes: 607671
filestreamClosed
Error #2044: Unhandled error:. text=MediaPlayer: Error during -(inputAttach)

 

Please use plain text.
Developer
Developer
mdd
Posts: 224
Registered: ‎01-17-2012
My Device: PB
My Carrier: ATT

Re: BB10: Download video for MediaPlayer

Dev Alpha simulator can't play streamed media.

 

Regards,

Please use plain text.