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.
我不使用database。這是在應用程序中我唯一的活動。 – Sachin
那麼在這種情況下,您在內容解析器遊標中傳遞的列名肯定是錯誤的。 – Rohit5k2
請參閱我的編輯,如果有幫助。 – Rohit5k2