播放网络视频(当Activity的背景)

注:SurfaceView+MediaPlayer来实现视频播放(播放网络视频的是会有一个加载的过程(也就是“黑屏”),后面我的解决办法是把视频下载下来,在加载。目前只想到这样办法,有其他办法希望来一起讨论)

一、加载本地视频(不会卡顿,也不会黑屏)

二、加载网络视频当界面背景(没下载的时候,有加载(也就是有所谓的黑屏))

三、加载网络视频当界面背景(提前下载视频到本地)不会出现黑屏,循环播放的时候也不会卡顿

    1、主要先在MainActivity.class下载网络视频(

          new load_video() .execute("http://ohfn56j2u.bkt.clouddn.com/584139b1ac294.mp4");//先下载网络视频)

       

         
  /**
     * 下载网络视频
     */
    class load_video extends AsyncTask
    
    
     
      {
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(String... params) {
            int count;
            for (int i = 0; i < params.length; i++) {
                try {
                    URL url = new URL(params[i]);
                    URLConnection conection = url.openConnection();
                    conection.connect();
                    int lenghtOfFile = conection.getContentLength();

                    // download the file
                    InputStream input = new BufferedInputStream(
                            url.openStream(), 8192);// 1024*8
                    File f = new File(Environment.getExternalStorageDirectory()
                            + "/jiaoju");
                    if (f.isDirectory()) {
                        System.out.println("exist!");
                    } else {
                        System.out.println("not exist!");
                        f.mkdirs();
                    }
                    // Output stream
                    OutputStream output = new FileOutputStream(Environment
                            .getExternalStorageDirectory().toString()
                            + "/jiaoju/splash.mp4");

                    byte data[] = new byte[1024];
                    while ((count = input.read(data)) != -1) {
                        // writing data to file
                        output.write(data, 0, count);
                    }

                    // flushing output
                    output.flush();

                    // closing streams
                    output.close();
                    input.close();

                } catch (Exception e) {
                    Log.e("Error: ", e.getMessage());
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
//            VideoView videoView = (VideoView) findViewById(R.id.videoview);
//            File videoFile = new File(Environment.getExternalStorageDirectory()
//                    .toString() + "/jiaoju/splash.mp4");
//            videoView.setVideoPath(videoFile.getPath());
//            videoView.start();
        }

    }
    
    


    2、然后在PhotographyNet.class播放本地视频

        File videoFile = new File(Environment.getExternalStorageDirectory().toString() + "/jiaoju/splash.mp4");

        mediaPlayer.setDataSource(videoFile.getPath());//加载网络视频
        mediaPlayer.setLooping(true);//循环播放

     

四、用第三方框架(PLVideoTextureView)循环播放(循环播放的时候,会卡顿)

      (由于时间比较急,没有整合在一起)第四种方法请看这个链接的的源码:

         http://download.csdn.net/detail/qq_30543115/9744452

五、用VideoView播放(VideoView的本质就是SurfaceView)



源码下载:http://download.csdn.net/detail/qq_30543115/9744455


有更好的办法的盆友,欢迎一起讨论哈。欢迎指正




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值