android - 无法在客户端打开文件

标签 android youtube android-videoview

我正在尝试在Android视频 View 中播放视频。但是,当我运行代码时,它将打开媒体播放器,但不播放视频,并显示一条错误消息,并且日志目录显示“无法在尝试服务器端的客户端打开文件”。这是我的代码:

public class ExercisesActivity extends Activity {

VideoView videoView;
String videoUrl;
public boolean flage = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_exercises);

     videoView = (VideoView)findViewById(R.id.videoView);
    new performBackgroundTask(ExercisesActivity.this).execute();
}

public void onPause ()
{
super.onPause();
videoView.stopPlayback();
}

private class performBackgroundTask extends AsyncTask < Void, Void, Void >
{


                private ProgressDialog Dialog;

                    public performBackgroundTask(ExercisesActivity context)
                    {
                        Dialog = new ProgressDialog(context);
                    }                   



            protected void onPreExecute()
               {

                this.Dialog.setMessage("Loading Video...");
                this.Dialog.show();

              }


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


            protected void onPostExecute(Void unused)
           {


                if(!flage){

                    if(Dialog.isShowing())
                    {
                        Dialog.dismiss();
                    }


                }
                else
                {
                    videoView.setVideoURI(Uri.parse(videoUrl));
        MediaController mc = new  MediaController(ExercisesActivity.this);
                    videoView.setMediaController(mc);
                    videoView.requestFocus();
                    videoView.start();          
                    mc.show();
                }

                    if(Dialog.isShowing())
                    {
                        Dialog.dismiss();
                    }
                }

            }


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;
    }

}

日志猫显示:
01-07 20:36:40.388: E/Video url for playing=========>>>>>(378):
rtsp://r4---sn-4g57kue6.c.youtube.com/CiILENy73wIaGQmlqd0wKsCfmxMYDSANFEgGUgZ2aWRlb3MM

/0/0/0/video.3gp
01-07 20:36:40.398: D/MediaPlayer(378): Couldn't open file on client side, trying
server side

最佳答案

RTSP链接不适用于Youtube API v 2.0提供的任何数据。而且我认为它不会得到解决。
https://code.google.com/p/gdata-issues/issues/detail?id=4858

关于android - 无法在客户端打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20976238/

相关文章:

android - 在android中隐藏媒体 Controller 的播放暂停按钮

android - 如何通过gradle构建不同版本的android

java - 5 秒自动复选框列表。安卓中的延迟

android - 对 recyclerview 触摸事件没有链式 react

android - 定期从蓝牙线程 Android 中采样数据

javascript - Youtube 脚本延迟

Youtube API - 获取特定关键字的热门视频

android - videoview特定位置的launch方法

php - Laravel:yt-player没有显示

android - YouTubePlayerFragment 在 CollapsingToolbarLayout 中不工作(播放)