0

从过去的 2-3 周开始,我正在寻找一种在 videoview 中播放 youtube 视频的方法,并且我尝试了几乎所有在 stackoverflow 中发布的方法。(几乎每一个......而且我必须告诉很多)。但似乎没有一个对我有用。

我什至也尝试过 android-youtube-player 但这对我不起作用。

我的要求: 在 VideoView 中播放 Youtube 视频,因为我不想打开 Youtube 应用程序(很多原因)

如果有人愿意分享一个工作代码,那将有很大的帮助。我几乎尝试了所有方法并且厌倦了编码。希望有人可以在这里帮助我。

4

1 回答 1

9

如前所述,您可以使用打开 youtube player 播放 youtube 视频

我在这里附上了工作样本。(驱动器是我想到的最佳解决方案,您可以按 ctrl+s 保存 rar 文件)。在那里你会发现两个项目。

  1. YouTubeTester - 我做过的示例应用
  2. OpenYouTubeActivity - 在此处下载表单的 youtube 播放器

我已将 OpenYouTubeActivity 项目的 jar 文件包含到我的项目中,如果您愿意,可以将 OpenYouTubeActivity 项目作为项目的库引用(如果您将项目作为库引用,请确保删除 jar 文件。)。OpenYouTubeActivity 的下载源已更新为问题列表中的提及

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 项目没有深入了解,但我可以提供帮助。

于 2012-12-29T08:15:04.950 回答