I am trying to implement a video player with the help of VideoView and MediaController. I have tried reading a mp4 file that I have recorded with my camera which is stored on my sdcard and also a file from a remote server. But everytime I am getting a dialog stating that "Can't play this video". These are the source codes.

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());
             }
        }
    });
}
}

Layout code--

<?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>

Log--

10-11 16:16:24.108: E/MediaPlayer(7429): Error (1,-2147483648)
10-11 16:16:24.108: D/VideoView(7429): Error: 1,-2147483648

Please can some one suggest any other way of doing this as I am finding the same examples similar to code above in most of the tutorials.

有帮助吗?

解决方案

You are going to so complicated to play a video. Please see the below link. It will be easy for you to play a video

How to play video file using VideoView in Android

Also check the below link also

Android video player example

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top