2012-12-11 89 views
21

我正在開發一個android應用程序,它需要嵌入一個youtube視頻播放器。我成功從API獲取了RTSP視頻網址,但在Android視頻視圖中嘗試加載此rtsp網址時,它顯示「Can't play this video.」。以前我在這個方法中開發了一個類似的應用程序,當時它運行良好,但它現在也無法加載。如何在Android視頻視圖中播放Youtube視頻?

我很確定,我從API獲取正確的RTSP URL。 rtsp://v6.cache6.c.youtube.com/CiULENy73wIaHAlV9VII3c64lRMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp

這裏是我的活動代碼:

mVideoURL = getIntent().getStringExtra("EXT_URL"); 
    Log.i("VIDEO URL", " " + mVideoURL); 

    MediaController mc = new MediaController(this); 
    mVideoStreamView = (VideoView) findViewById(R.id.vidPlayer); 

    mVideoStreamView.setVideoURI(Uri.parse(mVideoURL)); 
    mVideoStreamView.setMediaController(mc); 
    mVideoStreamView.requestFocus(); 
    mVideoStreamView.start(); 

編輯 發現從logcat的一些附加信息:

ARTSPConnection(6607): status: RTSP/1.0 200 OK 
ASessionDescription(6607): v=0 
ASessionDescription(6607): o=GoogleStreamer 378992432 328144046 IN IP4 74.125.213.182 
ASessionDescription(6607): s=Video 
ASessionDescription(6607): c=IN IP4 0.0.0.0 
ASessionDescription(6607): b=AS:29 
ASessionDescription(6607): t=0 0 
ASessionDescription(6607): a=control:* 
ASessionDescription(6607): a=range:npt=0-1703.000000 
ASessionDescription(6607): m=video 0 RTP/AVP 98 
ASessionDescription(6607): b=AS:17 
ASessionDescription(6607): a=rtpmap:98 H263-2000/90000 
ASessionDescription(6607): a=control:trackID=0 
ASessionDescription(6607): a=cliprect:0,0,144,176 
ASessionDescription(6607): a=framesize:98 176-144 
ASessionDescription(6607): a=fmtp:98 profile=0;level=10 
ASessionDescription(6607): m=audio 0 RTP/AVP 99 
ASessionDescription(6607): b=AS:12 
ASessionDescription(6607): a=rtpmap:99 AMR/8000/1 
ASessionDescription(6607): a=control:trackID=1 
ASessionDescription(6607): a=fmtp:99 octet-align 
ARTSPConnection(6607): status: RTSP/1.0 200 OK 
ARTSPConnection(6607): status: RTSP/1.0 200 OK 
ARTSPConnection(6607): status: RTSP/1.0 200 OK 
ARTSPConnection(6607): status: RTSP/1.0 200 OK 
ARTSPConnection(6607): status: RTSP/1.0 200 OK 
ASessionDescription(6607): v=0 
ASessionDescription(6607): o=GoogleStreamer 1299458498 503248054 IN IP4 74.125.213.182 
ASessionDescription(6607): s=Video 
ASessionDescription(6607): c=IN IP4 0.0.0.0 
ASessionDescription(6607): b=AS:29 
ASessionDescription(6607): t=0 0 
ASessionDescription(6607): a=control:* 
ASessionDescription(6607): a=range:npt=0-1703.000000 
ASessionDescription(6607): m=video 0 RTP/AVP 98 
ASessionDescription(6607): b=AS:17 
ASessionDescription(6607): a=rtpmap:98 H263-2000/90000 
ASessionDescription(6607): a=control:trackID=0 
ASessionDescription(6607): a=cliprect:0,0,144,176 
ASessionDescription(6607): a=framesize:98 176-144 
ASessionDescription(6607): a=fmtp:98 profile=0;level=10 
ASessionDescription(6607): m=audio 0 RTP/AVP 99 
ASessionDescription(6607): b=AS:12 
ASessionDescription(6607): a=rtpmap:99 AMR/8000/1 
ASessionDescription(6607): a=control:trackID=1 
ASessionDescription(6607): a=fmtp:99 octet-align 
ARTSPConnection(6607): status: RTSP/1.0 461 Unsupported Transport 
ARTSPConnection(6607): status: RTSP/1.0 461 Unsupported Transport 

請給我建議的方式來加載Android的視頻觀看YouTube視頻。

在此先感謝...

編輯 在其他設備剛纔檢查,的HTC Desire(2.2)。代碼工作正常。 我想知道關於思考,Nexus(4.1)會出現什麼問題?

+0

檢查了這一點:http://stackoverflow.com/questions/5162088/video-view-not-playing-youtube-video –

+0

你應該使用youtube API的Android,它非常簡單的方法 – vuhung3990

+0

這是它將幫助的鏈接.. http://www.truiton.com/2013/08/android-videoview-example-with-youtube-playback/ –

回答

27

由於我找不到任何方式在視頻視圖中加載rtsp URL(對於所有設備& Android版本),我解決了我的問題與另一項工作。我使用了webview將youtube播放器嵌入其中,這種方法在所有測試過的設備中都能很好地工作。

這裏是我的代碼:

mWebView.getSettings().setJavaScriptEnabled(true); 
mWebView.getSettings().setPluginState(PluginState.ON); 
mWebView.loadUrl("http://www.youtube.com/embed/" + videoID + "?autoplay=1&vq=small"); 
mWebView.setWebChromeClient(new WebChromeClient()); 

非常感謝您對您的所有幫助球員。

+7

這應該是compulsary在設備上有flash播放器,但現在android(4.0以上)不再支持flash內容 –

+0

使用此鏈接.. http://www.truiton.com/2013/08/android-videoview-example-with-youtube-playback/ –

0

使用視頻查看:

在佈局XML 1.Code:

<VideoView 

     android:layout_width=」wrap_content」 

     android:layout_height=」wrap_content」 

     android:id=」@+id/YoutubeVideoView」 /> 

2.Code在Java類:

VideoView v = (VideoView) findViewById(R.id.YoutubeVideoView); 

v.setVideoURI(Uri.parse(「rtsp://v4.cache3.c.youtube.com/CjYLENy73wIaLQlW_ji2apr6AxMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYOr_86Xm06e5UAw=/0/0/0/video.3gp」)); 

v.setMediaController(new MediaController(this)); //sets MediaController in the video view 

// MediaController containing controls for a MediaPlayer        

v.requestFocus();//give focus to a specific view 

v.start();//starts the video 

我們通過指定鏈接3GP設置VIDEOURI的Youtube視頻移動平臺。要添加媒體控件,如播放,暫停,後退,快進和進度滑塊,我們將MediaController添加到VideoView。

uri.parse(視頻3GP鏈接)...你可以從YouTube

+0

我也在使用這種方法,但視頻並未加載。我認爲這可能是我網絡中的rtsp協議問題。我編輯我的問題包括一些日誌。請看看..感謝您的時間.. – Midhu

+0

即時通訊發佈另一個代碼,它可以幫助你獲得RTSP網址的視頻..hope這將幫助你..... –

+0

只是看看它... –

14
private class YourAsyncTask extends AsyncTask<Void, Void, Void> 
    { 
     ProgressDialog progressDialog; 

     @Override 
     protected void onPreExecute() 
     { 
      super.onPreExecute(); 
      progressDialog = ProgressDialog.show(AlertDetail.this, "", "Loading Video wait...", true); 
     } 

     @Override 
     protected Void doInBackground(Void... params) 
     { 
      try 
      { 
       String url = "http://www.youtube.com/watch?v=1FJHYqE0RDg"; 
       videoUrl = getUrlVideoRTSP(url); 
       Log.e("Video url for playing=========>>>>>", videoUrl); 
      } 
      catch (Exception e) 
      { 
       Log.e("Login Soap Calling in Exception", e.toString()); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) 
     { 
      super.onPostExecute(result); 
      progressDialog.dismiss(); 
/* 
      videoView.setVideoURI(Uri.parse("rtsp://v4.cache1.c.youtube.com/CiILENy73wIaGQk4RDShYkdS1BMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp")); 
      videoView.setMediaController(new MediaController(AlertDetail.this)); 
      videoView.requestFocus(); 
      videoView.start();*/    
      videoView.setVideoURI(Uri.parse(videoUrl)); 
      MediaController mc = new MediaController(AlertDetail.this); 
      videoView.setMediaController(mc); 
      videoView.requestFocus(); 
      videoView.start();   
      mc.show(); 
     } 

    } 

public static String getUrlVideoRTSP(String urlYoutube) 
    { 
     try 
     { 
      String gdy = "http://gdata.youtube.com/feeds/api/videos/"; 
      DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
      String id = extractYoutubeId(urlYoutube); 
      URL url = new URL(gdy + id); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      Document doc = documentBuilder.parse(connection.getInputStream()); 
      Element el = doc.getDocumentElement(); 
      NodeList list = el.getElementsByTagName("media:content");///media:content 
      String cursor = urlYoutube; 
      for (int i = 0; i < list.getLength(); i++) 
      { 
       Node node = list.item(i); 
       if (node != null) 
       { 
        NamedNodeMap nodeMap = node.getAttributes(); 
        HashMap<String, String> maps = new HashMap<String, String>(); 
        for (int j = 0; j < nodeMap.getLength(); j++) 
        { 
         Attr att = (Attr) nodeMap.item(j); 
         maps.put(att.getName(), att.getValue()); 
        } 
        if (maps.containsKey("yt:format")) 
        { 
         String f = maps.get("yt:format"); 
         if (maps.containsKey("url")) 
         { 
          cursor = maps.get("url"); 
         } 
         if (f.equals("1")) 
          return cursor; 
        } 
       } 
      } 
      return cursor; 
     } 
     catch (Exception ex) 
     { 
      Log.e("Get Url Video RTSP Exception======>>", ex.toString()); 
     } 
     return urlYoutube; 

    } 

protected static String extractYoutubeId(String url) throws MalformedURLException 
    { 
     String id = null; 
     try 
     { 
      String query = new URL(url).getQuery(); 
      if (query != null) 
      { 
       String[] param = query.split("&"); 
       for (String row : param) 
       { 
        String[] param1 = row.split("="); 
        if (param1[0].equals("v")) 
        { 
         id = param1[1]; 
        } 
       } 
      } 
      else 
      { 
       if (url.contains("embed")) 
       { 
        id = url.substring(url.lastIndexOf("/") + 1); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      Log.e("Exception", ex.toString()); 
     } 
     return id; 
    } 
+0

我是成功從youtube API獲取RTSP網址。我的問題是,我無法在視頻視圖中加載該網址。我現在發現這個問題與我的網絡中的一些rtsp協議設置有關。 – Midhu

+0

所以它的工作....有沒有這樣的問題來實現這 –

+0

什麼是網絡設置 –

0

這要看的視頻編解碼格式,你recieving您的RTSP得到這個。某些設備不支持運行.mp4文件。請通過Android Media support瞭解更多信息。檢查是否可以播放其他.3gp文件。

+0

我收到來自YouTube的RTSP視頻URL。他們是.3gp文件。有什麼方法可以在' VideoView',Android version 4.1。我在試用2.2版本的代碼時得到了正確的輸出結果感謝您的支持 – Midhu

1

結帳這個link。它解釋瞭如何在VideoView中實現YouTube視頻。

1

使用YouTube Android Player API。它完美的作品。請從這裏開始我的例子:

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 

tools:context="com.example.andreaskonstantakos.vfy.MainActivity"> 

<com.google.android.youtube.player.YouTubePlayerView 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:visibility="visible" 
android:layout_centerHorizontal="true" 
android:id="@+id/youtube_player" 
android:layout_alignParentTop="true" /> 

<Button 
android:text="Button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true" 
android:layout_marginBottom="195dp" 
android:visibility="visible" 
android:id="@+id/button" /> 


</RelativeLayout> 

MainActivity.java:

package com.example.andreaskonstantakos.vfy; 


import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import com.google.android.youtube.player.YouTubeBaseActivity; 
import com.google.android.youtube.player.YouTubeInitializationResult; 
import com.google.android.youtube.player.YouTubePlayer; 
import com.google.android.youtube.player.YouTubePlayerView; 



public class MainActivity extends YouTubeBaseActivity { 

YouTubePlayerView youTubePlayerView; 
Button button; 
YouTubePlayer.OnInitializedListener onInitializedListener; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player); 
    button = (Button) findViewById(R.id.button); 


    onInitializedListener = new YouTubePlayer.OnInitializedListener(){ 

     @Override 
     public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { 

      youTubePlayer.loadVideo("Hce74cEAAaE"); 

      youTubePlayer.play(); 
    } 

     @Override 
     public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { 

     } 
    }; 

    button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

youTubePlayerView.initialize(PlayerConfig.API_KEY,onInitializedListener); 
     } 
    }); 
} 
} 

和PlayerConfig.java類:

package com.example.andreaskonstantakos.vfy; 

/** 
* Created by Andreas Konstantakos on 13/4/2017. 
*/ 

public class PlayerConfig { 

PlayerConfig(){} 

public static final String API_KEY = 
"xxxxx"; 
} 

更換「Hce74cEAAaE 「您的視頻ID爲https://www.youtube.com/watch?v=Hce74cEAAaE。從Console.developers.google.com獲取您的API_KEY,並將其替換爲PlayerConfig.API_KEY。對於任何進一步的信息,你可以按照以下教程一步一步:https://www.youtube.com/watch?v=3LiubyYpEUk

+0

儘管此鏈接可能會回答問題,但最好包含必要的部分答案在這裏,並提供參考鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/15827116) –

+1

我想現在是有用的。 –