如何在Android中运行youtube播放器

时间:2013-07-16 06:11:06

标签: android youtube

class loadhtml extends AsyncTask<String,Void,String>{

    ProgressDialog mProgressDialog;
    public static final String TAG_VIDEO="data";
    JSONArray data = null;


    @Override
    protected void onPreExecute() {

        mProgressDialog=new ProgressDialog(LatestVideoActivity.mContext);
        mProgressDialog.setTitle("Fetech Video...");
        mProgressDialog.setMessage("Please Wait...");
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }


    @Override
    protected String doInBackground(String... params) {
        String response="";

          try{
            String url="http://twominenglish.com/api/getvideo?videoid="+ videoId;
            response = sendRequest(url);


        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

        return response;
    }



    @Override
    protected void onPostExecute(String result) {
        //mProgressDialog.dismiss();
          if (!result.equalsIgnoreCase("")) {

                try {

                    JSONObject mainObject = new JSONObject(result);

                    if (mainObject.has("ID")) {
                         id = mainObject.getString("ID");
                            // Toast.makeText(getBaseContext(), ""+id, Toast.LENGTH_SHORT).show();
                    }

                    if (mainObject.has("VideoID")) {
                        mVideoId = mainObject.getString("VideoID");
                    //Toast.makeText(getBaseContext(), ""+videoId, Toast.LENGTH_SHORT).show();
                    }

                    if (mainObject.has("Title")) {
                         title = mainObject.getString("Title");
                        //Toast.makeText(getBaseContext(), ""+title, Toast.LENGTH_SHORT).show();
                    }

                    if (mainObject.has("Description")) {
                        desc = mainObject.getString("Description");
                        //Toast.makeText(getBaseContext(), ""+desc, Toast.LENGTH_SHORT).show();

                    }

                    if (mainObject.has("Script")) {
                        script = mainObject.getString("Script");
                        // Toast.makeText(getBaseContext(), ""+script, Toast.LENGTH_SHORT).show();

                    }

                    if (mainObject.has("Phrases")) {
                         phrases = mainObject.getString("Phrases");
                        //Toast.makeText(getBaseContext(), ""+phrases, Toast.LENGTH_SHORT).show();

                    }

                } catch (Exception e) {
                    e.printStackTrace();

                } finally {



                        mProgressDialog.dismiss();



                            Matcher matcher = Pattern.compile("http://www.youtube.com/embed/").matcher(mVideoId);
                            //Exampe of full path of youtube="http://www.youtube.com/embed/4oceDC_w6uY;

                            matcher.find();
                        //  Uri my =Uri.parse("http://www.youtube.com/embed/" +mVideoId);
                            Intent lVideoIntent = new Intent(
                                    null,
                                    Uri.parse("ytv://" + matcher.group(1)),
                                    LatestVideoActivity.mContext,
                                    com.astrobix.youtube.OpenYouTubePlayerActivity.class);
                                    startActivity(lVideoIntent);










                }
            }

        }

        public String sendRequest(String url) {
            String result = "";
            try {

                HttpClient client = new DefaultHttpClient();
                HttpParams httpParameters = client.getParams();
                HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
                HttpConnectionParams.setSoTimeout(httpParameters, 5000);
                HttpConnectionParams.setTcpNoDelay(httpParameters, true);
                HttpGet request = new HttpGet();
                request.setURI(new URI(url));
                HttpResponse response = client.execute(request);
                InputStream ips = response.getEntity().getContent();

                BufferedReader buf = new BufferedReader(new InputStreamReader(ips,
                        "UTF-8"));

                StringBuilder sb = new StringBuilder();
                String s;
                while (true) {
                    s = buf.readLine();
                    if (s == null || s.length() == 0)
                        break;
                    sb.append(s);

                }
                buf.close();
                ips.close();
                result = sb.toString();

            } catch (Exception e) {
                e.printStackTrace();
            }

            return result;
  }

当我运行此代码时,它显示异常:

  

“java.lang.IllegalStateException:到目前为止没有成功的匹配”“java.util.regex.Matcher.ensureMatch(Matcher.java:607)”“在java.util.regex.Matcher.group(Matcher.java) :358)“

1 个答案:

答案 0 :(得分:0)

删除模式匹配器:

                        Matcher matcher = Pattern.compile("http://www.youtube.com/embed/").matcher(mVideoId);
                        //Exampe of full path of youtube="http://www.youtube.com/embed/4oceDC_w6uY;

                        matcher.find();

并将Uri.parse("ytv://" + matcher.group(1)),替换为Uri.parse("ytv://" + mVideoId),