简体   繁体   English

使用Android Studio在应用中流式传输实时摄像头供稿

[英]Streaming live camera feed in app using android studio

I'm creating an app that I want to stream my foscam live feed in. I'm pretty new to coding and some of this code is over my head. 我正在创建一个应用程序,我想流式传输foscam实时供稿。我对编码foscam ,其中一些代码已经使我foscam I found some help getting this far but now am hitting a snag. 我找到了一些帮助,可以使您走得更远,但现在遇到了麻烦。 The app runs but only displays a black screen. 该应用程序运行,但仅显示黑屏。 I believe i have the manifest and XML code all correct. 我相信我的清单和XML代码都正确。 The problem lies in my code. 问题出在我的代码中。 I hope someone can help me out 我希望有人可以帮助我

 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(); } } 

Make sure that Android's MediaPlayer can actually open and decode your stream. 确保Android的MediaPlayer实际上可以打开并解码您的流。 Right now, if the MediaPlayer cannot handle your stream, you are catching any exception and silently ignoring it: 现在,如果MediaPlayer无法处理您的流,则说明您正在捕获任何异常并无视其忽略:

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

At the very least you should log the error: 至少您应该记录错误:

  } catch (Exception e) {
     Log.e("MyApp", "Could not open data source", e);
  }

Although the MediaPlayer service will most likely pepper the log with its own errors. 尽管MediaPlayer服务很可能会在日志中显示自己的错误。 So what you should do is review the logcat for any messages from the "VideoDecoder" or similar. 因此,您应该做的是查看logcat中是否有来自“ VideoDecoder”或类似内容的任何消息。

To see the logcat in Android Studio, open the "Android Monitor" tab which is on the bottom by default. 要在Android Studio中查看logcat,请默认打开底部的“ Android Monitor”标签。 If you want to see the unfiltered logcat make sure that in the top-right corner of the Android Monitor view it says "No Filters" instead of "Show only selected application" . 如果要查看未过滤的logcat,请确保在Android Monitor视图的右上角显示“无过滤器”,而不是“仅显示选定的应用程序”

I have some new code that links to the Foscam videostream but only grabs the frame when it starts then does not stream. 我有一些链接到Foscam视频流的新代码,但仅在启动时抓取帧,然后不进行流传输。 Im closer but still need help. 我靠近,但仍需要帮助。 Am i on the right track here? 我在这里正确吗?

 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(); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM