Black screen when trying to stream IP camera in Android
I am developing an android application to monitor some IP cameras. Im using MjpegView Class for video streaming.
I've three cameras.
- - Camera 1: A public camera that I found on the internet, no user / password.
- - Camera 2: public camera, but this requires username / password.
- - Camera 3: The camera that I will be using is finally in my application. It will also ask for credentials.
The code in my main activity is as follows:
public class MainActivity extends Activity {
private MjpegView mv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Camera 1
String URL = "http://216.62.222.101/mjpg/video.mjpg";
//Camera 2
// String URL = "http://user:user@iprobocam.marmitek.com/cgi/mjpg/mjpg.cgi";
//Camera 3
// String URL = "http://MyIp:MyPort/mjpg/video.mjpg";
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
mv = new MjpegView(this);
setContentView(mv);
mv.setSource(MjpegInputStream.read(URL));
mv.setDisplayMode(MjpegView.SIZE_BEST_FIT);
mv.showFps(true);
}
public void onPause() {
super.onPause();
mv.stopPlayback();
}
I can work with camera 1 without any problems. When I launch the app with cameras 2 or 3, there are no errors, no warning, but I get a black screen. I figured it was an authentication issue, but if I remove it from my camera, I get the same result, black screen.
What is the difference between cameras that make some of them work but not others?
Thanks in advance for your help.
--- EDIT ---
I found something strange while running my camera app 2. I am throwing an exception in the class MjpegView
when it calls a method MjpegInputStream.readMjpegFrame
. Looking deeper, I notice that the method getEndOfSeqeunce
always returns 1, while camera 1 (the one that works well) returns higher values (between 66 and 68).
Hopefully this can give someone an idea of what is going on here ...
source to share
Finally I solved it!
I don't know why it didn't work the first time I tried to transfer this camera by removing the authentication. But today I tried again and now it works.
So now the problem was with authentication. Can't add credentials to url like in browser.
I just changed MjpegInputStream
to set credentials to HTTPClient
:
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(new AuthScope(host, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password));
res = httpclient.execute(new HttpGet(URI.create(url)));
And yes finally it works
source to share
This also works if you don't want to enter the hostname.
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("username", "password");
provider.setCredentials(AuthScope.ANY, credentials);
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(provider);
res = httpclient.execute(new HttpGet(URI.create(url)));
source to share