Videoview不播放视频

时间:2013-07-09 14:11:30

标签: android android-videoview

我有播放视频的方法:

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

第一次调用方法时,正在播放视频。

但是当我试图呼叫videoView.start()时,任何其他时间都没有发生。

以下是视频观看的初始化

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

2 个答案:

答案 0 :(得分:0)

试试这个。对我来说没问题。

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


将您的视频放入 res / raw 文件夹中。然后..

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

当然可以在真实设备中而不是在模拟器中尝试。

答案 1 :(得分:0)

这是我正在使用的代码

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