Skip to content Skip to sidebar Skip to footer

Open Youtube App From My Android App. How To Pass The Id?

I am quite new to Android. I need to make an app that displays a bunch videos in an overview that gets retrieved from a API on the internet Here's what the API looks like: [{'name

Solution 1:

Try something like this:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=9VJirQKuoxs")));

Android will let you choose the way you want to open the video (browser or youtube app). This should works on a real device but not on emulator.

Hope it helps.

Solution 2:

Have a look at the YouTubeIntents. This is "A selection of static methods that create intents which navigate to specific activities within the main YouTube application."

The intent you are interested in is the YouTubeIntents.createPlayVideoIntent(context,videoId):

/**
 * Put this in your activity
 */publicvoidplayVideo(String videoId) {
    if(YouTubeIntents.canResolvePlayVideoIntent(this)) {
        Intent i = YouTubeIntents.createPlayVideoIntent(this, videoId);
        startActivity(i);
    }
}

As for getting the videoId from the clicked list item it depends a bit on how you create your list and pass the data to it. Do you pass the returned list (named 'alData' in your example) to the list adapter? You can probably do something like this in AdapterView.onItemClick():

JSONFields data = (JSONFields)listview.getAdapter().getItem(position);
String videoId = data.getVideoId();

I make a bunch of assumptions here. First of all I assume that your list is represented by a bunch of JSONField objects (your JSON parse function seem to indicate this). Second, I assume there's a JSONField.getVideoId() to match the setter that you already have.

Update: Since you mention a nullpointer when a list item is clicked there's a few things that could cause it. Either the listview, the adapter or the item is null. I'd suggest that you set a breakpoint in Eclipse at the first line of the click listener and see what's wrong. Alternatively you could try to use the AdapterView directly to get the list item:

@OverridepublicvoidonItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    JSONFielditem= (JSONField)arg0.getItemAtPosition(position);
    if(item != null) {
        StringvideoId= item.getVideoId();     
    }
}

Solution 3:

Try this:

Intentintent=newIntent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+videoId)); 
intent.putExtra("VIDEO_ID", videoId); 
startActivity(intent); 

Solution 4:

I have created a more full example using Damien R's suggestion above. If it works for you, then the error must be elsewhere in your list handling code.

package com.redchillipadi.youtube;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

publicclassMainActivityextendsActivity {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StringvideoID="9VJirQKuoxs";
        //TODO: Sanitize input to prevent code injection
        startActivity(newIntent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v="+videoID)));
    }


    @OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        returntrue;
    }
}

Post a Comment for "Open Youtube App From My Android App. How To Pass The Id?"