Android Mjpeg Viwer(IP-камера) по фрагментам
Я хочу просмотреть mjpeg (IP-камеру) на странице фрагмента, используйте код как: https://bitbucket.org/neuralassembly/simplemjpegview
Когда он в действии велик, тогда я превращаю его во фрагмент, который я могу перемещать влево и вправо, я перемещаю onCreat() в onCreaView() и делаю некоторые изменения. Но когда я вызываю DoRead(). Execute(URL);, приложение просто выключилось,
Кажется, в общедоступном классе DoRead расширяет AsyncTask (String, Void, MjpegInputStream)
return new MjpegInputStream(res.getEntity().getContent());
вызовет некоторые проблемы, кто-то может мне помочь?
public class MjpegActivity extends Fragment {
private static final boolean DEBUG=false;
private static final String TAG = "MJPEG";
private LinearLayout llLayout;
private FragmentActivity faActivity;
private MjpegView mv = null;
String URL;
// for settings (network and resolution)
private static final int REQUEST_SETTINGS = 0;
private int width = 320 ;
private int height = 240 ;
private int ip_ad1 = 192;
private int ip_ad2 = 168;
private int ip_ad3 = 2;
private int ip_ad4 = 112;
private int ip_port = 8080;
private String ip_command = "videofeed";
private boolean suspending = false;
final Handler handler = new Handler();
public static MjpegActivity newInstance(Context context) {
MjpegActivity fragmentFirst = new MjpegActivity();
Bundle args = new Bundle();
fragmentFirst.setArguments(args);
return fragmentFirst;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
faActivity = (FragmentActivity) super.getActivity();
llLayout = (LinearLayout) inflater.inflate(R.layout.fragment_mjpegviwer, container, false);
SharedPreferences preferences = getActivity().getSharedPreferences("SAVED_VALUES", Context.MODE_PRIVATE);
width = preferences.getInt("width", width);
height = preferences.getInt("height", height);
ip_ad1 = preferences.getInt("ip_ad1", ip_ad1);
ip_ad2 = preferences.getInt("ip_ad2", ip_ad2);
ip_ad3 = preferences.getInt("ip_ad3", ip_ad3);
ip_ad4 = preferences.getInt("ip_ad4", ip_ad4);
ip_port = preferences.getInt("ip_port", ip_port);
ip_command = preferences.getString("ip_command", ip_command);
StringBuilder sb = new StringBuilder();
String s_http = "http://";
String s_dot = ".";
String s_colon = ":";
String s_slash = "/";
sb.append(s_http);
sb.append(ip_ad1);
sb.append(s_dot);
sb.append(ip_ad2);
sb.append(s_dot);
sb.append(ip_ad3);
sb.append(s_dot);
sb.append(ip_ad4);
sb.append(s_colon);
sb.append(ip_port);
sb.append(s_slash);
sb.append(ip_command);
URL = new String(sb);
mv = (MjpegView) llLayout.findViewById(R.id.mv);
if(mv != null){
mv.setResolution(width, height);
}
super.getActivity().setTitle(R.string.title_connecting);
new DoRead().execute(URL);
return llLayout;
}
public void onResume() {
if(DEBUG) Log.d(TAG,"onResume()");
super.onResume();
if(mv!=null){
if(suspending){
new DoRead().execute(URL);
suspending = false;
}
}
}
public void onStart() {
if(DEBUG) Log.d(TAG,"onStart()");
super.onStart();
}
public void onPause() {
if(DEBUG) Log.d(TAG,"onPause()");
super.onPause();
if(mv!=null){
if(mv.isStreaming()){
mv.stopPlayback();
suspending = true;
}
}
}
public void onStop() {
if(DEBUG) Log.d(TAG,"onStop()");
super.onStop();
}
public void onDestroy() {
if(DEBUG) Log.d(TAG,"onDestroy()");
if(mv!=null){
mv.freeCameraMemory();
}
super.onDestroy();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_SETTINGS:
if (resultCode == Activity.RESULT_OK) {
width = data.getIntExtra("width", width);
height = data.getIntExtra("height", height);
ip_ad1 = data.getIntExtra("ip_ad1", ip_ad1);
ip_ad2 = data.getIntExtra("ip_ad2", ip_ad2);
ip_ad3 = data.getIntExtra("ip_ad3", ip_ad3);
ip_ad4 = data.getIntExtra("ip_ad4", ip_ad4);
ip_port = data.getIntExtra("ip_port", ip_port);
ip_command = data.getStringExtra("ip_command");
if(mv!=null){
mv.setResolution(width, height);
}
SharedPreferences preferences = getActivity().getSharedPreferences("SAVED_VALUES", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("width", width);
editor.putInt("height", height);
editor.putInt("ip_ad1", ip_ad1);
editor.putInt("ip_ad2", ip_ad2);
editor.putInt("ip_ad3", ip_ad3);
editor.putInt("ip_ad4", ip_ad4);
editor.putInt("ip_port", ip_port);
editor.putString("ip_command", ip_command);
editor.commit();
new RestartApp().execute();
}
break;
}
}
public void setImageError(){
handler.post(new Runnable() {
@Override
public void run() {
getActivity().setTitle(R.string.title_imageerror);
return;
}
});
}
public class DoRead extends AsyncTask<String, Void, MjpegInputStream> {
protected MjpegInputStream doInBackground(String... url) {
//TODO: if camera has authentication deal with it and don't just not work
HttpResponse res = null;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpParams httpParams = httpclient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5*1000);
HttpConnectionParams.setSoTimeout(httpParams, 5*1000);
if(DEBUG) Log.d(TAG, "1. Sending http request");
try {
res = httpclient.execute(new HttpGet(URI.create(url[0])));
if(DEBUG) Log.d(TAG, "2. Request finished, status = " + res.getStatusLine().getStatusCode());
if(res.getStatusLine().getStatusCode()==401){
//You must turn off camera User Access Control before this will work
return null;
}
return new MjpegInputStream(res.getEntity().getContent());
} catch (ClientProtocolException e) {
if(DEBUG){
e.printStackTrace();
Log.d(TAG, "Request failed-ClientProtocolException", e);
}
//Error connecting to camera
} catch (IOException e) {
if(DEBUG){
e.printStackTrace();
Log.d(TAG, "Request failed-IOException", e);
}
//Error connecting to camera
}
return null;
}
protected void onPostExecute(MjpegInputStream result) {
mv.setSource(result);
if(result!=null){
result.setSkip(1);
getActivity().setTitle(R.string.app_name);
}else{
getActivity().setTitle(R.string.title_disconnected);
}
mv.setDisplayMode(MjpegView.SIZE_BEST_FIT);
mv.showFps(false);
}
}
public class RestartApp extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... v) {
//MjpegActivity.this.finish();
return null;
}
protected void onPostExecute(Void v) {
//startActivity((new Intent(MjpegActivity.this, MjpegActivity.class)));
}
}
}
Бревно получит это: 04-07 04: 29: 42.199: DEBUG / MJPEG (4462): onStart ()
04-07 04: 29: 42.199: DEBUG / MJPEG (4462): onResume ()
04-07 04: 29: 42.203: DEBUG / MJPEG (4462): 1. Отправка http-запроса
04-07 04:29:42.391: DEBUG/MJPEG(4462): 2. Запрос выполнен, статус = 200
04-07 04: 29: 42.464: DEBUG / MJPEG (4462): onPause ()
04-07 04: 29: 43.213: DEBUG / MJPEG (4462): onStop ()
04-07 04: 29: 43.224: DEBUG / MJPEG (4462): onDestroy ()