【问题标题】:VideoView Playing a file while it is received via socketVideoView 在通过套接字接收文件时播放文件
【发布时间】:2015-03-31 15:23:21
【问题描述】:

我有一个应用程序正在从另一个作为服务器工作的应用程序接收视频文件。当应用程序保存在套接字上接收到的文件时,视频流开始播放文件(正在构建中)。在代码示例中,按下 btnStream 后,按下 btnPlay,App 运行成功。但是,如果播放速率大于下载速率,则会出现错误。我想避免这种情况。因此,我需要在 Video Playing 上有一个侦听器,当它预测会发生此错误时,它将暂停 videoview。我知道一个解决方案,如果我知道视频大小,我可以计算接收到的字节数并监控缓冲了多少秒,看看视频视图是否应该暂停。但是,是否可以在不知道视频文件大小的情况下做到这一点?或者有两个相互依赖的线程?谢谢。

注意:使用的 VideoView 是自定义的,它可以播放 FileDescriptor。

btnStream.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String s = etURL.getText().toString();
                String ip = "10.0.0.24";
                int port = 7878;
                mct= new VideoDownloadTask(ip,port);
                mct.execute();      

            }});
        final MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(mVideoView);


        Button btnPlay = (Button) findViewById(R.id.button2);
        btnPlay.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    mVideoView.setVideoFD((new FileInputStream(new File("/sdcard/tempVideo.mp4")).getFD()));
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                mVideoView.seekTo(0);
                mVideoView.start();

            }
        });
    }

    public class VideoDownloadTask extends AsyncTask<Void, Void, Void> {

        String dstAddress;
        int dstPort;
        String response = "";
        Socket socket=null;

        VideoDownloadTask(String addr, int port){
            dstAddress = addr;
            dstPort = port;
        }

        @Override
        protected Void doInBackground(Void... arg0) {

                try {
                    socket = new Socket(InetAddress.getByName(dstAddress), dstPort);
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    try {
                        if(socket!=null)socket.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }


                File f = new File("/sdcard/tempVideo.mp4");

                try {
                    f.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                DataInputStream in=null;
                try {
                    in = new DataInputStream (socket.getInputStream());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                FileOutputStream videoFile = null;
                try {
                    videoFile = new FileOutputStream(f);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                int len;
                byte buffer[] = new byte[8192];

                try {
                    while((len = in.read(buffer)) != -1) {
                        videoFile.write(buffer, 0, len);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                try {
                    videoFile.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Toast.makeText(getApplicationContext(), "Done Downloading File", 
                       Toast.LENGTH_LONG).show();
            super.onPostExecute(result);
        }

    }

}

【问题讨论】:

    标签: java android sockets video-streaming android-videoview


    【解决方案1】:

    我应用了一个简单的解决方案来解决问题。如果有人遇到同样的问题,我会分享它。解决方案是简单地向videoView 添加一个错误侦听器,它将阻止错误弹出窗口并暂停视频。

    mVideoView.setOnErrorListener(new OnErrorListener(){
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    // TODO Auto-generated method stub
                    statusText.setText("ERROR PLAYING VIDEO");
                    mVideoView.pause();
                    return true;
                }
            });
    

    【讨论】:

      【解决方案2】:
      pDialog = new ProgressDialog(PlayVideoActivity.this);
      pDialog.setTitle("Gajacharitra");
      pDialog.setMessage("Buffering video...");
      pDialog.setIndeterminate(false);
      pDialog.setCancelable(false);
      pDialog.show();
      
      try {
          // Start the MediaController
          mediacontroller.setAnchorView(mVideoView);
          // Get the URL from String VideoURL
          Uri video = Uri.parse(mVideoURL);
          mVideoView.setMediaController(mediacontroller);
          mVideoView.setVideoURI(video);
          mVideoView.requestFocus();
          mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
      
              // Close the progress bar and play the video
              public void onPrepared(MediaPlayer mp) {
      
                  pDialog.dismiss();
                  mVideoView.start();
              }
          });
      
          mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
      
              @Override
              public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
      
                  mVideoView.pause();
                  pDialog.dismiss();
                  Toast.makeText(PlayVideoActivity.this, "Can't play this video.", Toast.LENGTH_LONG).show();
      
                  finish();
                  return true;
              }
          });
      } catch (Exception e) {
      
          /*Log.e("Error", e.getMessage());
          e.printStackTrace();*/
      
          pDialog.dismiss();
          Toast.makeText(PlayVideoActivity.this, "Can't play this video.", Toast.LENGTH_LONG).show();
          finish();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-03
        • 2011-07-28
        • 2013-05-19
        • 1970-01-01
        • 2016-12-08
        • 2016-12-28
        • 2020-12-21
        相关资源
        最近更新 更多