如何播放从 SD 卡中选择的视频?

发布于 2024-11-19 06:48:09 字数 5345 浏览 2 评论 0原文

    public class VideoDemo extends Activity {
        private VideoView video;
        private MediaController ctlr;

        File clip=new File(Environment.getExternalStorageDirectory();
        {

                if (clip.exists()) {
                video=(VideoView)findViewById(R.id.videoGrdView);
                video.setVideoPath(clip.getAbsolutePath());

                ctlr=new MediaController(this);
                ctlr.setMediaPlayer(video);
                video.setMediaController(ctlr);
                video.requestFocus();
                video.start();
        }
   }};
}

因此,

我的 SD 卡上的所有视频都有一个 VideoGrdView,可以在单独的活动中显示,现在我需要知道如何从网格中单击视频并使其通过此媒体播放器播放。任何帮助表示赞赏。

    public class Menus extends Activity {
//set constants for MediaStore to query, and show videos
private final static Uri MEDIA_EXTERNAL_CONTENT_URI =      
    MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
private final static String _ID = MediaStore.Video.Media._ID;
private final static String MEDIA_DATA = MediaStore.Video.Media.DATA;
//flag for which one is used for images selection
private GridView _gallery; 
private Cursor _cursor;
private int _columnIndex;
private int[] _videosId;
private Uri _contentUri;


protected Context _context;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    _context = getApplicationContext();
    _gallery = (GridView) findViewById(R.id.videoGrdVw);
    //set default as external/sdcard uri
    _contentUri = MEDIA_EXTERNAL_CONTENT_URI;
    //initialize the videos uri 
    //showToast(_contentUri.getPath());
    initVideosId();
    //set gallery adapter
    setGalleryAdapter();
}
private void setGalleryAdapter() {
    _gallery.setAdapter(new VideoGalleryAdapter(_context));
    _gallery.setOnItemClickListener(_itemClickLis);

}
private AdapterView.OnItemClickListener _itemClickLis = new OnItemClickListener() 
{
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
    {
        // Now we want to actually get the data location of the file
        String [] proj={MEDIA_DATA};
        // We request our cursor again
        _cursor = managedQuery(_contentUri,
                proj, // Which columns to return
                null,       // WHERE clause; which rows to return (all rows)
                null,       // WHERE clause selection arguments (none)
                null); // Order-by clause (ascending by name)
        // We want to get the column index for the data uri
        int count = _cursor.getCount();
        //
        _cursor.moveToFirst();
        //
        _columnIndex = _cursor.getColumnIndex(MEDIA_DATA);
        // Lets move to the selected item in the cursor
        _cursor.moveToPosition(position);
        startActivity(new Intent("com.ave"));
        }
};
private void initVideosId() {
    try
    {
        //Here we set up a string array of the thumbnail ID column we want to get back
        String [] proj={_ID};
        // Now we create the cursor pointing to the external thumbnail store
        _cursor = managedQuery(_contentUri,
                proj, // Which columns to return
                null,       // WHERE clause; which rows to return (all rows)
                null,       // WHERE clause selection arguments (none)
                null); // Order-by clause (ascending by name)
        int count= _cursor.getCount();
        // We now get the column index of the thumbnail id
        _columnIndex = _cursor.getColumnIndex(_ID);
        //initialize 
        _videosId = new int[count];
        //move position to first element
        _cursor.moveToFirst();          
        for(int i=0;i<count;i++)
        {           
            int id = _cursor.getInt(_columnIndex);
            //
            _videosId[i]= id;
            //
            _cursor.moveToNext();
            //
        }
    }catch(Exception ex)
    {

    }

}


//
private class VideoGalleryAdapter extends BaseAdapter
{
    public VideoGalleryAdapter(Context c) 
    {
        _context = c;
    }
    public int getCount() 
    {
        return _videosId.length;
    }
    public Object getItem(int position) 
    {
        return position;
    }
    public long getItemId(int position) 
    {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ImageView imgVw= new ImageView(_context);;
        try
        {
            if(convertView!=null)
            {
                imgVw= (ImageView) convertView;
            }
            imgVw.setImageBitmap(getImage(_videosId[position]));
            imgVw.setLayoutParams(new GridView.LayoutParams(96, 96));
            imgVw.setPadding(8, 8, 8, 8);
        }
        catch(Exception ex)
        {
            System.out.println("StartActivity:getView()-135: ex " + ex.getClass() +", "+ ex.getMessage());
        }
        return imgVw;
    }

    // Create the thumbnail on the fly
    private Bitmap getImage(int id) {
        Bitmap thumb = MediaStore.Video.Thumbnails.getThumbnail(
                getContentResolver(),
                id, MediaStore.Video.Thumbnails.MICRO_KIND, null);
        return thumb;
    }

}

}

    public class VideoDemo extends Activity {
        private VideoView video;
        private MediaController ctlr;

        File clip=new File(Environment.getExternalStorageDirectory();
        {

                if (clip.exists()) {
                video=(VideoView)findViewById(R.id.videoGrdView);
                video.setVideoPath(clip.getAbsolutePath());

                ctlr=new MediaController(this);
                ctlr.setMediaPlayer(video);
                video.setMediaController(ctlr);
                video.requestFocus();
                video.start();
        }
   }};
}

}

So i've got a VideoGrdView of all videos on my sd card to display in a separate activity, now i need to know how to click a video from the grid and have it play through this media player. Any help is appreciated.

    public class Menus extends Activity {
//set constants for MediaStore to query, and show videos
private final static Uri MEDIA_EXTERNAL_CONTENT_URI =      
    MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
private final static String _ID = MediaStore.Video.Media._ID;
private final static String MEDIA_DATA = MediaStore.Video.Media.DATA;
//flag for which one is used for images selection
private GridView _gallery; 
private Cursor _cursor;
private int _columnIndex;
private int[] _videosId;
private Uri _contentUri;


protected Context _context;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    _context = getApplicationContext();
    _gallery = (GridView) findViewById(R.id.videoGrdVw);
    //set default as external/sdcard uri
    _contentUri = MEDIA_EXTERNAL_CONTENT_URI;
    //initialize the videos uri 
    //showToast(_contentUri.getPath());
    initVideosId();
    //set gallery adapter
    setGalleryAdapter();
}
private void setGalleryAdapter() {
    _gallery.setAdapter(new VideoGalleryAdapter(_context));
    _gallery.setOnItemClickListener(_itemClickLis);

}
private AdapterView.OnItemClickListener _itemClickLis = new OnItemClickListener() 
{
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
    {
        // Now we want to actually get the data location of the file
        String [] proj={MEDIA_DATA};
        // We request our cursor again
        _cursor = managedQuery(_contentUri,
                proj, // Which columns to return
                null,       // WHERE clause; which rows to return (all rows)
                null,       // WHERE clause selection arguments (none)
                null); // Order-by clause (ascending by name)
        // We want to get the column index for the data uri
        int count = _cursor.getCount();
        //
        _cursor.moveToFirst();
        //
        _columnIndex = _cursor.getColumnIndex(MEDIA_DATA);
        // Lets move to the selected item in the cursor
        _cursor.moveToPosition(position);
        startActivity(new Intent("com.ave"));
        }
};
private void initVideosId() {
    try
    {
        //Here we set up a string array of the thumbnail ID column we want to get back
        String [] proj={_ID};
        // Now we create the cursor pointing to the external thumbnail store
        _cursor = managedQuery(_contentUri,
                proj, // Which columns to return
                null,       // WHERE clause; which rows to return (all rows)
                null,       // WHERE clause selection arguments (none)
                null); // Order-by clause (ascending by name)
        int count= _cursor.getCount();
        // We now get the column index of the thumbnail id
        _columnIndex = _cursor.getColumnIndex(_ID);
        //initialize 
        _videosId = new int[count];
        //move position to first element
        _cursor.moveToFirst();          
        for(int i=0;i<count;i++)
        {           
            int id = _cursor.getInt(_columnIndex);
            //
            _videosId[i]= id;
            //
            _cursor.moveToNext();
            //
        }
    }catch(Exception ex)
    {

    }

}


//
private class VideoGalleryAdapter extends BaseAdapter
{
    public VideoGalleryAdapter(Context c) 
    {
        _context = c;
    }
    public int getCount() 
    {
        return _videosId.length;
    }
    public Object getItem(int position) 
    {
        return position;
    }
    public long getItemId(int position) 
    {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ImageView imgVw= new ImageView(_context);;
        try
        {
            if(convertView!=null)
            {
                imgVw= (ImageView) convertView;
            }
            imgVw.setImageBitmap(getImage(_videosId[position]));
            imgVw.setLayoutParams(new GridView.LayoutParams(96, 96));
            imgVw.setPadding(8, 8, 8, 8);
        }
        catch(Exception ex)
        {
            System.out.println("StartActivity:getView()-135: ex " + ex.getClass() +", "+ ex.getMessage());
        }
        return imgVw;
    }

    // Create the thumbnail on the fly
    private Bitmap getImage(int id) {
        Bitmap thumb = MediaStore.Video.Thumbnails.getThumbnail(
                getContentResolver(),
                id, MediaStore.Video.Thumbnails.MICRO_KIND, null);
        return thumb;
    }

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×纯※雪 2024-11-26 06:48:09

Gallery 的工作方式与 ListView 几乎相同。在 onItemClick 方法中,您应该能够知道单击了哪个特定项目。获取该项目的 Uri/绝对路径,并将该信息传递给下一个活动。

在 VideoDemo 类中,提取此 Uri/路径并将其设置为 VideoView。

Gallery, pretty much works the same way as a ListView. Inside the onItemClick method, you should be able to know which specific item was clicked. Get the Uri/absolute path for that item, and pass on that information to the next activity.

In the VideoDemo class, extract this Uri/path and set it to the VideoView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文