问题
I’m developping an android app to monitor some IP cameras. I’m using the MjpegView Class to stream the video.
I’ve three cameras.
- - Camera 1: A public camera i found on internet, without user/password.
- - Camera 2: A public camera but this one require username/password.
- - Camera 3: The camera I’m going to use finally in my app. It will also ask for credentials.
The code in my main activity is the following:
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 stream the camera 1 without problems. When I run the app with the cameras 2 or 3 there are no errors neither warnings but the most I get is a black screen. I thougth it was a problem with the authentification but if I remove it from my camera I get the same result, the black screen.
What is the difference between the cameras that makes some of them work but not others?
Thanks in advance for any help.
--- EDIT ---
I've found something weird while running the app with the camera 2.
I catch an exception in MjpegView
class when it calls the method MjpegInputStream.readMjpegFrame
.
Looking deeper I notice that the method getEndOfSeqeunce
always return 1 while Camera 1 (the one which works well) return higher values (between 66 and 68).
I hope this can give someone an idea of what is happening here...
回答1:
Finally I solved it!
I don't know why it didn't work the first time I tried to stream this camera removing the Authentication. But today I've tried again and now it works.
So now the problem was in the Authentication. It's not possible to add the credentials in the URL like in a browser.
I just modified the MjpegInputStream
to set the credentials in the 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's working
回答2:
This works too if you don't want to enter 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)));
来源:https://stackoverflow.com/questions/14695905/black-screen-while-trying-to-stream-ip-camera-in-android