首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在VideoView中播放Youtube视频(在应用程序中)

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

Stack Overflow用户
提问于 2012-10-16 07:25:36
回答 1查看 5.9K关注 0票数 0

从过去的2-3周,我正在寻找一种方式来播放youtube视频的视频视频,我已经尝试了几乎所有的方式,张贴在堆栈溢出。(几乎每一个.还有很多事情我必须告诉你)。但似乎没有人为我工作。

我甚至尝试过安卓-youtube-播放器,但这对我不起作用。

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

如果有人愿意分享一个工作代码,那将是很有帮助的。我几乎什么都试过了,也厌倦了编码。希望有人能帮我。

EN

回答 1

Stack Overflow用户

发布于 2012-12-29 08:15:04

正如前面提到的,之前 可以使用打开的youtube播放器播放youtube视频。

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

  1. YouTubeTester --我做过的示例应用程序
  2. OpenYouTubeActivity -从这里下载的youtube播放器

我已经将OpenYouTubeActivity项目的jar文件包含在我的项目中,如果您愿意,可以将OpenYouTubeActivity项目作为项目的库(如果将该项目称为库,请确保删除jar文件)。下载的OpenYouTubeActivity源代码已经更新,如列表问题中提到的那样。

代码语言:javascript
复制
VideoStream.java (Line: 30)
change: mUrl = lArgMap.get("url");
to:  mUrl = lArgMap.get("url") + "&signature=" + lArgMap.get("sig");

现在回到示例项目。

报表文件

代码语言:javascript
复制
<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活动类

代码语言:javascript
复制
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

代码语言:javascript
复制
<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项目没有很深的理解,但我能够提供帮助。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12909515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档