For all you guys asking how to play audio when an image is recognised this is how i did it:
1. First create a new mediaplayer global variable as
MediaPlayer mp = new MediaPlayer();
2. As soon as you find the trackable verify it in the c++ code and call this method of java through c++
if(trackable->getName(), "nameoftrackable") == 0 )
{
if (trackable->getId() != lastTrackableId )
{
jstring js = env->NewStringUTF(trackable->getName());
jclass javaClass = env->GetObjectClass(obj);
jmethodID method = env->GetMethodID(javaClass, "displayMessage", "(Ljava/lang/String;)V");
env->CallVoidMethod(obj, method, js);
lastTrackableId = trackable->getId();}}
3.Add this to ImageTargetsRenderer.java
public static Handler mainActivityHandler;
// Called from native to display a message
public void displayMessage(String text)
{
// We use a handler because this thread cannot change the UI
Message message = new Message();
message.obj = text;
mainActivityHandler.sendMessage(message);
DebugLog.LOGD("player start");
}
4.In ImageTargets.java in the onresume() method
add a if loop and verify the name of the trackable with the trackable for which you want the audio to play
if(str.equals("nameoftrackable"))
{
try {
//InputStream ins = getResources().openRawResource(R.raw.audiofile);
mp = MediaPlayer.create(getApplicationContext(), R.raw.audiofile);
mp.setLooping(true);
DebugLog.LOGD("audioplay");
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
}
5. In ImageTargets.cpp check check every frame that the trackable is still there if it's not again call a java function in the onresume() method and stop the audio player.
(Note: The str i am verifying here is the string that is defined as
jstring js = env->NewStringUTF(trackable->getName()); )
You can call it with the string "stop" instead of the string "trackable->getName" as used above.
if(str.equals("stop")){
try{
if(mp.isPlaying())
{
DebugLog.LOGD("true");
mp.stop();
mp.reset();
}
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} }
Hope this helps. If any problems please ask.
For your idea, I would recommend starting with the VirtualButtons sample. Take a look at the native renderFrame method (in VirtualButtons.cpp), at the code around button->http://ar.qualcomm.at/node/2000032getNumVirtualButtons(); ++i)
{
const QCAR::VirtualButton* button = target->getVirtualButton(i);
for (int j = 0; j {
if (strcmp(button->getName(), virtualButtonColors[j]) == 0)
{
if (button->isPressed())
{
if (!buttonPressed[j])
{
// do something in response to the button press here
buttonPressed[j] = true;
}
}
else
{
buttonPressed[j] = false;
}
break;
}
}
}
[/CODE]
Take a look at this post for some code that displays a message in Java in response to a tracking event in C++: https://ar.qualcomm.at/arforums/showthread.php?p=70&postcount=2
You may want to do something similar to play your sound in Java.
- Kim