虽然它是通过插座接收VideoView播放文件 [英] VideoView Playing a file while it is received via socket

查看:167
本文介绍了虽然它是通过插座接收VideoView播放文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从另一个应用程序被工作作为服务器接收视频文件的应用程序。而应用程序是拯救套接字上接收的文件,视频流开始播放文件(正在建设中)。在code样品,之后我preSS的btnStream,我preSS的btnPlay和App成功运行。但是,如果播放速度比下载速度越大,就会发生错误。我想避免这种情况。所以,我需要对视频播放,这将暂停,当$会出现此错误p $ pdicts的videoview的监听器。我知道一个解决方案,如果我知道视频的大小,我可以对抗收到的字节数和监视多少秒被缓存,看看是否应该videoview暂停或不。但是,是有可能做到这一点不知道视频文件的大小?或具有两个线程依赖于彼此?谢谢。

请注意:使用VideoView是一个自定义的地方可以玩的FileDescriptor。

  btnStream.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                字符串s = etURL.getText()的toString()。
                串IP =10.0.0.24;
                INT端口= 7878;
                MCT =新VideoDownloadTask(IP,端口);
                mct.execute();            }});
        最终的MediaController的MediaController =新的MediaController(本);
        mediaController.setAnchorView(mVideoView);
        按钮btnPlay =(按钮)findViewById(R.id.button2);
        btnP​​lay.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                尝试{
                    mVideoView.setVideoFD((新的FileInputStream(新文件(/ SD卡/ tempVideo.mp4))getFD()));
                }赶上(FileNotFoundException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }赶上(IOException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }
                mVideoView.seekTo(0);
                mVideoView.start();            }
        });
    }    公共类VideoDownloadTask扩展的AsyncTask<太虚,太虚,太虚> {        串dstAddress;
        INT dstPort;
        串响应=;
        Socket套接字= NULL;        VideoDownloadTask(字符串地址,端口INT){
            dstAddress =地址;
            dstPort =口;
        }        @覆盖
        保护无效doInBackground(虚空......为arg0){                尝试{
                    插座=新的Socket(InetAddress.getByName(dstAddress),dstPort);
                }赶上(UnknownHostException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }赶上(IOException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                    尝试{
                        如果(插座!= NULL)socket.close();
                    }赶上(IOException异常E1){
                        // TODO自动生成catch块
                        e1.printStackTrace();
                    }
                }
                文件f =新的文件(/ SD卡/ tempVideo.mp4);                尝试{
                    f.createNewFile();
                }赶上(IOException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }
                DataInputStream以在= NULL;
                尝试{
                    在=新DataInputStream所(socket.getInputStream());
                }赶上(IOException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }
                FileOutputStream中的录像档案= NULL;
                尝试{
                    录像档案=新的FileOutputStream(F);
                }赶上(FileNotFoundException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }
                INT LEN;
                字节的缓冲区[] =新的字节[8192];                尝试{
                    而((LEN = in.read(缓冲液))!= - 1){
                        videoFile.write(缓冲液,0,LEN);
                    }
                }赶上(IOException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }                尝试{
                    videoFile.close();
                }赶上(IOException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }
                尝试{
                    socket.close();
                }赶上(IOException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }            返回null;
        }        @覆盖
        保护无效onPostExecute(虚空结果){
            Toast.makeText(getApplicationContext(),完成下载文件,
                       Toast.LENGTH_LONG).show();
            super.onPostExecute(结果);
        }    }}


解决方案

我申请了一个简单的解决方案,解决了这个问题。如果有人有相同的问题,我分享。解决的办法是根本的错误监听器添加到 videoView ,这将阻止错误弹出式广告和暂停视频。

  mVideoView.setOnErrorListener(新OnErrorListener(){
            @覆盖
            公共布尔的onError(MediaPlayer的熔点,诠释了什么,整型附加){
                // TODO自动生成方法存根
                statusText.setText(ERROR播放视频);
                mVideoView.pause();
                返回true;
            }
        });

I have an App that is receiving a video file from another App that is working as a Server. While the App is saving the file received on the socket, the video stream starts playing the file (which is under construction). In the code sample, after I press the btnStream, I press the btnPlay and App runs successfully. However, if the playing rate is greater than the download rate, an error will occur. I want to avoid this case. So I need to have a listener on the Video Playing that will pause the videoview when it predicts that this error will occur. I know a solution where if I know the video size, I can counter the bytes received and monitor how many seconds have been buffered and see if the videoview should pause or not. However, is it possible to do it without knowing the video file size? Or having two threads that depends on each other? Thanks.

Note: the VideoView used is a custom one where it can play 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);
        }

    }

}

解决方案

I applied a simple solution that resolved the problem. I am sharing it if anyone is having the same problem. The solution was simply to add an error listener to the videoView that will block the error popups and pauses the video.

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

这篇关于虽然它是通过插座接收VideoView播放文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆