无法逐个循环播放视频 [英] Unable to loop the videos one by one

查看:88
本文介绍了无法逐个循环播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在内部文件夹中有21个视频,通过使用videocursor.getcount()它可以计算所有视频并显示正确的文件路径。

但是当我放置for loop播放所有视频时。但是可以播放所有视频。它只播放最后一个视频。

And它不是在循环播放。

 



  package  com.example.videolist; 

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

public class VideoActivity extends 活动{
private Cursor videocursor;
private int video_column_index;
ListView视频列表;
int count;
String name = null;
VideoView video_player_view;
DisplayMetrics dm;
SurfaceView sur_View;
MediaController media_Controller;

public class MediaFileInfo {

private String fileName,filePath,fileType;

public String getFileName(){
return fileName;
}

public void setFileName( String fileName){
this .fileName = fileName;
}

public String getFilePath(){
return filePath;
}

public void setFilePath( String filePath){
this .filePath = filePath;
}

public String getFileType(){
return fileType;
}

public void setFileType( String fileType){
this .fileType = fileType;
}
}
@覆盖
public < span class =code-keyword> void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
System.gc();

setContentView(R.layout.main);
init_phone_video_grid();
}

private void init_phone_video_grid(){
System.gc();
String [] proj = {MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore。 Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE};
videocursor = getContentResolver()。query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,proj,null,null,null);


count = videocursor.getCount();
Log.d( count:字符串 .valueOf(计数));
videolist =(ListView)findViewById(R.id.VideoList);
videolist.setAdapter( new VideoAdapter(getApplicationContext()));

Log.d( No of Video + count);




for int i = 0; i< count; i ++){
Log.d( i String .valueOf(i));
MediaFileInfo mediaFileInfo = new MediaFileInfo();
video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
videocursor.moveToPosition(i);
// Log.d(getcolumn,String.valueOf(videocursor.moveToPosition(i))) ;
name = videocursor.getString(video_column_index);
mediaFileInfo.setFileName(name);
Log.d( name,name);
int column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
Log.d( column_index .valueOf(Column_Index中));
videocursor.moveToPosition(i);

String filepath = videocursor.getString(column_index);
Log.d( last i字符串 .valueOf(I));
Log.d( filepath,filepath);
mediaFileInfo.setFilePath(filepath);
video_player_view =(VideoView)findViewById(R.id.videoView);
media_Controller = new MediaController( this );
dm = new DisplayMetrics();
this .getWindowManager()。getDefaultDisplay()。getMetrics(dm);
int height = dm.heightPixels;
int width = dm.widthPixels;
video_player_view.setMinimumWidth(width);
video_player_view.setMinimumHeight(height);

video_player_view.setMediaController(media_Controller);
Log.d( last i字符串 .valueOf(I));
Log.d( filepath,filepath);


for (i = 0; i< videocursor.getCount(); i ++){
video_player_view.setVideoPath (文件路径);

video_player_view.requestFocus();
video_player_view.start();

}

video_player_view.setOnCompletionListener( new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer player){
Log.i( VideoView onCompletion() );
/
video_player_view.requestFocus();

video_player_view.start();
}

} );



}

}






public class VideoAdapter extends BaseAdapter {
private Context vContext;

public VideoAdapter(Context c){
vContext = c;
}

public int getCount(){
返回计数;
}

public Object getItem( int position){
返回位置;
}

public long getItemId( int position){
return position;
}
@覆盖
public 查看getView( int 位置,View convertView,ViewGroup parent){
System.gc();

TextView tv = new TextView(vContext.getApplicationContext());

String id = null;
if (convertView == null){
video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
videocursor.moveToPosition(position);
id = videocursor.getString(video_column_index);


tv.setText(id);

} else
tv =(TextView)convertView;
return tv;
}
}

}





我尝试过:



累了,而while while循环没用。

解决方案

评论为暂停



setOnCompletionListener



这是一个问题,不知道你在做什么<>


  for (i = 0; i< videocursor.getcount();我++){> 
video_player_view.setVideoPath(filepath);





删除第二个for循环{}围绕路径,requestfocus并开始。< strike>你甚至可以删除setvideopath,就像你在代码中那样做。我一定看过的东西。



/ Darren


I had 21 videos in the internal folder, by using videocursor.getcount() it was able to count all the videos.and showing correct path for file.
but when i place "for loop" to play the all the videos.but it was eable to play the all the videos.it was only playing last video.
And it was not playing in the loop.


package com.example.videolist;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

public class VideoActivity extends Activity {
      private Cursor videocursor;
      private int video_column_index;
      ListView videolist;
      int count;
          String name = null;
       VideoView video_player_view;
    DisplayMetrics dm;
    SurfaceView sur_View;
    MediaController media_Controller;
    
       public class MediaFileInfo {

           private String fileName,filePath,fileType;

           public String getFileName() {
               return fileName;
           }

           public void setFileName(String fileName) {
               this.fileName = fileName;
           }

           public String getFilePath() {
               return filePath;
           }

           public void setFilePath(String filePath) {
               this.filePath = filePath;
           }

           public String getFileType() {
               return fileType;
           }

           public void setFileType(String fileType) {
               this.fileType = fileType;
           }
       }
       @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
          System.gc();
       
            setContentView(R.layout.main);
            init_phone_video_grid();
      }

      private void init_phone_video_grid() {
            System.gc();
            String[] proj = { MediaStore.Video.Media._ID,
                    MediaStore.Video.Media.DATA,
                    MediaStore.Video.Media.DISPLAY_NAME,
                    MediaStore.Video.Media.SIZE };
                     videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,proj,null, null, null);

          
            count = videocursor.getCount();
            Log.d("count:", String.valueOf(count));
            videolist = (ListView) findViewById(R.id.VideoList);
            videolist.setAdapter(new VideoAdapter(getApplicationContext()));
          
            Log.d("No of Video",""+count);




         for(int i=0; i< count; i++) {
               Log.d("i", String.valueOf(i));
               MediaFileInfo mediaFileInfo =new MediaFileInfo();
          video_column_index =videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
          videocursor.moveToPosition(i);
            // Log.d("getcolumn", String.valueOf(videocursor.moveToPosition(i)));
               name = videocursor.getString(video_column_index);
               mediaFileInfo.setFileName(name);
               Log.d("name",name);
               int column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
               Log.d("column_index", String.valueOf(column_index));
               videocursor.moveToPosition(i);

               String filepath = videocursor.getString(column_index);
             Log.d("last i", String.valueOf(i));
             Log.d("filepath",filepath);
               mediaFileInfo.setFilePath(filepath);
              video_player_view = (VideoView) findViewById(R.id.videoView);
              media_Controller = new MediaController(this);
              dm = new DisplayMetrics();
              this.getWindowManager().getDefaultDisplay().getMetrics(dm);
              int height = dm.heightPixels;
              int width = dm.widthPixels;
              video_player_view.setMinimumWidth(width);
              video_player_view.setMinimumHeight(height);

                  video_player_view.setMediaController(media_Controller);
              Log.d("last i", String.valueOf(i));
              Log.d("filepath",filepath);

         
                     for(i=0;i<videocursor.getCount();i++){
                         video_player_view.setVideoPath(filepath);
                    
                         video_player_view.requestFocus();
                         video_player_view.start();
            
             }

              video_player_view.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                  public void onCompletion(MediaPlayer player) {
                      Log.i("VideoView", "onCompletion()");
                      /
                      video_player_view.requestFocus();

                      video_player_view.start();
                  }

              });



          }

      }






      public class VideoAdapter extends BaseAdapter {
            private Context vContext;

                  public VideoAdapter(Context c) {
                        vContext = c;
            }

            public int getCount() {
                  return count;
            }

            public Object getItem(int position) {
                  return position;
            }

            public long getItemId(int position) {
                  return position;
            }
@Override
            public View getView(int position, View convertView, ViewGroup parent) {
                  System.gc();

                  TextView tv = new TextView(vContext.getApplicationContext());

                  String id = null;
                  if (convertView == null) {
                        video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
                        videocursor.moveToPosition(position);
                       id = videocursor.getString(video_column_index);
                       

                       tv.setText(id);

                  } else
                        tv = (TextView) convertView;
                 return tv;
            }
      }

}



What I have tried:

tired while and do while loop no use.

解决方案

comment out for the time being

setOnCompletionListener

This is a problem, and not sure what you are doing with

for(i=0;i<videocursor.getcount();i++){>
   video_player_view.setVideoPath(filepath);



remove the second for loop {} around the path, requestfocus and start. You can even remove the setvideopath as you do that way above in the code. I must have been seeing things.

/Darren


这篇关于无法逐个循环播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆