2015-01-07 107 views
2

這是我的視頻視圖實現視頻視圖

包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. 

回答

0

當你的logcat的錯誤跟蹤說

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

這意味着getColumnIndex()無法找到光標並指定列-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); 
+0

我不使用database。這是在應用程序中我唯一的活動。 – Sachin

+0

那麼在這種情況下,您在內容解析器遊標中傳遞的列名肯定是錯誤的。 – Rohit5k2

+0

請參閱我的編輯,如果有幫助。 – Rohit5k2