youtube视频正在处理数据计划但不能在wifi中工作

时间:2015-01-20 04:47:08

标签: android youtube wifi android-videoview

我正在尝试在android videoview中播放youtube视频,它正在使用dataplan但无法在WIFI中工作,通常我从youtube网址转换为rtsp网址,然后我尝试在videoview中播放rtsp网址但我不喜欢如果我使用WIFI,我知道它为什么不起作用。你能帮帮我吗,这是我的代码,

public class MainActivity extends Activity {

        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.main, 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 version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yvideo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

       <uses-permission android:name="android.permission.INTERNET" />
       <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
       <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
       <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

0 个答案:

没有答案