在过去的2-3周中,我一直在寻找一种在videoview中播放youtube视频的方法,并且我已经尝试了stackoverflow中发布的几乎所有方法。 (几乎每个……而且我必须告诉很多)。但是似乎没有人为我工作。
我什至也尝试过android-youtube-player,但这对我不起作用。
我的要求:
因为我不想打开Youtube应用,所以在VideoView中播放Youtube视频(很多原因)
如果有人愿意共享一个工作代码,那将有很大的帮助。我已经尝试了几乎所有内容,并对编码感到厌倦。希望有人可以在这里帮助我。
最佳答案
如previously 所述,您可以使用打开的youtube播放器播放youtube视频。
我已附上工作示例here。 (驱动器是我想到的最佳解决方案,您可以通过按ctrl + s来保存rar文件)。在那里您将找到两个项目。
我已经将OpenYouTubeActivity项目的jar文件包含在我的项目中,如果愿意,可以将OpenYouTubeActivity项目作为项目的库(如果将项目称为库,请确保删除jar文件。)。如问题list中所述,已下载了OpenYouTubeActivity的下载源
VideoStream.java (Line: 30)
change: mUrl = lArgMap.get("url");
to: mUrl = lArgMap.get("url") + "&signature=" + lArgMap.get("sig");
现在回到示例项目。
list 文件
<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 Activity 类
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项目没有深刻的了解,但我可以为您提供帮助。
关于android - 在VideoView中播放Youtube视频(在应用程序中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12909515/