2012-10-16 139 views
0

從過去2-3周開始,我正在尋找一種在videoview中播放YouTube視頻的方法,並且我嘗試了幾乎所有發佈在stackoverflow上的方法。 (幾乎每一個......並且我必須告訴我很多)。但似乎沒有人爲我工作。在VideoView中播放Youtube視頻(在應用程序內)

我甚至還嘗試過android-youtube-player,但那不適用於我。

我的要求: 播放YouTube視頻VideoView因爲我想不想開的YouTube應用程式(很多原因)

如果有人願意分享一個工作代碼比這將是很有幫助。我嘗試了幾乎所有的東西,並厭倦了編碼。希望有人能幫助我。

+0

你首先從視頻的youtube網址中提取視頻ID,然後你可以播放它。 –

+0

我認爲youtube webservice提供了rstp links.Android 4.0.4支持RSTP鏈接。 – AppMobiGurmeet

+0

任何人都可以提供我的工作示例 – user1551578

回答

9

如上所述previously您可以與開放的youtube播放器播放youtube視頻。

我附上了工作樣本here。 (驅動器是我心中最好的解決方案,你可以按ctrl + s保存rar文件)。在那裏你會找到兩個項目。

  1. YouTubeTester - 示例應用程序我已經做了
  2. OpenYouTubeActivity - 即下載的形式here

YouTube播放器我已經包括OpenYouTubeActivity項目的jar文件到我的項目,如果你喜歡你可以將OpenYouTubeActivity項目作爲項目庫引用(如果將項目作爲庫引用,請確保刪除該jar文件)。 OpenYouTubeActivity的下載源已發行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項目沒有深入的瞭解,但我可以提供幫助。

+0

你確定在所有設備上面運行示例嗎?或以上的Android Api 2.2? –

+0

它的工作。非常感謝你.. :) –

+0

:)測試2.2和以上。 – saji159

相關問題