我正在尝试在 VideoView 和 MediaController 的帮助下实现视频播放器。我尝试读取我用相机录制的 mp4 文件,该文件存储在我的 sdcard 上,还有一个来自远程服务器的文件。但是每次我收到一个对话框,指出“无法播放此视频”。这些是源代码。
MainActivity.java
@SuppressLint("ValidFragment")
public class VideoPlayFragment extends Fragment
{
private VideoView mVideoView;
private ImageView mPlayButtonImageView,mThumbnailImageView;
private MediaController mMediaController;
Context mCntx;
String mUrl;
public VideoPlayFragment(Context context, String mPhotoURLString)
{
// TODO Auto-generated constructor stub
this.mCntx=context;
this.mUrl=mPhotoURLString;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.video_play_layout, null);
initUI(view);
return view;
}
private void initUI(View view)
{
// TODO Auto-generated method stub
mVideoView=(VideoView) view.findViewById(R.id.video_play_layout_video_view);
mPlayButtonImageView=(ImageView) view.findViewById(R.id.video_play_layout_play_button);
mThumbnailImageView=(ImageView) view.findViewById(R.id.video_play_layout_thumnail);
Bitmap bmThumbnail;
bmThumbnail = ThumbnailUtils.createVideoThumbnail(mUrl, Thumbnails.FULL_SCREEN_KIND);
mThumbnailImageView.setImageBitmap(bmThumbnail);
mPlayButtonImageView.setOnClickListener(new OnClickListener()
{
@Override public void onClick(View v)
{
// TODO Auto-generated method stub
mPlayButtonImageView.setVisibility(View.GONE);
mThumbnailImageView.setVisibility(View.GONE);
mVideoView.setVisibility(View.VISIBLE);
try {
mMediaController = new MediaController(mCntx);
mMediaController.setAnchorView(mVideoView);
Uri video = Uri.parse(mUrl);
mVideoView.setMediaController(mMediaController);
mVideoView.setVideoURI(video);
mVideoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
}
});
}catch(Exception e){
System.out.println("Video Play Error :"+e.getMessage());
}
}
});
}
}
布局代码——
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<VideoView
android:id="@+id/video_play_layout_video_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="gone"/>
<ImageView
android:id="@+id/video_play_layout_thumnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:contentDescription="@string/app_name"
android:layout_marginBottom="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<ImageView
android:id="@+id/video_play_layout_play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="@string/app_name"
android:src="@drawable/playbttn" />
</RelativeLayout>
日志 -
10-11 16:16:24.108: E/MediaPlayer(7429): Error (1,-2147483648)
10-11 16:16:24.108: D/VideoView(7429): Error: 1,-2147483648
请有人建议任何其他方式来执行此操作,因为我在大多数教程中都找到了与上面代码类似的相同示例。