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

时间:2017-08-03 13:32:09

标签: android video android-videoview android-sdcard

我在视频视频中成功播放了一个视频。 现在我想循环播放2个或更多视频。 视频从服务器下载并保存在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 个答案:

答案 0 :(得分:0)

如果您想要播放下一个视频,则需要增加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数组的范围内。如果是,videoIncrementer设置为videoIncrementer,否则重置为0;