在Android应用程序中实现视频

时间:2015-01-07 14:35:12

标签: android

这是我的视频观看代码

package com.example.videoviewsample;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;

   public class VideoViewSample extends Activity
{
    private VideoView mVideoView;

    private MediaController mController;

    MediaMetadataRetriever mMetadataRetriever;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mVideoView = (VideoView) findViewById(R.id.myplaysurface);

        mMetadataRetriever = new MediaMetadataRetriever();

        Intent intent = new Intent();

        intent.setType("video/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);

        startActivityForResult(Intent.createChooser(intent, "Video File to Play"), 0);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
    }

    public void startPlayback(String videoPath)
    {
        mMetadataRetriever.setDataSource(videoPath);

        Uri uri = Uri.parse(videoPath);
        mVideoView.setVideoURI(uri);

        mController = new MediaController(this, false);
        mVideoView.setMediaController(mController);
        mVideoView.requestFocus();
        mVideoView.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.video_view_sample, menu);

        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == 0)
        {
            if (resultCode == RESULT_OK)
            {
                Uri sourceUri = data.getData();
                String source = getPath(sourceUri);

                startPlayback(source);
            }
        }
    }

    public String getPath(Uri uri)
    {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);

        if (cursor == null)
        {
            return uri.getPath();
        } else
        {
            cursor.moveToFirst();

            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);

            return cursor.getString(idx);
        }**strong text**`enter code here`
    }
}

LogCat错误

01-07 19:46:45.231: E/AndroidRuntime(30303): Process: 
com.example.videoviewsample, PID: 30303
01-07 19:46:45.231: E/AndroidRuntime(30303): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/video:31385 flg=0x1 }} to activity {com.example.videoviewsample/com.example.videoviewsample.VideoViewSample}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

1 个答案:

答案 0 :(得分:0)

正如您的logcat错误跟踪所示

Couldn't read row 0, col -1 from CursorWindow

这意味着getColumnIndex()无法在Cursor中找到指定的列,并且-1作为列索引返回。

请检查您在cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);中传递的列名是否正确。

修改

如果您要播放视频,那么为什么使用MediaStore.Images.ImageColumns.DATA。我不确定,但似乎不对。难道不应该是MediaStore.Video.Media.DATA吗?

像这样使用

String[] col = { MediaStore.Video.Media.DATA };
Cursor cursor = getContentResolver().query(uri, col, null, null, null);