播放YouTube视频VideoView(在Application) [英] Play Youtube video in VideoView (Within Application)
问题描述
从以往的2-3个星期我在寻找一种方式,videoview播放YouTube视频,我已经试过几乎所有被张贴在计算器的方式。 (几乎每...而且有好多我必须告诉)。但没有一个似乎为我工作。
From past 2-3 weeks I am searching for a way to play youtube video in videoview and I have tried almost all the ways that are posted in stackoverflow. (Almost every... and there are lot I must tell). But none seems working for me.
我甚至尝试Android系统的YouTube播放器,但也确实是我没有工作。
I even tried android-youtube-player also but that doesn't works for me.
我的要求:
播放YouTube视频VideoView,因为我想不想打开YouTube应用(很多原因)
MY Requirement: Play Youtube video in VideoView because I want don't want to open Youtube app(A lot of reasons)
如果有人愿意分享一个工作code比这将是很大的帮助。我已经试过几乎所有的东西,累了编码。希望有人可以帮助我在这里。
If some one is willing to share a working code than that would be of great help. I have tried almost everything and tired of CODING. Hope someone could help me out here.
推荐答案
正如前面提到的 previously 您可以打开YouTube播放器播放YouTube视频
我在这里附工作范例。 (驱动器是最好的解决方案在弹出我的脑海里,你可以节省pressing CTRL + S RAR文件)。在那里,你会发现两个项目。
I have attached the working sample here. (Drive is the best solution pops in to my mind and you can save the rar file by pressing ctrl+s). There you will find two projects.
- YouTubeTester - the sample app I have done
- OpenYouTubeActivity - the youtube player that downloaded form here
我已经包括OpenYouTubeActivity项目的jar文件在我的项目,如果你preFER您可以参阅OpenYouTubeActivity项目作为您的项目库(如果你是指该项目作为库确保删除罐子文件。)。 OpenYouTubeActivity的下载源已被更新为发行名单提一>
I have included the jar file of OpenYouTubeActivity project in to my project and if you prefer you can refer the OpenYouTubeActivity project as a library for your project (If you refer the project as a library make sure to remove the jar file.). The downloaded source of OpenYouTubeActivity has been updated as mention in issue list
VideoStream.java (Line: 30)
change: mUrl = lArgMap.get("url");
to: mUrl = lArgMap.get("url") + "&signature=" + lArgMap.get("sig");
现在回到示例项目。
清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.youtubetester"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<!--INTERNET and ACCESS_WIFI_STATE permissions are required. -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".YouTubeTest"
android:label="@string/title_activity_you_tube_test" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- You should include following part orientation is your choice-->
<activity
android:name="com.keyes.youtube.OpenYouTubePlayerActivity"
android:screenOrientation="landscape" >
</activity>
</application>
</manifest>
YouTubeTest活动类
package com.youtubetester;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.keyes.youtube.OpenYouTubePlayerActivity;
public class YouTubeTest extends Activity {
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_you_tube_test);
button =(Button) findViewById(R.id.play);
/*
* The Youtube URL that we get is something like following.
* http://www.youtube.com/watch?v=J467jzLlDcc
* We need the last part of the URL or id of the video-J467jzLlDcc **/
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent lVideoIntent = new Intent(null, Uri
.parse("ytv://"+"J467jzLlDcc"),
YouTubeTest.this,
OpenYouTubePlayerActivity.class);
startActivity(lVideoIntent);
/*
* Please note only the id has been passed and prefix is "ytv" NOT "ytpl"*/
}
});
}
}
布局 - activity_you_tube_test.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".YouTubeTest" />
</RelativeLayout>
希望这会有所帮助。如果您有任何问题,请询问。我没有对OpenYouTubeActivity项目有深刻的理解,但我能提供帮助。
Hope this will help. Please ask if you have any problems. I don't have a deep understanding about the OpenYouTubeActivity project but I'll be able to help.
这篇关于播放YouTube视频VideoView(在Application)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!