在Android上流式传输视频

时间:2013-05-24 07:32:56

标签: java android stream video-streaming rtsp

我试图将桌面部分流式传输到我的Android设备。

我通过VLC媒体播放器创建流。我用2048 Kb /秒和30 fps构建H.264流,将其打包到mp4 / mov容器中并按如下方式发布:rtsp:// [my-ip]:[port] / stream。重要的是,我不想简单地从媒体服务器流式传输文件,但我希望通过具有属性的流来实现我的桌面的实时翻译,如上所述。

根据此处的Android文档http://developer.android.com/guide/appendix/media-formats.html,必须可以在Android设备上通过RTSP流消费H.264视频(我的设备在4.2上运行)。

但是我的VideoView小部件说未找到视频,而我可以通过此处安装的VLC播放器实际看到来自另一台PC的流。那么我怎样才能达到我的初步目标呢?

这是我的Android应用程序代码:

main_layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <VideoView
        android:id="@+id/videoRectangle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="100dp" />

    <Button
        android:id="@+id/connectToVideoServerButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/videoRectangle"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:text="Присоединиться" />

    <EditText
        android:id="@+id/streamServerAddress"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignBaseline="@+id/connectToVideoServerButton"
        android:layout_alignBottom="@+id/connectToVideoServerButton"
        android:layout_marginLeft="30dp"
        android:layout_toRightOf="@+id/connectToVideoServerButton"
        android:ems="10"
        android:hint=""
        android:text="" >

        <requestFocus />
    </EditText>

</RelativeLayout>

MainActivity.java:

package com.example.videostreamerclient;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.VideoView;

public class MainActivity extends Activity {

    private VideoView videoView;
    private Button videoServerConnectBtn;
    private EditText streamServerAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        videoView = (VideoView) findViewById(R.id.videoRectangle);
        streamServerAddress = (EditText) findViewById(R.id.streamServerAddress);
        videoServerConnectBtn = (Button) findViewById(R.id.connectToVideoServerButton);

        videoServerConnectBtn.setOnClickListener(videoServerConnectBtnClicked);     
    }

    private View.OnClickListener videoServerConnectBtnClicked = new View.OnClickListener() {
        @Override
        public void onClick(View v){
            if ( streamServerAddress.getText().toString() != "" )
            {
                videoView.setVideoPath( streamServerAddress.getText().toString() );
                videoView.start();
            }           
        }
    };  

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

0 个答案:

没有答案