Android rtsp直播视频从IP cam无法正常工作

时间:2016-03-06 14:17:44

标签: android streaming rtsp

我有一个IP摄像头(高清无线N立方体网络摄像机 - D-LINK),我试图使用应用程序流我开发的问题是我得到以下错误,我所看到的是一个黑屏。

03-06 16:00:33.434 22670-22670/com.shai.ipcamdemo W/MediaPlayer: Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No content provider: rtsp://xx.xx.xx.151:554/live1.sdp 

03-06 16:00:33.451 22670-22682/com.shai.ipcamdemo W/MediaPlayer: info/warning (701, 0)

03-06 16:00:35.297 22670-22682/com.shai.ipcamdemo W/AudioSystem: AudioFlinger server died! 

03-06 16:00:35.297 22670-22681/com.shai.ipcamdemo W/IMediaDeathNotifier: media server died 

03-06 16:00:35.297 22670-22681/com.shai.ipcamdemo E/MediaPlayer: error (100, 0)

03-06 16:00:35.298 22670-22670/com.shai.ipcamdemo E/MediaPlayer: Error (100,0)

这是视频配置:D-Link config我在Nexus 5设备上进行测试。

我知道已经提出了类似的问题,但我没有找到可以帮助我的相同或一个。

public class MainActivity extends Activity implements MediaPlayer.OnPreparedListener, SurfaceHolder.Callback{

    final static String USERNAME = "admin";
    final static String PASSWORD = "xxxxx";
    final static String RTSP_URL = "rtsp://xx.xx.xx.151:554/live1.sdp";

    private MediaPlayer _mediaPlayer;
    private SurfaceHolder _surfaceHolder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = getWindow();
        window.setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.setBackgroundDrawableResource(android.R.color.black);

        setContentView(R.layout.activity_main);

        // Configure the view that renders live video.
        SurfaceView surfaceView =
                (SurfaceView) findViewById(R.id.surfaceView);
        _surfaceHolder = surfaceView.getHolder();
        _surfaceHolder.addCallback(this);
        _surfaceHolder.setFixedSize(320, 240);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        _mediaPlayer = new MediaPlayer();
        _mediaPlayer.setDisplay(_surfaceHolder);

        Context context = getApplicationContext();
        Map<String, String> headers = getRtspHeaders();
        Uri source = Uri.parse(RTSP_URL);

        try {
            // Specify the IP camera's URL and auth headers.
            _mediaPlayer.setDataSource(context, source, headers);

            // Begin the process of setting up a video stream.
            _mediaPlayer.setOnPreparedListener(this);
            _mediaPlayer.prepareAsync();
        }
        catch (Exception e) {}
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        _mediaPlayer.release();
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        _mediaPlayer.start();
    }

    private Map<String, String> getRtspHeaders() {
        Map<String, String> headers = new HashMap<String, String>();
        String basicAuthValue = getBasicAuthValue(USERNAME, PASSWORD);
        headers.put("Authorization", basicAuthValue);
        return headers;
    }

    private String getBasicAuthValue(String usr, String pwd) {
        String credentials = usr + ":" + pwd;
        int flags = Base64.URL_SAFE | Base64.NO_WRAP;
        byte[] bytes = credentials.getBytes();
        return "Basic " + Base64.encodeToString(bytes, flags);
    }
}

清单

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.shai.ipcamdemo">

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.RECORD_AUDIO" />

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

布局

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.shai.ipcamdemo.MainActivity">

 <SurfaceView
     android:id="@+id/surfaceView"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

问题是并非所有Android设备都支持.sdp transfer:(