开发者

Calling YouTube app using ACTION_VIEW intent Failing most of the time

I've written a small app to parse some RSS feeds from YouTube and launch videos selected by the user. To play the video, I'm using an intent:

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(videoAddress);

In order to call the YouTube app, if installed on the device.

The problem I'm having is that, of the population of videos I am using in my app, about 90% of them display a 'Cannot play video' error message: "Sorry, this video cannot be played.". A few of them work just fine from my app. The videos that do not work will play fine in the YouTube app if searched for and launched entirely from within the YouTube app.

Has anybody seen this behavior, or does anybody have any ideas for things to try? Obviously the开发者_如何学JAVA YouTube app launches videos in a slightly different way internally than it does from an Intent request, but I haven't a clue how to get to the bottom of it.


I have the same issue. Are you sure that all of the video play correctly from the youtube app? In my case, on an old G1, the videos I can't play from my app won't play even if searched from within the youtube app. I think the video encoding is not supported in some case and/or the combination of a slow cpu and slow network make the video not playable. I've read about people just refreshing many times untill the video starts playing... I guess in thier care it was a network/buffering issue. More discussion here: http://www.google.com.tw/support/forum/p/android/thread?tid=3a62cdf7188384af&hl=en

For this reason my App (similar to yours) got a lot of bed comments. I republished it only for Android >=2.1 and I now I have fewer bad feedback.


startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(YOUTUBE_URL))); 

The above line of code works for my App.

What it basically does is lets Android handle the startActivity with available installed software on the device. Android in turn opens the IntentChooser and lets the user decide which appropriate software to use in this case a Browser and Youtube App to open the video.

Try it out and let me knwo if it works for you or f you have any other issues.


The most reliable method I have found for accessing youtube from an application is using the mobile site, try this instead (eg for searching):

String videoUrl = "http://m.youtube.com/#/results?q=ciaconna+bach";

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(videoUrl)));

This solved the "Cannot play video' error message" that I was receiving.


I use this code:

String vid= Uri.parse(urlVideo).getQueryParameter("v");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + vid)); 
try{
    startActivity(intent);
}
catch (ActivityNotFoundException ex){
    Log.e(TAG, "Couldn't find activity to view this video");
}

May be works for you.


I have got the same problem only with HTC Hero 2.1. You can force the intent to launch the htc flash player instead of the Youtube app. With the flash player app I have not had any problem:

Uri uri = Uri.parse("vnd.youtube:" + videoUrl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);

String availableFlashPlayer = availableFlashPlayer();

if (availableFlashPlayer != null) {
    // launch the intent with the available flash player
    intent.setPackage(availableFlashPlayer);
}
startActivity(intent);

The availableFlashPlayer method:

public String availableFlashPlayer() {

    String availableFlashPlayer = null;
    String FLASH_PLAYER = "com.htc.flash";
    PackageManager pm = getPackageManager();
    try {
        ApplicationInfo ai = pm
                .getApplicationInfo(FLASH_PLAYER, 0);
        if (ai != null) {
            availableFlashPlayer = FLASH_PLAYER;
        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
    }       
    return availableFlashPlayer;
}

You can also check the Adobe Flash Player:

String FLASH_PLAYER = "com.adobe.flashplayer";

Alternatively, you can force the intent to launch the Android Browser as follows:

Uri uri = Uri.parse(videoUrl);
String packageName = "com.android.browser"; 
String className = "com.android.browser.BrowserActivity"; 
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setClassName(packageName, className);
startActivity(intent);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜