我可以使用的MediaPlayer播放视频的流线 [英] Can I use MediaPlayer play video from stream line

查看:201
本文介绍了我可以使用的MediaPlayer播放视频的流线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code。这code要等到VDO被转移那么它可以播放

如何修改它从流线播放视频

  videoV =(SurfaceView)findViewById(R.id.SurfaceView1);
    Log.d(连接.... 0,);
    SH = videoV.getHolder();
    B =(按钮)findViewById(R.id.button1);
    文件路径= Environment.getExternalStorageDirectory();
    档案文件=新的文件(路径,sample.3gp);    Log.d(连接.... 1,等待套接字);
    尝试{
        Log.e(连接.... 2,);
        袜子=新的Socket(10.4.18.43,5550);
        Log.e(接收视频...,);        最终的FileOutputStream FOS =新的FileOutputStream(文件);
        最后一个字节[]数据=新的字节[1024];        线程t =新主题(新的Runnable(){            @覆盖
            公共无效的run(){
                诠释计数= 0;
                尝试{
                    数= sock.getInputStream()读取(数据,0,data.length)。
                }赶上(IOException异常E1){
                    // TODO自动生成catch块
                    e1.printStackTrace();
                }
                 而(计数!= -1){
                    的System.out.println(计数);
                尝试{
                    fos.write(数据0,计);
                    数= sock.getInputStream()读取(数据,0,data.length)。
                    Log.e(接收,接收);
                    }赶上(IOException异常五){
                    e.printStackTrace();
                }                }
                 Log.e(接收,完成);
            }
        });
        t.start();    }赶上(E3的UnknownHostException){
        // TODO自动生成catch块
        e3.printStackTrace();
    }赶上(IOException异常E3){
        // TODO自动生成catch块
        e3.printStackTrace();
    }
    sh.addCallback(本);
    熔点为新的MediaPlayer();
    尝试{        mp.setDataSource(file.getAbsolutePath());
        //mp.setDataSource(ParcelFileDescriptor.fromSocket(袜子).getFileDescriptor());
        mp.setDisplay(SH);
    }赶上(抛出:IllegalArgumentException E1){
        e1.printStackTrace();
    }赶上(E1 IllegalStateException异常){
        e1.printStackTrace();
    }赶上(IOException异常E1){
        e1.printStackTrace();
    }     b.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                尝试{
                    MP prepare()。
                    //mp.$p$ppareAsync();
                }赶上(IllegalStateException异常五){
                    e.printStackTrace();
                }赶上(IOException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }                mp.start();
            }
        });


解决方案

录音是可能的,但你不能直接播放,而无需编写文件的视频流。
如果你要直接从插座包裹文件描述符播放流视频,媒体播放器将抛出一个setDetaSourceFD例外,因为套接字文件fd是不可搜索。

Here is my code. This code have to wait until Vdo is transferred then it can play

How can i modify it to play video from stream line

    videoV = (SurfaceView) findViewById(R.id.SurfaceView1);
    Log.d("Connecting.... 0 " , "");
    sh = videoV.getHolder();
    b = (Button)findViewById(R.id.button1);
    File path = Environment.getExternalStorageDirectory();
    File file = new File(path, "sample.3gp");

    Log.d("Connecting.... 1 " , " Wait For Socket");
    try {
        Log.e("Connecting.... 2" , "");
        sock = new Socket("10.4.18.43", 5550);
        Log.e("Receiving video..." , "");

        final FileOutputStream fos = new FileOutputStream(file);
        final byte[] data = new byte[1024];

        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                int count = 0;
                try {
                    count = sock.getInputStream().read(data, 0, data.length);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                 while (count != -1) {
                    System.out.println(count);
                try {
                    fos.write(data, 0, count);
                    count = sock.getInputStream().read(data, 0, data.length);
                    Log.e("Receiving", "Receiving");
                    } catch (IOException e) {
                    e.printStackTrace();
                }

                }
                 Log.e("Receiving", "Finish");
            }
        });
        t.start();



    } catch (UnknownHostException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    } catch (IOException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }


    sh.addCallback(this);     
    mp = new MediaPlayer();
    try {

        mp.setDataSource(file.getAbsolutePath());
        //mp.setDataSource(ParcelFileDescriptor.fromSocket(sock).getFileDescriptor());
        mp.setDisplay(sh);
    } catch (IllegalArgumentException e1) {
        e1.printStackTrace();
    } catch (IllegalStateException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

     b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                try {
                    mp.prepare();
                    //mp.prepareAsync();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                mp.start();
            }
        });

解决方案

Recording is possible, but you can't play streaming video directly without writing file. If you are going to play stream video directly from socket parcel file descriptor, Mediaplayer will throw a setDetaSourceFD exception because socket file fd is not seekable.

这篇关于我可以使用的MediaPlayer播放视频的流线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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