2017-08-03 91 views
1

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

下面的代码正在工作,但问题是只有第一个视频播放循环。 我想一个接一个地播放所有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(); 
    } 

} 
} 

回答

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;

+0

感谢Budddy我已经想通了......! :p然后,谢谢你,至少你回答了! @Eselfar –