代码之家  ›  专栏  ›  技术社区  ›  MazRoid

VideoView是否循环播放多个视频?

  •  1
  • MazRoid  · 技术社区  · 8 年前

    我已经成功地在视频视图中播放了一个视频。 现在我想在一个循环中播放两个或更多视频。 视频正在从服务器下载并保存在SD卡中,我想从那里播放。

    我想一个接一个地循环播放所有3个视频。

    我的代码

    public class display extends Activity implements MediaPlayer.OnPreparedListener ,MediaPlayer.OnCompletionListener {
    
    Uri Uvid;
    io.vov.vitamio.MediaPlayer mplyer;
    private SurfaceHolder vidHolder;
    SurfaceView Sview;
    Uri uri;
    File[] files;
    int videoIncrementer = 0, i = 0;
    String[] tempPath;
    String videolink[];
    String id;
    private ProgressDialog pDialog;
    JSONParser jsonParser = new JSONParser();
    File file, dir, root;
    
    private static final String VIDEO_URL = "http://eazeltv.com/admin/api/videonew.php";
    private SharedPreference sharedPreference;
    VideoView Vvid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!io.vov.vitamio.LibsChecker.checkVitamioLibs(this))
            return;
        setContentView(R.layout.activity_display);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        Vvid = (VideoView) findViewById(R.id.videoV);
    
        pDialog = new ProgressDialog(display.this);
        sharedPreference = new SharedPreference();
        id = sharedPreference.getValue(display.this);
    
    
       new DownloadFileFromURL().execute();
    }
    
    
    
    
    
    class DownloadFileFromURL extends AsyncTask<String, String, String> {
        List params;
        /**
         * Before starting background thread Show Progress Bar Dialog
         */
        @Override
        public void onPreExecute() {
            super.onPreExecute();
            pDialog.setMessage("Downloading Video...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
    
            // id = bundle.getString("id");
            params = new ArrayList();
            params.add(new BasicNameValuePair("id", id));
    
            System.out.println("=====Mazhar==VideoplayerID=====" + id);
    
        }
    
        /**
         * Downloading file in background thread
         */
        @Override
        public String doInBackground(String... f_url) {
    
            // File cacheDir = getBaseContext().getCacheDir();
            // File tempFile = new File(cacheDir.getPath());
    
            System.out.println("=====mazhar==Downloading file in background thread==" + dir);
    
            root = android.os.Environment.getExternalStorageDirectory();
            dir = new File(root.getAbsolutePath() + "/myvideo");
            if (dir.exists() == false) {
                dir.mkdirs();
            }
            files = dir.listFiles();
            tempPath = new String[files.length];
    
            if (files.length > 0) {
                System.out.println("=====mazhar==length thread==");
                int i = 0;
                for (File file : files) {
                    tempPath[i] = file.getAbsolutePath();
    
                    System.out.println("=====mazhar==Downloading==" + tempPath[i]);
    
                    if (file.isDirectory()) {
                        // inFiles.addAll(getListFiles(file));
                        if (file.getName().endsWith(".mp4")) {
                            // inFiles.add(file);
                            Toast.makeText(display.this, file.getName(), Toast.LENGTH_SHORT).show();
                            System.out.println("=====mazhar==in background thread==" + file);
                        }
    
                    }
                    i++;
                }
            } else {
    
                JSONArray Jarray = null;
                JSONObject json = jsonParser.makeHttpRequest(VIDEO_URL, "POST", params);
                if (json != null) {
                    System.out.println("=====Mazhar==JsonVideo=start===" + json);
                    try {
                        Jarray = json.getJSONArray("data");
                        System.out.println("===Mazhar==VideoJArray=start===" + Jarray);
                    } catch (JSONException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    videolink = new String[Jarray.length()];
    
    
                    System.out.println("=====mazhar====Jarray===" + Jarray.length());
    
                    downloadfile(videolink, Jarray);
    
                } else {
                    System.out.println("====Mazhar===VideoURL==JSON Null");
                }
            }
            return null;
        }
    
        /**
         * Updating progress bar
         */
        @Override
        public void onProgressUpdate(String... progress) {
            // setting progress percentage
            // pDialog.setProgress(Integer.parseInt(progress[0]));
    
        }
    
        /**
         * After completing background task Dismiss the progress dialog
         **/
        @Override
        public void onPostExecute(String file_url) {
    
            playVideo();
    
            pDialog.dismiss();
        }
    
    }
    
    
    
    private void downloadfile(String[] VLink, JSONArray Jarray) {
    
        for (int j = 0; j < Jarray.length(); j++) {
    
            try {
    
                JSONObject Jasonobject = Jarray.getJSONObject(j);
                System.out.println("=====Mazda==VideoLinks=====" + Jasonobject);
    
                // type[j] = Jasonobject.getString("type");
                // System.out.println("=====Mazhar==TypeLinks=11111===="+type[j]);
                videolink[j] = Jasonobject.getString("video_link");
                // videolink = videolink.replaceAll("/", "");
                videolink[j] = videolink[j].trim().replaceAll(" ", "%20");
                // jsonFormattedString.add(videolink);
                System.out.println("==arrayV==Mazhar===" + videolink[j]);
                // videolink = json.getString("video_link");
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        // temppath tha
        tempPath = new String[Jarray.length()];
    
        // Toast.makeText(getApplicationContext(),"Temp Path is" + tempPath ,
        System.out.println("===Mazhar==Temp Path is :" + tempPath[i]);
        // Toast.LENGTH_LONG).show();
        for (int i = 0; i < Jarray.length(); i++) {
            try {
                storeSDCard(videolink[i], i);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }
    
    private void storeSDCard(String path, int arrayIndex) throws IOException {
        try {
            Log.d("TwoSizeVideo", "storeSDCard");
            URLConnection cn = new URL(path).openConnection();
            cn.connect();
            InputStream istream = cn.getInputStream();
    
            // try {
            // System.out.println("=====storeSDCard==Type==="+type[arrayIndex]);
            // if(type[arrayIndex].equals("mp4")){
    
            File savePath = new File(dir, "");
            System.out.println("=====storeSDCard=====" + dir);
    
            file = File.createTempFile("AdMovies", ".mp4", savePath);
            tempPath[arrayIndex] = file.getAbsolutePath();
            System.out.println("===storeSDCard==tempath===" + tempPath[arrayIndex]);
    
            FileOutputStream out = new FileOutputStream(file);
            byte buf[] = new byte[16384];
            do {
                int numread = istream.read(buf);
                if (numread <= 0)
                    break;
                out.write(buf, 0, numread);
    
            } while (true);
            out.close();
            istream.close();
    
        } catch (Exception e) {
            System.out.println("===catch===StoreCard==" + e);
        }
    }
    
    
    public void playVideo()
    {
        System.out.println("PlayVideo");
        uri = Uri.parse(tempPath[videoIncrementer]);
        System.out.println("TempPath" + tempPath[videoIncrementer]);
        MediaController mediaController = new MediaController(this);
        Vvid.setVideoURI(uri);
        Vvid.requestFocus();
        Vvid.setMediaController(mediaController);
        Vvid.setOnCompletionListener(this);
        Vvid.setOnPreparedListener(this);
        Vvid.start();
    
    }
    
    
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.setLooping(true);
    }
    
    @Override
    public void onCompletion(MediaPlayer mp) {
        try {
        mp.stop();
        mp.reset();
        mp.setDataSource(tempPath[videoIncrementer]);
        mp.prepare();
        mp.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Eselfar    8 年前

    你需要增加你的 videoIncrementer 如果你想播放下一个视频。

        @Override
    public void onCompletion(MediaPlayer mp) {
        try {
        mp.stop();
        mp.reset();
        // Add this line
        videoIncrementer = ++videoIncrementer < tempPath.length ? videoIncrementer : 0;
        mp.setDataSource(tempPath[videoIncrementer]);
        mp.prepare();
        mp.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    

    ++videoIncrementer

    < tempPath.length 检查是否增加了 videoIncrement tempPath 设置为 视频增量器 否则重置为0;