简体   繁体   中英

Playing video in videoview android

I am trying to play a video in a video view. The xml code contains the video view as:

<VideoView
    android:id="@+id/vvIntroVideo"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:layout_weight="38" />

and in the Java part of code I have:

public class LoginPage extends Activity {

private VideoView introVideoView;

private static String DEBUG = LoginPage.class.getSimpleName();

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

    //initializing part
    introVideoView = (VideoView) findViewById(R.id.vvIntroVideo);

    try {
        introPlayer();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.v(DEBUG, e.getMessage().toString());
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login_page, menu);
    return true;
}

public void introPlayer(){
    Uri video = Uri.parse("android:resource://"+getPackageName()+"/"+R.raw.documentariesandyou);
    introVideoView.setVideoURI(video);
    introVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {
            // TODO Auto-generated method stub
            mp.setLooping(true);
        }
    });
    introVideoView.start();
}
}

The application shows the error that the video cannot be played. The video is in MP4 so there might not be problem in that. Can anyone help me in this case.

First define ProgressDialog mProgressDialog;

Then try this code,it worked for me:

video = (VideoView) findViewById(R.id.VideoView1);
        final MediaController mc = new MediaController(this);
        mc.setAnchorView(video);
        mc.setMediaPlayer(video);
        Uri uri = Uri.parse(path1);
        video.setMediaController(mc);
        System.out.println("!-- uri: "+uri.toString());
        video.setVideoURI(uri);

        video.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {



                   mProgressDialog.dismiss();






            }
        });

        video.start();

        video.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {

            }

        });


    }


    public void showLoaderProgress(){
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("Loading Video\nPlease wait...");
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

where path1 is your video path.

As you are a beginer i am giving you code to play video in videoview,this will play both videos from raw folder as well from server url with live streaming. to play local just load URI to vieoview rest all thing are same.its very easy to understand.i hope it will help you.

globals in activity

private MediaController controller;
ProgressBar progressBar = null;
private String _url;

Oncreate

 VideoView video = (VideoView) findViewById(R.id.videoView1);
    controller = new MediaController(VideoScreen.this);
    video.setMediaController(controller);

    video.setVideoPath(_url);
    video.requestFocus();
    video.start();
    progressBar.setVisibility(View.VISIBLE);
    video.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            // TODO Auto-generated method stub
            mp.start();
            progressBar.setVisibility(View.GONE);
            mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
                @Override
                public void onVideoSizeChanged(MediaPlayer mp, int arg1,
                        int arg2) {
                    progressBar.setVisibility(View.GONE);
                    mp.start();
                }
            });

        }
    });

I found the solution. Actually the problem was in encoding of video. I was using gingerbread emulator to test application and the video was in mp4 h.264. But it seems gingerbread is capable in playing mp4 or 3gpp h.263 only in default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM