1

我有这种播放视频的方法:

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 = (VideoView) findViewById(R.id.preview);
    MediaController mc = new MediaController(getActivity());
    // mc.setAnchorView(videoView);
    mc.setMediaPlayer(videoView);
    videoView.setMediaController(mc);
    videoView.setVideoURI(Uri.fromFile(file));
4

2 回答 2

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

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

于 2013-07-10T07:38:07.353 回答
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();
        }
    });
于 2018-05-21T12:09:12.280 回答