1

I have this method for playing the video:

private void changeVideoState() {
    playing = !playing;
    if (playing) {
        videoView.start();
        play.setVisibility(View.GONE);
        stop.setVisibility(View.VISIBLE);
    } else {
        videoView.stopPlayback();
        stop.setVisibility(View.GONE);
        play.setVisibility(View.VISIBLE);
    }
}

At the first time when the method is called the video is being played.

But any other time when I try to call videoView.start() nothing happens.

Here is the initialization of the videoview:

    videoView = (VideoView) findViewById(R.id.preview);
    MediaController mc = new MediaController(getActivity());
    // mc.setAnchorView(videoView);
    mc.setMediaPlayer(videoView);
    videoView.setMediaController(mc);
    videoView.setVideoURI(Uri.fromFile(file));
user1940676
  • 4,348
  • 9
  • 44
  • 73

2 Answers2

0

Try this one.It works fine for me..

VideoView videoView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    videoView = (VideoView)findViewById(R.id.videoView);        
    videoView.setVideoPath("/sdcard/sample.3gp");
    videoView.start();  
}

Or,
Put your video in res/raw folder.And then..

public void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       showVideo();
   }
private void showVideo()
{
    VideoView vd = (VideoView)findViewById(R.id.videoView);
    Uri uri = Uri.parse("android.resource://package/"+R.raw.movie);
    MediaController mc = new MediaController(this);
    vd.setMediaController(mc);
    vd.setVideoURI(uri);
    vd.start();
}

Of course try it in a real device rather than in emulator.

ridoy
  • 6,274
  • 2
  • 29
  • 60
  • at the first time it works, but after i stop the video 'stopPlayback()` and then call `videoView.start();` again it doesn't work. – user1940676 Jul 10 '13 at 07:41
  • See http://stackoverflow.com/questions/9290477/videoview-stop-display-when-activity-resumed-and-restart – ridoy Jul 10 '13 at 07:46
  • I have a 'stop' and 'start' buttons , when clicking on one it should stop and start the video again. – user1940676 Jul 10 '13 at 07:48
  • In your changeVideoState() you need to set your videoView in MediaController object before you declare videoView.start(); – ridoy Jul 10 '13 at 07:55
  • I have changed my code, doing that and it didn't help. `mc.setMediaPlayer(videoView); videoView.setMediaController(mc);` – user1940676 Jul 10 '13 at 09:19
  • try to use my above showVideo() and then call it..if (playing) { showVideo(); play.setVisibility(View.GONE); stop.setVisibility(View.VISIBLE); } – ridoy Jul 10 '13 at 09:37
0

this is code which i am using

    String _video_link ="http://techslides.com/demos/sample-videos/small.mp4";

    vv = (VideoView)findViewById(R.id.vv);

    try {
        MediaController mediacontroller = new MediaController(this);
        mediacontroller.setAnchorView(vv);

        Uri uri = Uri.parse(_video_link);
        vv.setMediaController(mediacontroller);
        vv.setVideoURI(uri);
        vv.seekTo(1);

    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }

    vv.requestFocus();
    vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            mp.setLooping(true);
            vv.start();
        }
    });
    vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp)
        {
            vv.resume();
        }
    });