简体   繁体   中英

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. 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. 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. Right now, if the MediaPlayer cannot handle your stream, you are catching any exception and silently ignoring it:

  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. So what you should do is review the logcat for any messages from the "VideoDecoder" or similar.

To see the logcat in Android Studio, open the "Android Monitor" tab which is on the bottom by default. 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" .

I have some new code that links to the Foscam videostream but only grabs the frame when it starts then does not stream. 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(); } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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