2017-06-30 53 views
0

我做了搜索,并试图或VideoView内的堆叠/ Android文档中的所有解决方案教程MediaPlayer“无法打开<url>”?

任何想法,为什么我不能播放视频URL(从YT)成我的应用程序? AndroidManifest文件中添加INTERNET权限,也这是我的代码的一部分:

String url = "http://www.youtube.com/watch?v="+eventData.getYoutube_id(); 
final VideoView vw = views.getVideoView(R.id.vw_media); 
vw.setVisibility(View.VISIBLE); 

try{ 
    MediaController mc = new MediaController(context); 
    mc.setAnchorView(mc); 
    Uri video = Uri.parse(url); 
    vw.setMediaController(mc); 
    vw.setVideoURI(video); 
catch (Exception e){ 
    Log.d(TAG, e.getMessage()); 
} 
vw.requestFocus(); 
vw.start(); 

我得到了应用程序,说“无法播放此视频” enter image description here

logcat的消息: MediaPlayer的:无法打开http://www.youtube.com/watch?v=sfkmKzr8zgg:java.io.FileNotFoundException:没有内容提供商:http://www.youtube.com/watch?v=sfkmKzr8zgg

不能顺便说一下解决这个问题所有.. 和固醇的版本是7.0

+0

如果您有YouTube视频ID,然后使用YouTubePlayerFragment(YouTubeAndroidPlayerApi),而不是VideoView – akhilesh0707

+0

我会的,但是这部分代码是不活动/框架内。 。如果不扩展YoutubeBaseActivity类,我无法整合它。 –

回答

0
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(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

java.io.FileNotFoundException:http://gdata.youtube.com/feeds/api/videos/sfkmKzr8zgg –

+0

我设法解决您的解决方案,结果是相同的“无法播放此视频”.. –

+0

亚,我仍然收到错误。我目前正在研究exoplayer:https://developer.android.com/guide/topics/media/exoplayer.html – chornge