使用android studio在应用程序中流式传输相机

时间:2016-05-01 00:50:25

标签: java android surfaceview

我正在创建一个我希望流式传输foscam实时Feed的应用。我对编码非常陌生,而且有些代码在我脑海中。我找到了一些帮助,但现在遇到了麻烦。该应用程序运行但只显示黑屏。我相信我的清单和XML代码都是正确的。问题出在我的代码中。我希望有人可以帮助我



package com.rednak.camerastream;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Base64;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends Activity
implements MediaPlayer.OnPreparedListener,
  SurfaceHolder.Callback {
    final static String USERNAME = "guest";
    final static String PASSWORD = "Guest";
    final static String RTSP_URL = "rtsp://http://rednak71.ddns.net:8090/live1.sdp";

    private MediaPlayer _mediaPlayer;
    private SurfaceHolder _surfaceHolder;

    @
    Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      // Set up a full-screen black window.
      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);
    }

    // More to come…

    /*
    SurfaceHolder.Callback
    */

    @
    Override
    public void surfaceChanged(
      SurfaceHolder sh, int f, int w, int h) {}

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

      Context context = getApplicationContext();
      Map 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 surfaceDestroyed(SurfaceHolder sh) {
      _mediaPlayer.release();
    }

    private Map getRtspHeaders() {
      Map headers = new HashMap();
      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);
    }

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




2 个答案:

答案 0 :(得分:3)

确保Android的MediaPlayer可以实际打开并解码您的信息流。现在,如果MediaPlayer无法处理您的流,您将捕获任何异常并默默忽略它:

<!DOCTYPE html>
<html lang="en">
    <head>
    <title>Bootstrap 101 Template</title>
    <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap -->
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

    <!-- Bootstrap Dropdown Hover CSS -->
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/bootstrap-dropdownhover.css" rel="stylesheet">

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

    <!-- Bootstrap Dropdown Hover JS -->
    <script src="js/bootstrap-dropdownhover.js"></script>
    <style>
    .my_caret {
        position: absolute;
        right: 0;
    }
</style>
    </head>
    <body>
    <div class="container"> 

      <!-- Static navbar -->
      <nav class="navbar navbar-default">
        <div class="container-fluid">
          <div class="navbar-header">
            <button aria-controls="navbar" aria-expanded="false" data-target="#navbar" data-toggle="collapse" class="navbar-toggle collapsed" type="button"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button>
            <a href="#" class="navbar-brand">Project name</a> </div>
          <div class="navbar-collapse collapse" id="navbar" data-toggle="dropdown" data-hover="dropdown" data-animations="zoomIn zoomIn zoomIn zoomIn">
            <ul class="nav navbar-nav">
              <li class="active"><a href="#">Home</a></li>
              <li><a href="#">About</a></li>
              <li><a href="#">Contact</a></li>
              <li class="dropdown"> <a class="dropdown-toggle" href="#">Dropdown <span class="caret"></span></a>
                <ul class="dropdown-menu">
                  <li><a href="#">Action</a></li>
                  <li><a href="#">Another action</a></li>
                  <li><a href="#">Something else here</a></li>
                  <li class="dropdown"> <a href="#">One more separated link<span class="caret my_caret"></span></a>
                    <ul class="dropdown-menu">
                      <li><a href="#">Something else here</a></li>
                      <li class="dropdown"><a href="#">Separated link<span class="caret my_caret"></span></a>
                        <ul class="dropdown-menu">
                          <li><a href="#">Something</a></li>
                        </ul>
                      </li>
                    </ul>
                  </li>
                </ul>
              </li>
            </ul>
          </div>
        </div>
      </nav>
    </div>
</body>

至少应该记录错误:

  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) {}

虽然MediaPlayer服务很可能会使用自己的错误来记录日志。因此,您应该检查logcat以查找来自“VideoDecoder”或类似消息的任何消息。

要在Android Studio中查看logcat,请打开默认情况下位于底部的“Android Monitor”标签。如果您想查看未经过滤的logcat,请确保在Android Monitor视图的右上角显示“No Filters”而不是“仅显示所选应用程序”

答案 1 :(得分:1)

我有一些链接到Foscam视频流的新代码,但只有在启动时才抓取帧然后不会流式传输。我离得更近但仍然需要帮助。我在这里走在正确的轨道上吗?

package com.rednak.camstream;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.VideoView;

public class MainCamActivity extends AppCompatActivity {

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

        VideoView vidView = (VideoView)findViewById(R.id.CamVideoView);

        String vidAddress = "http://rednak71.ddns.net:8090/CGIProxy.fcgi?       cmd=snapPicture2&usr=guest&pwd=guest&t=";
        Uri vidUri = Uri.parse(vidAddress);

        vidView.setVideoURI(vidUri);
        vidView.start();

    }
}