开发者

Can I use MediaPlayer play video from stream line

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜