2015-01-09 262 views
1

我正在開發在視頻播放中播放YouTube視頻,我收到錯誤如「無法播放此視頻」。我用這個代碼的視頻觀看播放YouTube視頻如何在視頻視圖中播放YouTube視頻

public class MainActivity extends ActionBarActivity { 


    private VideoView videoView; 
    private MediaController mController; 
    private Uri uriYouTube; 

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


     videoView = (VideoView) findViewById(R.id.videoView1); 
     mController = new MediaController(this); 
     videoView.setMediaController(mController); 
     videoView.requestFocus(); 


     if (savedInstanceState != null) { 
      int loc = savedInstanceState.getInt("Loc"); 
      Log.i("Loaction: ", loc + ""); 
      uriYouTube = Uri.parse(savedInstanceState.getString("url")); 
      videoView.setVideoURI(uriYouTube); 
      videoView.seekTo(loc); 


      videoView.setOnPreparedListener(new OnPreparedListener() { 
      @Override 
      public void onPrepared(MediaPlayer mp) { 
      Log.v("onPrepared", "ok"); 
      mp.start(); 
      } 
      }); 
      } else { 
      RTSPUrlTask truitonTask = new RTSPUrlTask(); 
      truitonTask.execute("http://www.youtube.com/watch?v=2zNSgSzhBfM"); 
      } 

      } 

      void startPlaying(String url) { 
      uriYouTube = Uri.parse(url); 
      videoView.setVideoURI(uriYouTube); 
      videoView.start(); 
      } 

      @Override 
      protected void onSaveInstanceState(Bundle outState) { 
      super.onSaveInstanceState(outState); 
      outState.putInt("Loc", videoView.getCurrentPosition()); 
      outState.putString("url", uriYouTube.toString()); 
      } 

      /* @Override 
      public boolean onCreateOptionsMenu(Menu menu) { 
      getMenuInflater().inflate(R.menu.video_view, menu); 
      return true; 
      }*/ 

      private class RTSPUrlTask extends AsyncTask<String, Void, String> { 
      @Override 
      protected String doInBackground(String... urls) { 
      String response = getRTSPVideoUrl(urls[0]); 
      return response; 
      } 

      @Override 
      protected void onPostExecute(String result) { 
      startPlaying(result); 
      } 

      public String getRTSPVideoUrl(String urlYoutube) { 
      try { 
      String gdy = "http://gdata.youtube.com/feeds/api/videos/"; 
      DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance() 
      .newDocumentBuilder(); 
      String id = extractYoutubeId(urlYoutube); 
      URL url = new URL(gdy + id); 
      HttpURLConnection connection = (HttpURLConnection) url 
      .openConnection(); 
      Document doc = dBuilder.parse(connection.getInputStream()); 
      Element el = doc.getDocumentElement(); 
      NodeList list = el.getElementsByTagName("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) { 
      return urlYoutube; 
      } 
      } 

      public String extractYoutubeId(String url) throws MalformedURLException { 
      String query = new URL(url).getQuery(); 
      String[] param = query.split("&"); 
      String id = null; 
      for (String row : param) { 
      String[] param1 = row.split("="); 
      if (param1[0].equals("v")) { 
      id = param1[1]; 
      } 
      } 
      return id; 
      } 
    } 

這是我的xml文件, 我得到URL錯誤,也可能在客戶端無法打開文件試圖服務器端

<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" 
    tools:context=".VideoViewActivity" 
    android:background="#000000" > 

    <VideoView 
     android:id="@+id/videoView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" /> 

</RelativeLayout> 

請建議我一種在Android視頻視圖中加載YouTube視頻的方法。

在此先感謝...

+0

這可能有助於http://www.gadgetsaint.com/android/play-youtube-android-youtube-player- api/ – ASP

回答

1

請使用Android版YouTube播放器API https://developers.google.com/youtube/android/player/

+0

我的要求是在視頻視圖中播放這就是我提到的視頻視圖,你能幫我解決這個問題 –

+0

我有一個鏈接給你。希望它會工作http://www.truiton.com/2013/08/android-videoview-example-with-youtube-playback/ – droidd

+0

我使用你的代碼,但我越來越「不能播放這個視頻」我是使用micromax設備可以告訴我什麼問題? –