Log in or register to post comments

How to call from native to play the video clips..

November 1, 2011 - 5:04am #1

Dear All,

I trying to find the way to call video function from native(JNI). I already implemented the video using android SDK API demo in imagetarget example. If I press menu(i.e auto focus), the video play using media player. But now I want when the marker detect video should auto play. My coding is here to play the video from menu:

imagetargets.java:

public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getTitle().equals("Toggle flash"))
{
Intent intent = new Intent(this,MediaPlayerDemo_Video.class);
intent.putExtra(MEDIA, LOCAL_VIDEO);
startActivity(intent);

}

Now how I call this intent function from native(imagetargets.cpp) so that marker detect and play the video.

Please help me ...

Regards
Rassall

How to call from native to play the video clips..

July 29, 2013 - 6:11am #7

How to call from native to play the video clips..

July 29, 2013 - 3:08am #6

I used the similar method in VirtualButtons, i would like to play a video when the user press the virtual button, but it doesn't work in this scenario. Below is my code:

VirtualButtons.cpp

for (int i = 0; i < imageTargetResult->getNumVirtualButtons(); ++i)
        {
            const QCAR::VirtualButtonResult* buttonResult = imageTargetResult->getVirtualButtonResult(i);
            const QCAR::VirtualButton& button = buttonResult->getVirtualButton();
 
            // If the button is pressed, than use this texture:
            if (buttonResult->isPressed())
            {
            if(strcmp(button.getName(),virtualButtonColors[0])==0)
            {
            textureIndex=0;
            jclass activityClass=env->GetObjectClass(obj);
            jmethodID method=env->GetMethodID(activityClass, "playVideo","()V");
            env->CallVoidMethod(obj, method);
            break;
            }
}
 
VirtualButtons.java:
public void playVideo(){
    this.runOnUiThread(new Runnable() {
            public void run() {
                String url="http://www.youtube.com/watch?v=YVJsZCU6z1A";
                Uri uri=Uri.parse(url);
                Intent i=new Intent(Intent.ACTION_VIEW,uri);
                startActivity(i);
            } 
        });
    } 
 
please give me some suggestions. Thank you.

 

Re: How to call from native to play the video clips..

November 3, 2011 - 12:30am #5

Thanks Kim...

It's now working as expected...

Regards

Rassall

Re: How to call from native to play the video clips..

November 2, 2011 - 10:19pm #4

You can probably skip the Handler here. Try storing the activity context in the ImageTargetsRenderer class:

ImageTargets.cpp

int lastTrackableId = -1;

JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargetsRenderer_renderFrame(JNIEnv* env, jobject obj)
{
    ...
    
    // Did we find any trackables this frame?
    for(int tIdx = 0; tIdx < state.getNumActiveTrackables(); tIdx++)
    {
        // Get the trackable:
        const QCAR::Trackable* trackable = state.getActiveTrackable(tIdx);
        
        // Compare this trackable's id to a globally stored id
        // If this is a new trackable, find the displayMessage java method and
        // call it with the trackable's name
        if (trackable->getId() != lastTrackableId) {
            jstring js = env->NewStringUTF(trackable->getName());
            jclass javaClass = env->GetObjectClass(obj);
            jmethodID method = env->GetMethodID(javaClass, "playVideo", "(Ljava/lang/String;)V");
            env->CallVoidMethod(obj, method, js);
            lastTrackableId = trackable->getId();
        }
        
        ...
    }
    
    ...
}

ImageTargetsRenderer.java

    // Store the activity context
    public static Context context;
    
    // Called from native to play a video
    public void playVideo(String trackableName)
    {
    	Intent intent = new Intent(context,MediaPlayerDemo_Video.class);
    	intent.putExtra(MEDIA,LOCAL_VIDEO);
    	context.startActivity(intent);
    }

ImageTargets.java

    protected void onResume()
    {
        super.onResume();
        
        ImageTargetsRenderer.context = this;
        
        ...
    }

Note I changed CallObjectMethod to CallVoidMethod in the JNI section, someone pointed out that this is the correct way to do it.

- Kim

Re: How to call from native to play the video clips..

November 2, 2011 - 6:00am #3
ksiva wrote:

You'll want to use the JNI (Java Native Interface) to call a java method from native. See this post for some sample code that displays a Toast in java when a trackable is found:

http://ar.qualcomm.at/node/2000032

- Kim

Thanks Kim for reply. I already seen that post and tried to implement my coding also. As that post based on to show text and my intention to call from native to play video, I finding difficulties here. Anyway I posted my coding here:

ImageTargets.cpp :

JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargetsRenderer_renderFrame(JNIEnv* env, jobject obj)
{
. . . . .
for(int tIdx = 0; tIdx {

int textureIndex = (!strcmp(trackable->getName(), "stone")) ? 0 : 1;
const Texture* const thisTexture = textures[textureIndex];
jstring js = env->NewStringUTF(trackable->getName());
jclass javaClass = env->GetObjectClass(obj);
jmethodID method = env->GetMethodID(javaClass, "displayMessage", "(Ljava/lang/String;)V");
env->CallObjectMethod(obj, method, js);

}

ImageTargets.java :

public static Handler mainActivityHandler;

//calling my video playback code from here
public void displayMessage(String text)
{
Intent intent = new Intent(this,MediaPlayerDemo_Video.class);
intent.putExtra(MEDIA,LOCAL_VIDEO);
startActivity(intent);

}

protected void onResume()
{
ImageTargetsRenderer.mainActivityHandler = new Handler() {
public void handleMessage(Message msg) {
. . . . . . . .
//don’t know what to written here as the mentioned code for showing text
}

It’s successfully built and installed in phone. But after detect the marker, the program exit automatically. As the given[post] code for showing text, so can you help me to convert the coding to call from native to play video… Under display message function, my video code is there.

Thanks
Rassall

Re: How to call from native to play the video clips..

November 1, 2011 - 8:06pm #2

You'll want to use the JNI (Java Native Interface) to call a java method from native. See this post for some sample code that displays a Toast in java when a trackable is found:

http://ar.qualcomm.at/node/2000032

- Kim

Log in or register to post comments