我创建了一个应用程序,可以在Android上下载YouTube视频。现在,我希望当您在YouTube本机应用程序中播放视频时,也可以下载它。为此,我需要知道YouTube本机应用程序发出的Intent以播放YouTube应用程序。
如果我在模拟器上安装了YouTube程序,那么我可以很容易地做到这一点,因此我的第一个问题是:
1. 我能否为我的模拟器下载YouTube应用程序,或者...
2. 当用户选择播放视频时使用的意图是什么。
我创建了一个应用程序,可以在Android上下载YouTube视频。现在,我希望当您在YouTube本机应用程序中播放视频时,也可以下载它。为此,我需要知道YouTube本机应用程序发出的Intent以播放YouTube应用程序。
如果我在模拟器上安装了YouTube程序,那么我可以很容易地做到这一点,因此我的第一个问题是:
1. 我能否为我的模拟器下载YouTube应用程序,或者...
2. 当用户选择播放视频时使用的意图是什么。
Uri uri = Uri.parse( "https://www.youtube.com/watch?v=bESGLojNYSo" );
uri = Uri.parse( "vnd.youtube:" + uri.getQueryParameter( "v" ) );
startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
/**
* Intent to open a YouTube Video
*
* @param pm
* The {@link PackageManager}.
* @param url
* The URL or YouTube video ID.
* @return the intent to open the YouTube app or Web Browser to play the video
*/
public static Intent newYouTubeIntent(PackageManager pm, String url) {
Intent intent;
if (url.length() == 11) {
// youtube video id
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + url));
} else {
// url to video
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
}
try {
if (pm.getPackageInfo("com.google.android.youtube", 0) != null) {
intent.setPackage("com.google.android.youtube");
}
} catch (NameNotFoundException e) {
}
return intent;
}
你也可以直接获取WebViewClient
wvClient = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("youtube:")) {
String youtubeUrl = "http://www.youtube.com/watch?v="
+ url.Replace("youtube:", "");
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(youtubeUrl)));
}
return false;
}
在我的应用中运行良好。
试试这个:
public class abc extends Activity implements OnPreparedListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
}
}
}
void watch_video(String url)
{
Intent yt_play = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
Intent chooser = Intent.createChooser(yt_play , "Open With");
if (yt_play.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
}
if(YouTubeIntents.canResolvePlayVideoIntent(mContext)){
mContext.startActivity(YouTubeIntents.createPlayVideoIntent(mContext, mVideoId));
}else {
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=" + mVideoId));
mContext.startActivity(webIntent);
}
试试这个,
WebView webview = new WebView(this);
String htmlString =
"<html> <body> <embed src=\"youtube link\"; type=application/x-shockwave-flash width="+widthOfDevice+" height="+heightOfDevice+"> </embed> </body> </html>";
webview.loadData(htmlString ,"text/html", "UTF-8");
使用 Kotlin 扩展
更加容易。
fun Context.watchYoutube(id: String) {
val appIntent = Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:$id"))
val webIntent = Intent(
Intent.ACTION_VIEW,
Uri.parse("https://youtu.be/$id")
)
try {
this.startActivity(appIntent)
} catch (ex: ActivityNotFoundException) {
this.startActivity(webIntent)
}
}
然后你可以在 MainActivity.kt
中实现它,就像这样
fun onClickSomething(){
val linkYoutubeId = "https://youtu.be/3s21Ynn4Huw".substringAfterLast("/")
watchYoutube(linkYoutubeId)
}