在VideoView中播放Youtube视频(在应用程序内)

use*_*578 0 youtube android android-videoview

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

我甚至尝试过android-youtube-player,但这对我不起作用.

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

如果有人愿意分享工作代码而不是那将会有很大的帮助.我已经尝试了几乎所有的东西而厌倦了CODING.希望有人可以帮助我.

saj*_*159 9

如前所述以前 你可以用开放的YouTube播放器播放YouTube视频.

在这里附上了工作样本.(Drive是最好的解决方案,我可以通过按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");
Run Code Online (Sandbox Code Playgroud)

现在回到示例项目.

清单文件

<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>
Run Code Online (Sandbox Code Playgroud)

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"*/
            }
        });
    }


}
Run Code Online (Sandbox Code Playgroud)

布局 - 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>
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助.请问你是否有任何问题.我对OpenYouTubeActivity项目没有深刻的了解,但我能够提供帮助.