我可以使用MediaPlayer从流线播放视频

时间:2011-07-13 11:47:41

标签: android sockets video streaming

这是我的代码。此代码必须等到Vdo转移然后才能播放

如何修改它以从流线

播放视频
    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();
            }
        });

1 个答案:

答案 0 :(得分:0)

可以录制,但无法在不写文件的情况下直接播放流式视频。 如果您要直接从套接字包文件描述符播放流视频,Mediaplayer将抛出一个setDetaSourceFD异常,因为套接字文件fd不可搜索。