且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

"不能视频播放错误显示" android系统中播放视频后

更新时间:2023-12-03 13:07:04

使用此方法提取视频ID

 公共字符串extract***Id(字符串videoUrl){        字符串VIDEO_ID =;
        如果(videoUrl =空&放大器;!&放大器; videoUrl.trim()长度()大于0){            字符串前pression =^ *((youtu.be。+\\\\ /)+|(V \\\\ /)|(\\\\ / U \\\\ / W \\\\ /)|(嵌入\\\\ /)|(看\\\\))\\\\ ?? v =([^#\\\\&放大器; \\\\?] *)*;??
            CharSequence中输入= videoUrl;
            模式模式= Pattern.compile(如pression,Pattern.CASE_INSENSITIVE);
            匹配匹配= pattern.matcher(输入);
            如果(matcher.matches()){
                串groupIndex1 = matcher.group(7);
                如果(groupIndex1 = NULL&放大器;!&安培; groupIndex1.length()== 11)
                    VIDEO_ID = groupIndex1;
            }
        }        返回VIDEO_ID;    }

更新增加一个监听器

  videoview.setOnErrorListener(新OnErrorListener(){            @覆盖
            公共布尔的onError(MediaPlayer的为arg0,ARG1 INT,INT ARG2){
                // hadle错误
                返回true;
            }
        });

In my code ,display *** video in video viewer.

I am successfully done but end of the video display dialogue "Can't not play video"

here my code

package com.example.videodemo;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends Activity {

    private VideoView videoViewer;
    private ProgressBar mProgressBar;
    String videoUrl ="http://podcast.20min-tv.ch/podcast/20min/199733.mp4";

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

        videoViewer=(VideoView)findViewById(R.id.videoView1);
        //videoViewer.setVideoPath("/sdcard/documentariesandyou.mp4");


       /* Uri uri=Uri.parse(path1);
        http://www.***.com/watch?v=Hxy8BZGQ5Jo
        videoViewer.setVideoURI(uri);   */


        mProgressBar = (ProgressBar) findViewById(R.id.Progressbar);
        mProgressBar.setProgress(0);
        mProgressBar.setMax(100);
        mProgressBar.setVisibility(View.INVISIBLE);

        videoViewer.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {

                Toast.makeText(MainActivity.this, "End Video", Toast.LENGTH_LONG).show();
            }
        });

       new YourAsyncTask().execute();

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();

        Toast.makeText(MainActivity.this, "Pause Video", Toast.LENGTH_LONG).show();

    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();

        Toast.makeText(MainActivity.this, "Stop Video", Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Toast.makeText(MainActivity.this, "Destroy Video", Toast.LENGTH_LONG).show();
        /*WifiManager wifiManager = null;
        wifiManager.disconnect();*/

    }
    private class YourAsyncTask extends AsyncTask<Void, Void, Void>
    {
        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading Video wait...", true);
        }

        @Override
        protected Void doInBackground(Void... params)
        {
            try
            {
                String url = "https://www.***.com/watch?v=SyrO83x7g-E";
                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);
            try {
                progressDialog.dismiss();

                videoViewer.setVideoURI(Uri.parse(videoUrl));
                MediaController mc = new MediaController(MainActivity.this);
                //videoViewer.setMediaController(mc);
                videoViewer.requestFocus();
                videoViewer.start();          
                mc.show();
            } catch (Exception e) {

                e.printStackTrace();
            }
        }

    }

public static String getUrlVideoRTSP(String url***)
    {
        try
        {
            String gdy = "http://gdata.***.com/feeds/api/videos/";
            DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            String id = extract***Id(url***);
            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 = url***;
            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 url***;

    }

protected static String extract***Id(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;
    }
}

Use this method to extract videoid

public String extract***Id(String videoUrl) {

        String video_id = "";
        if (videoUrl != null && videoUrl.trim().length() > 0) {

            String expression = "^.*((youtu.be" + "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*";
            CharSequence input = videoUrl;
            Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(input);
            if (matcher.matches()) {
                String groupIndex1 = matcher.group(7);
                if (groupIndex1 != null && groupIndex1.length() == 11)
                    video_id = groupIndex1;
            }
        }

        return video_id;

    }

UPDATE add one more listener

videoview.setOnErrorListener(new OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
                // hadle the error
                return true;
            }
        });