如何使用网址在我的应用上播放视频

时间:2016-05-18 06:34:00

标签: android android-studio

当我使用我的内部存储播放视频时,它工作正常,但是当我提供URL时,这表示无法播放此视频。请提供建议。提前谢谢。

    public class Video-demo extends Activity {
      private static final String path ="http://demo.digi-corp.com/S2LWebservice/Resources/SampleVideo.mp4";
      private VideoView video;
        private MediaController ctlr;
       @Override
       public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            getWindow().setFormat(PixelFormat.TRANSLUCENT);
            setContentView(R.layout.videoview);

            video = (VideoView) findViewById(R.id.video);
            video.setVideoPath(path);

            ctlr = new MediaController(this);
            ctlr.setMediaPlayer(video);
            video.setMediaController(ctlr);
            video.requestFocus();
     }
}

1 个答案:

答案 0 :(得分:0)

试试这个

String path = "http://demo.digi-corp.com/S2LWebservice/Resources/SampleVideo.mp4";

Uri uri = Uri.parse(path);

VideoView vv = (VideoView) findViewById(R.id.videoView1);
vv.setOnErrorListener(new OnErrorListener() {
    @Override
    public boolean onError(MediaPlayer arg0, int arg1, int arg2) {return false;}
});
try {vv.setVideoURI(uri);} catch (Exception e) {}
try {vv.start();} catch (Exception e) {}
vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.setLooping(true);
        mp.setOnCompletionListener(null);
    }
});

并在 AndroidManifest.xml

中添加这些权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>