Hello, last night I decided to take my first foray into Marapi, also started playing around
with Java which is something I've never really had too much time to work with.
And what I did was created an app, a Marapi-based air app to grab some input from the Java application.
The Java application is listening to the microphone and based on that input, it's sending over
messages to our air application.
So let me do a quick demo here.
So the first thing we're going to want to do is in our Java application, which you can
see right here, is we're going to go ahead and start that up.
And then once that's started, we're going to want to start up our application.
And now, I can play notes on my guitar.
Alright, so now that you've seen the demonstration, let's just go ahead and take a look to see
what makes this app tick.
Just as a disclaimer, this is something I worked on quickly yesterday and so it's definitely
not best practices, but it'll give you an idea of how to work with Marapi.
So the first step that you want to take is within your Java application, you want to
open a bridge instance to connect to the Marapi bridge.
So in your Java application, we have our main class here with our main method.
And in the main method, we're just calling bridge.open on our Marapi bridge instance.
So this creates the connection to Marapi in our Java application.
From there, we want to create our message objects, and these are the objects that are
going to be passed back and forth over the bridge.
If you're familiar with remoting, then you'll see that basically what we do is we create
an object class in our Java application, and then we create a similar class in our Air
application so that the data can be translated back and forth.
So right now, we're still in our Java app, and we've created a frequency info message
class that extends the actual Marapi message class.
We have a string, and this is, in a way, you can kind of think of it like an event type,
but it's a message type, and that's how you can add handlers based on the various message
types that get sent across the bridge.
From there, we have a setter and a getter for the actual data itself.
So when we send this message over, and we want to send data from our Java application
to our Air application, we're going to use this property here.
So now that we have the object message object in our Java app, let's go ahead and look at
it in the Air app.
So in the Air app, we have the message class that mirrors the one that was in the Java
project.
So here's our frequency info message class that extends the Marapi message class.
We have the same type here listed in our ActionScript class, and then we also have our data here.
So that's our payload.
So now we've connected to the bridge in the Java application, and we've created a message
object to pass data back and forth between the two apps.
Now what we want to do is in our Air application, we're going to go ahead and create an instance
of our frequency info message class here.
So as you can see down here on line 174, we have our frequency info message, and right
now I'm just saying the data is null.
And the other thing we want to do is we want to create a handler that way when we get a
message back of this type from Marapi, we can actually make it do something in our Air
app.
So as you can see right underneath the definition for a message, we have this message handler
instance.
So here's our message handler instance, and it's going to handle messages of type frequency
info message dot frequency info.
And as you can tell, we actually have this data field property that maps to the frequency
info string, which is the payload, and that's actually going to allow us to access this
data through a data property for a frequency info handler.
So the handler itself has a data property, and when you set the data field, that's what
it's going to map that data property to.
So now, if you look a little bit further down, we have this label here, and this label is
actually bound to the frequency info handler's data property.
So basically, this binding is going to cause this method to fire.
And this method is what we're doing whenever we get a message back from the Java app.
And if we just go ahead and scroll up, so we have this update console.
So we're creating some output data, and we're checking to see if it's null, if so, just
set it to a blank string.
And then if it's not equal, so if this is a different note coming in from the Java app,
then we're going to say that the note frequency changed, and we're going to play a different
note.
So as the app ran, you saw that different notes were being played as the text updated.
So now, to start the process, we've opened up our applications, and they're running.
And the first thing that's going to happen is we're sending a message from the Flex app.
So on my creation complete handler for our Air app, sorry, we're going to go ahead and
send a message over.
So basically, these message objects have this property called send, or have this method
called send, that is going to allow them to hit the bridge and be sent back and forth
between the Air and the Java app.
So now that we've sent this from the Air app, we're going to go back to our Java app, and
we're going to handle this message.
So as you can see, we have this class called Frequency Info Message Handler.
Extends the Merapi Message Handler class.
And it has a method in here called Handle Message, which handles a generic iMessage
interface message.
Then we check to make sure that it's the right kind of message class.
And then based on that, we can do what we need to do.
In our case, what we're doing is if it's the right kind of message, we're going to start
our audio sampler.
So that way, we can start listening to the data coming in from our microphone.
And there, what we want to do is when data comes in from our microphone, and it's the
right kind of data, so it's a note of a particular frequency that we're monitoring, we want to
take a message and send it over to the Air app from the Java app.
So in our handler, we're going to send a message over.
So we have this frequency analyzer here that's listening to the microphone data.
And under a particular circumstance, it's going to want to send messages over to the
Air app.
So right now, it's going to bubble this message up to the parent, which is the frequency
info message handler.
And this is the data.
This is the payload here.
And then the handler, frequency info message handler, which is of type Merapi dot handlers
dot message handler, is going to run the send message function.
And this frequency info message instance is an instance of type frequency info message.
So this is that message class that you use to pass data back and forth.
So what we do is we set the data payload here using the set frequency info.
And then we call send.
And send is what takes the message, puts it on the bridge so that the Air app can grab
it.
Once that's done, we go ahead and go to step nine, and that is now we're back in the Air
app.
And here you can see this message handler that we've instantiated in MXML as a data field
of type frequency info, which is the payload.
And when this changes, it causes the data property of this handler to change, which is
what this label binding is bound to, frequency info handler dot data.
And that's what causes the update console method to run, which A sets the label text
and B causes the notes to play.
So that's it in a nutshell.
It's a very, very clean cut, straightforward API.
To make use of it, you need to create a Java project and an Air project.
And when you do, all you really need to do is just go on your project properties and
go into your build settings and make sure that the Marapi libraries are included.
Rich Tritola has several great tutorials on how to work with Marapi and how to set up
your project.
So you may want to check out his blog.
It's Everything Flex.
That's what I used when I was getting up and running.
So let's cancel that.
And then as you can see, here's the Air app.
So to support Marapi, we just go into our Flex build path and we go to our library path.
And in our Libs directory, you can see we have the Marapi library switch.
And that's it.
I hope you enjoyed this and until next time.
