如何在同一页面播放视频(媒体播放器)

发布于 2024-10-07 05:18:14 字数 6524 浏览 2 评论 0原文

嗨,我创建了一个简单的媒体播放器......现在它工作正常。我在图库中检索视频缩略图。当我单击缩略图时,视频将在其他页面中播放...如果我单击缩略图视频播放同一页面...请参阅我的屏幕截图...我的视频在那个红色框中播放...这种类型的框架如何创建.....

这是我的屏幕截图:alt text

如果我单击缩略图,视频将播放另一页: 该屏幕截图: alt text

这是我的编码:

public class videothumb extends Activity  
{
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 Gallery _gallery; 
private Cursor _cursor;
private int _columnIndex;
private int[] _videosId;
private Uri _contentUri;
private int video_column_index;

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

    setContentView(R.layout.main);


    //set GridView for gallery
    _gallery = (Gallery) 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(videogridlistener);
    }
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();
        System.out.println("total"+_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)
    {
        showToast(ex.getMessage().toString());            
    }

}
protected void showToast(String msg)
{
    Toast.makeText(_context, msg, Toast.LENGTH_LONG).show();
}
private AdapterView.OnItemClickListener videogridlistener = 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);
          //System.gc();
         // video_column_index = 
           _cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
          _columnIndex = _cursor.getColumnIndex(MEDIA_DATA);
            // Lets move to the selected item in the cursor
            _cursor.moveToPosition(position);

          String filename = _cursor.getString(_columnIndex);
          Intent intent = new Intent(videothumb.this, ViewVideo.class);
          intent.putExtra("videofilename", filename);
          startActivity(intent);
          showToast(filename);
         // Toast.makeText(videothumb.this, "" + position, Toast.LENGTH_SHORT).show();

        }
       };
    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.setAdjustViewBounds(true);
            imgVw.setBackgroundColor(Color.WHITE);
            imgVw.setLayoutParams(new Gallery.LayoutParams(150, 100));
            imgVw.setPadding(5,5,5,5);
            imgVw.setScaleType(ImageView.ScaleType.FIT_XY);
    }
        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);
    System.out.println("ff"+MediaStore.Video.Thumbnails.
  getThumbnail(getContentResolver(),id, MediaStore.Video.Thumbnails.MICRO_KIND, null));
        return thumb;
       }
      }
     }

编码 2:`

public class ViewVideo extends Activity {
private String filename;
private VideoView Video;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

      setContentView(R.layout.main1);
    //setContentView(new Zoom(this));
    Video=(VideoView)findViewById(R.id.VideoView01);
    System.gc();
    Intent i = getIntent();
    Bundle extras = i.getExtras();
    filename = extras.getString("videofilename");
    Video.setVideoPath(filename);

    Video.setMediaController(new MediaController(this));
    Video.requestFocus();
    Video.start();
   }

   } 

任何人都可以帮助我...

hi i have create one simple media player.... now its working fine. i retrieve video thumbnails in gallery. when i click the thumbnails video will play in other page... if i click thumbnails video play same page...see my screen shot....my video play in that red box...this type of frame how to create.....

this is my screen shot:alt text

if i click thumbnails the video will play another page:
that screen shot:
alt text

this is for my coding:

public class videothumb extends Activity  
{
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 Gallery _gallery; 
private Cursor _cursor;
private int _columnIndex;
private int[] _videosId;
private Uri _contentUri;
private int video_column_index;

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

    setContentView(R.layout.main);


    //set GridView for gallery
    _gallery = (Gallery) 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(videogridlistener);
    }
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();
        System.out.println("total"+_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)
    {
        showToast(ex.getMessage().toString());            
    }

}
protected void showToast(String msg)
{
    Toast.makeText(_context, msg, Toast.LENGTH_LONG).show();
}
private AdapterView.OnItemClickListener videogridlistener = 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);
          //System.gc();
         // video_column_index = 
           _cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
          _columnIndex = _cursor.getColumnIndex(MEDIA_DATA);
            // Lets move to the selected item in the cursor
            _cursor.moveToPosition(position);

          String filename = _cursor.getString(_columnIndex);
          Intent intent = new Intent(videothumb.this, ViewVideo.class);
          intent.putExtra("videofilename", filename);
          startActivity(intent);
          showToast(filename);
         // Toast.makeText(videothumb.this, "" + position, Toast.LENGTH_SHORT).show();

        }
       };
    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.setAdjustViewBounds(true);
            imgVw.setBackgroundColor(Color.WHITE);
            imgVw.setLayoutParams(new Gallery.LayoutParams(150, 100));
            imgVw.setPadding(5,5,5,5);
            imgVw.setScaleType(ImageView.ScaleType.FIT_XY);
    }
        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);
    System.out.println("ff"+MediaStore.Video.Thumbnails.
  getThumbnail(getContentResolver(),id, MediaStore.Video.Thumbnails.MICRO_KIND, null));
        return thumb;
       }
      }
     }

coding 2:`

public class ViewVideo extends Activity {
private String filename;
private VideoView Video;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

      setContentView(R.layout.main1);
    //setContentView(new Zoom(this));
    Video=(VideoView)findViewById(R.id.VideoView01);
    System.gc();
    Intent i = getIntent();
    Bundle extras = i.getExtras();
    filename = extras.getString("videofilename");
    Video.setVideoPath(filename);

    Video.setMediaController(new MediaController(this));
    Video.requestFocus();
    Video.start();
   }

   } 

anyone help me...

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

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

发布评论

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

评论(2

莫多说 2024-10-14 05:18:14
You want to use Videoview

<VideoView android:id="@+id/Videoview1"
    android:layout_marginTop="100px"
    android:layout_marginLeft="10px" 
    android:layout_width="20px" 
    android:layout_height="200px"/>

and create object in your activity
and play.
videoview.start();
like this.

Hope this will help you
You want to use Videoview

<VideoView android:id="@+id/Videoview1"
    android:layout_marginTop="100px"
    android:layout_marginLeft="10px" 
    android:layout_width="20px" 
    android:layout_height="200px"/>

and create object in your activity
and play.
videoview.start();
like this.

Hope this will help you
浮云落日 2024-10-14 05:18:14
private VideoView mVView;

mVView= (VideoView) findViewById(R.id.Videoview1);

mVView.setVideoPath("sdcard/filename.mp4");
mVView.start();

private VideoView mVView;

mVView= (VideoView) findViewById(R.id.Videoview1);

mVView.setVideoPath("sdcard/filename.mp4");
mVView.start();

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