2012-12-11 193 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

我想现在是有用的。 –

相关问题