TouchEvents for Flash Builder 4.5 - apache-flex

I have successfully made my first application and imported it into my iPhone, but I can't seem to get TouchEvents to work. What am I doing wrong? Here is the code that I have currently:
package
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
public class TestIos extends Sprite
{
private var boll:Sprite = new Sprite;
private var radius:Number = 40;
public function TestIos()
{
super();
// support autoOrients
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
this.addChild(boll);
this.drawBoll();
// Add events
boll.addEventListener(TouchEvent.TOUCH_BEGIN,onTouchBegin);
boll.addEventListener(TouchEvent.TOUCH_TAP,onTouchTap);
}
private function onTouchBegin(e:TouchEvent):void {
var g:Graphics = boll.graphics;
g.clear();
g.beginFill(0xcc66cc,1);
g.drawCircle(150,radius,radius);
boll.scaleX = boll.scaleY = 1.25;
boll.addEventListener(TouchEvent.TOUCH_END,onTouchEnds);
boll.addEventListener(TouchEvent.TOUCH_MOVE,onTouchMove);
}
private function onTouchMove(e:TouchEvent):void {
boll.x = e.stageX;
boll.y = e.stageY;
}
private function onTouchTap(e:TouchEvent):void {
var g:Graphics = boll.graphics;
g.clear();
g.beginFill(0x33cc33,1);
g.drawCircle(150,radius,radius);
boll.scaleX = boll.scaleY = 1;
}
private function onTouchEnds(e:TouchEvent):void {
this.drawBoll();
boll.removeEventListener(TouchEvent.TOUCH_MOVE,onTouchMove);
boll.removeEventListener(TouchEvent.TOUCH_END,onTouchEnds);
}
private function drawBoll():void {
var g:Graphics = boll.graphics;
g.clear();
g.beginFill(0x3399cc,1);
g.drawCircle(150,radius,radius);
boll.scaleX = boll.scaleY = 1;
}
}
}

I would recommend you look at the API and examples. In this case, it seems you're missing the Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; declaration.

Related

javaFX: MediaPlayer's seek method hangs the player without error message oder Status change

I have a programm with 8 mediaplayer, which are controlled like one big video with a single set of controls.
I have one Slider to control the time, aka I call all MediaPlayer's seek methods in onMouseReleased of the slider. My Problem is, the mediaplayer hang all the time, without changing their status or calling onError . When I put every seek in a new Thread, these problems disappear msot of the time, but not always and I'm gettign a lot of concurrency issues.
Does anybody know the reason why the player hangs?
EDIT:
Ok, here a minimal reproducible example:
Class MediaControlMinimal:
package org.example;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Window;
import javafx.util.Duration;
import java.util.ArrayList;
import java.util.List;
public class MediaControlMinimal extends BorderPane {
private final List<MediaPlayer> mpList;
private Duration duration;
private boolean shouldPlay;
private int setupComplete;
private final Slider timeSlider;
private final HBox mediaBar;
private final Button playButton;
private final MediaPlayer controlMediaPlayer;
public MediaControlMinimal(List<MediaPlayer> mpList, int videoRows) {
this.mpList = mpList;
List<MediaView> mvList = new ArrayList<>(mpList.size());
Pane mvPane = new Pane() {
};
for (MediaPlayer mp : mpList) {
MediaView mediaView = new MediaView(mp);
mvList.add(mediaView);
mvPane.getChildren().add(mediaView);
}
mvPane.setStyle("-fx-background-color: black;");
setCenter(mvPane);
mediaBar = new HBox(); // 5 als param für spacing = 5, sieh zeile 247
mediaBar.setAlignment(Pos.CENTER);
mediaBar.setPadding(new Insets(5, 10, 5, 10));
BorderPane.setAlignment(mediaBar, Pos.CENTER);
playButton = new Button();
playButton.setOnAction(e -> {
shouldPlay = !shouldPlay;
if (shouldPlay) {
playAll();
} else {
pauseAll();
}
});
// Add time slider
timeSlider = new Slider();
HBox.setHgrow(timeSlider, Priority.ALWAYS);
timeSlider.setMinWidth(50);
timeSlider.setMaxWidth(Double.MAX_VALUE);
timeSlider.setOnMouseReleased(event -> {
for (MediaPlayer mp : mpList) {
mp.seek(Duration.millis((event.getX() / timeSlider.getWidth()) * timeSlider.getMax());
}
});
controlMediaPlayer = mpList.get(1);
controlMediaPlayer.currentTimeProperty().addListener(observable -> {
updateValues(controlMediaPlayer);
});
for (MediaPlayer mp : mpList) {
mp.setOnReady(() -> {
int videosPerRow = mpList.size() / videoRows;
if (setupComplete == 0) {
duration = mp.getMedia().getDuration();
timeSlider.setMax(duration.toMillis());
updateValues(mp);
final Window window = mvPane.getScene().getWindow();
final double titleHeight = window.getHeight() - mvPane.getScene().getHeight();
double windowHeight = videoRows * mp.getMedia().getHeight() + titleHeight;
if (!Main.isTransDesign) {
windowHeight += mediaBar.getHeight();
}
window.setHeight(windowHeight);
window.setWidth(videosPerRow * mp.getMedia().getWidth());
}
if (setupComplete < mpList.size()) {
final Node mpNode = mvPane.getChildren().get(mpList.indexOf(mp));
if (mpList.indexOf(mp) != 0 && mpNode.getLayoutX() == 0 && mpNode.getLayoutY() == 0) {
//fenster höhe
double xRelocate = mp.getMedia().getWidth() * (mpList.indexOf(mp) % videosPerRow);
double yRelocate = mp.getMedia().getHeight() * Math.floorDiv(mpList.indexOf(mp), videosPerRow);
mpNode.relocate(xRelocate, yRelocate);
}
++setupComplete;
}
});
mp.setCycleCount(MediaPlayer.INDEFINITE);
}
mediaBar.getChildren().add(playButton);
mediaBar.getChildren().add(timeSlider);
setBottom(mediaBar);
}
private void playAll() {
for (MediaPlayer mp : mpList) {
mp.play();
}
}
private void pauseAll() {
for (MediaPlayer mp : mpList) {
mp.pause();
}
}
protected void updateValues(MediaPlayer mp) {
if (timeSlider != null) {
Platform.runLater(() -> {
Duration currentTime = mp.getCurrentTime();
timeSlider.setDisable(duration.isUnknown());
if (!timeSlider.isDisabled() && duration.greaterThan(Duration.ZERO) && !timeSlider.isValueChanging()) {
timeSlider.setValue(currentTime.toMillis());
}
});
}
}
}
Class EmbeddedMediaPlayer
package org.example;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
public class EmbeddedMediaPlayer extends Application {
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle(Main.WINDOW_TITLE);
Group root = new Group();
Scene scene = new Scene(root, 1586, 900);
List<MediaPlayer> mediaPlayerList = new ArrayList<>();
// create media player
for(String s : Main.MEDIA_URL){
MediaPlayer mediaPlayer = new MediaPlayer(new Media(s));
mediaPlayer.setAutoPlay(false);
mediaPlayerList.add(mediaPlayer);
}
MediaControlMinimal mediaControl = new MediaControlMinimal(mediaPlayerList, Main.VIDEO_ROWS);
scene.setRoot(mediaControl);
primaryStage.setScene(scene);
primaryStage.show();
}
#Override
public void stop(){
System.out.println("Stage is closing");
System.exit(0);
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Class Main
package org.example;
public class Main {
public static final String[] MEDIA_URL = {
"https://video.fogodosamba.de/media/SambaReggae_Sticks.mp4",
"https://video.fogodosamba.de/media/SambaReggae_Fundo1.mp4",
"https://video.fogodosamba.de/media/SambaReggae_Dobra.mp4",
"https://video.fogodosamba.de/media/SambaReggae_Fundo2.mp4",
"https://video.fogodosamba.de/media/SambaReggae_Ansage.mp4",
"https://video.fogodosamba.de/media/SambaReggae_Timbal.mp4",
"https://video.fogodosamba.de/media/SambaReggae_Caixa.mp4",
"https://video.fogodosamba.de/media/SambaReggae_Repi.mp4"
};
public static final int VIDEO_ROWS = 2;
public static final String WINDOW_TITLE = "";
public static void main(String[] args) {
EmbeddedMediaPlayer.main(args);
}
}
EDIT 2:
Sometimes a video hsngs for a while, then starts up again, but plays in slow motion. No error message in onerror, no state change and getCurrentRate = 1.

Row renderer with height depending on row index

I have an IDropInListItemRenderer that is being used inside a List. The height of the row depends on both the data and it's position in the List's view.
How do I remeasure as soon as the row index changes?
updateDisplayList doesn't get called every time, and also by that point it's too late because the List has already measured it.
edit
The effect I'm trying to achieve is similar to iOS where the header for a section sticks to the top
Here is the basic renderer that doesn't measure correctly:
import mx.controls.Label;
import mx.controls.listClasses.BaseListData;
import mx.controls.listClasses.IDropInListItemRenderer;
import mx.controls.listClasses.IListItemRenderer;
import mx.core.UIComponent;
import mx.events.FlexEvent;
public class MyRowRenderer extends UIComponent implements IListItemRenderer, IDropInListItemRenderer {
private static const HEADER_HEIGHT:int = 20;
private static const LABEL_HEIGHT:int = 20;
private var _data:Object;
private var _label:Label;
private var _header:Label;
private var _listData:BaseListData;
public function MyRowRenderer() {
super();
}
[Bindable("dataChange")]
public function get data():Object {
return _data;
}
public function set data(value:Object):void {
this._data = value;
invalidateProperties();
dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
}
[Bindable("dataChange")]
public function get listData():BaseListData {
return _listData;
}
public function set listData(value:BaseListData):void {
_listData = value;
}
override protected function createChildren():void {
super.createChildren();
_header = new Label();
addChild(_header);
_label = new Label();
addChild(_label);
}
override protected function measure():void {
super.measure();
if (_label == null || _header == null) {
return;
}
var h:Number = LABEL_HEIGHT;
if (_listData.rowIndex == 0) {
h += HEADER_HEIGHT;
}
explicitHeight = measuredHeight = height = h;
}
override protected function commitProperties():void {
super.commitProperties();
_label.text = _data.label;
_header.text = _data.header;
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
super.updateDisplayList(unscaledWidth, unscaledHeight);
_header.visible = _header.includeInLayout = (_listData.rowIndex == 0);
if (_header.visible) {
_header.move(0, 0);
_header.setActualSize(unscaledWidth, HEADER_HEIGHT);
_label.move(0, HEADER_HEIGHT);
_label.setActualSize(unscaledWidth, LABEL_HEIGHT);
} else {
_label.move(0, 0);
_label.setActualSize(unscaledWidth, LABEL_HEIGHT);
}
}
}
}
I ended up creating a subclass of List and overriding the shiftRow function to dispatch a custom RowIndexChangeEvent. In the row renderer's set listData it adds an event listener to the owner for the RowIndexChangeEvent and invalidates it's properties, size and displaylist.

Linking a webcam in a flex application

i am having a very strange problem while linking a webcam i xperience following error
ArgumentError: Error #2126: NetConnection object must be connected.
at flash.net::NetStream/ctor()
at flash.net::NetStream()
Following is my code in main.mxml
<fx:Script>
<![CDATA[
import flash.media.Camera;
import flash.media.Video;
import flash.net.NetConnection;
import mx.core.UIComponent;
import com.kahaf.plutay.* ;
private var inVideo:Video;
private var outVideo:Video;
private var inVideoWrapper:UIComponent;
private var camera:Camera;
private var mic:Microphone;
private var inStream:NetStream;
private var outStream:NetStream;
private function defaultVideoMode(): void
{
VideoPanel.width = 726;
VideoPanel.height = 494;
inVideo.width = 726;
inVideo.height = 494;
}
private function showInComingVideo():void
{
inVideo = new Video(VideoPanel.width,VideoPanel.height);
inVideo.attachNetStream(inStream);
inVideoWrapper = new UIComponent();
inVideoWrapper.addChild(inVideo);
VideoPanel.addElement(inVideoWrapper);
defaultVideoMode();
}
private function setupVideo(event:MouseEvent): void
{
camera = Camera.getCamera();
mic = Microphone.getMicrophone();
mic.setLoopBack(false);
mic.setUseEchoSuppression(true);
camera.setMode(640,480,20);
camera.setQuality(65536,90);
var conn:NetConnection = Connection.getConnection().conn;
inStream = new NetStream(conn);
inStream.play(conn);
showInComingVideo();
}
]]>
<s:Group x="283" y="330" width="234" height="149" id="VideoPanel" >
</s:Group>
<s:Button x="447" y="151" label="Click Me." click="setupVideo(event)"/>
here is the code of my connection class :
import flash.net.NetConnection;
public class Connection extends NetConnection
{
public static var conObj:Connection;
public var conn:NetConnection;
public var target:Object;
public var selector:Function;
public function Connection()
{
conn = new NetConnection;
target = null;
selector = null;
conn.client = this;
}
public static function getConnection():Connection
{
if(conObj == null)
{
conObj = new Connection();
}
return conObj;
}
}
This the correct order when handling NetConnection and NetStreams:
Create and establish the NetConnection (NetConnection.connect())
Wait for the NetConnection.Connect.Success event (NetStatusEvent.NET_STATUS)
Create your NetStream and attach the connected NetConnection to it
Publish/play your stream

save button error in AS project

Whats wrong with the following code,There is an error at
saveButton.visible = false;
discardButton.visible = false;
package
{
import flash.display.Sprite;
import flash.media.Camera;
import flash.media.Video;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.utils.ByteArray;
import com.adobe.images.JPGEncoder;
public class caml extends Sprite
{
private var camera:Camera = Camera.getCamera();
private var video:Video = new Video();
private var bmd:BitmapData = new BitmapData(320,240);
private var bmp:Bitmap;
private var fileReference:FileReference = new FileReference();
private var byteArray:ByteArray;
private var jpg:JPGEncoder = new JPGEncoder();
public function caml()
{
saveButton.visible = false;
discardButton.visible = false;
saveButton.addEventListener(MouseEvent.MOUSE_UP, saveImage);
discardButton.addEventListener(MouseEvent.MOUSE_UP, discard);
capture.addEventListener(MouseEvent.MOUSE_UP, captureImage);
if (camera != null)
{
video.smoothing = true;
video.attachCamera(camera);
video.x = 140;
video.y = 40;
addChild(video);
}
else
{
trace("No Camera Detected");
}
}
private function captureImage(e:MouseEvent):void
{
bmd.draw(video);
bmp = new Bitmap(bmd);
bmp.x = 140;
bmp.y = 40;
addChild(bmp);
capture.visible = false;
saveButton.visible = true;
discardButton.visible = true;
}
private function saveImage(e:MouseEvent):void
{
byteArray = jpg.encode(bmd);
fileReference.save(byteArray, "Image.jpg");
removeChild(bmp);
saveButton.visible = false;
discardButton.visible = false;
capture.visible = true;
}
private function discard(e:MouseEvent):void
{
removeChild(bmp);
saveButton.visible = false;
discardButton.visible = false;
capture.visible = true;
}
}
}
I guess this is a document root class and the buttons is added to stage in the flash fla file. In that case you need to add the buttons to your declaration as public members:
public var saveButton : Button;
public var discardButton : Button;
UPDATE
I never use the flash components myself but you might find an answer here on how to use Flash components in Flashbuilder:
http://www.moock.org/blog/archives/000253.html
http://www.ruttencutter.com/?p=20

How to add an EventListener to a moiveClips/Sprites's scaleX change event?

How can i trigger an event for the scaleX property change of the movieClip or Sprite in Flash AS 3.
For Example: CustomSprite.as
package view {
import flash.display.Sprite;
import flash.events.Event;
import events.ScaleChangeEvent;
public class CustomSprite extends Sprite {
override public function set scaleX(value:Number):void {
super.scaleX = value;
dispatchEvent(new ScaleChangeEvent(ScaleChangeEvent.SCALE_CHANGED));
}
public function CustomSprite() {
super();
}
override public function dispatchEvent(event:Event):Boolean {
if (willTrigger(event.type)) {
return super.dispatchEvent(event);
}
return true;
}
}}
ScaleChangeEvent.as
package events {
import flash.events.Event;
public class ScaleChangeEvent extends Event {
public static const SCALE_CHANGED:String = "scaleChanged";
public function ScaleChangeEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false) {
super(type, bubbles, cancelable);
}
override public function clone():Event {
return new ScaleChangeEvent(type, bubbles, cancelable);
}
override public function toString():String {
return formatToString("ScaleChangeEvent", type, bubbles, cancelable, eventPhase);
}
}}
Test.as
package {
import flash.display.Sprite;
import flash.events.Event;
import view.CustomSprite;
import events.ScaleChangeEvent;
public class Test extends Sprite {
public function Test() {
addEventListener(Event.ADDED_TO_STAGE, addedToStageListener, false, 0, true);
}
private function addedToStageListener(event:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, addedToStageListener);
var customSprite:CustomSprite = new CustomSprite();
customSprite.addEventListener(ScaleChangeEvent.SCALE_CHANGED, scaleChangedListener, false, 0, true);
customSprite.scaleX = 0.2;
}
private function scaleChangedListener(event:ScaleChangeEvent):void {
trace(event.target.scaleX);
}
}}

Resources