Android 安卓Mjpeg Viwer(IP cam)通过片段
我想在片段页面上查看mjpeg(IP cam), 将代码用作: 当它在活动中是伟大的, 然后我把它变成一个碎片,可以左右滑动, 我将onCreat()移动到onCreaView()并进行了一些修改。 但是当我调用DoRead().execute(URL),应用程序刚刚关闭 它似乎在公共类DoRead中扩展了AsyncTask(String、Void、MjpegInputStream) 会引起一些问题,有人能帮我吗Android 安卓Mjpeg Viwer(IP cam)通过片段,android,android-fragments,ip-camera,mjpeg,Android,Android Fragments,Ip Camera,Mjpeg,我想在片段页面上查看mjpeg(IP cam), 将代码用作: 当它在活动中是伟大的, 然后我把它变成一个碎片,可以左右滑动, 我将onCreat()移动到onCreaView()并进行了一些修改。 但是当我调用DoRead().execute(URL),应用程序刚刚关闭 它似乎在公共类DoRead中扩展了AsyncTask(String、Void、MjpegInputStream) 会引起一些问题,有人能帮我吗 public class MjpegActivity extends Fragme
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)));
}
}
}
公共类活动扩展片段{
私有静态最终布尔调试=false;
私有静态最终字符串标记=“MJPEG”;
私人线路布局;
私人碎片活动;
私有视图mv=null;
字符串URL;
//用于设置(网络和分辨率)
私有静态最终整数请求_设置=0;
私有整数宽度=320;
私人内部高度=240;
私有int ip_ad1=192;
私有int ip_ad2=168;
私有int ip_ad3=2;
私有int ip_ad4=112;
专用int ip_端口=8080;
私有字符串ip_command=“videofeed”;
私有布尔值=false;
最终处理程序=新处理程序();
公共静态活动newInstance(上下文){
MjpegActivity fragmentFirst=新的MjpegActivity();
Bundle args=新Bundle();
fragmentFirst.setArguments(args);
首先返回碎片;
}
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
faActivity=(FragmentActivity)super.getActivity();
llLayout=(线性布局)充气机。充气(R.layout.FRAGENTmjPegviwer,容器,假);
SharedReferences首选项=getActivity().GetSharedReferences(“保存的值”,Context.MODE\u PRIVATE);
宽度=首选项.getInt(“宽度”,宽度);
高度=首选项.getInt(“高度”,高度);
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_命令=preferences.getString(“ip_命令”,ip_命令);
StringBuilder sb=新的StringBuilder();
字符串s_http=“http://”;
字符串s_dot=“.”;
字符串s_冒号=“:”;
字符串s_斜杠=“/”;
sb.追加(s_http);
sb.追加(ip_ad1);
某人附加(s_点);
某人追加(ip_ad2);
某人附加(s_点);
某人追加(ip_ad3);
某人附加(s_点);
附加某人(ip_ad4);
sb.附加(s_冒号);
sb.附加(ip_端口);
某人追加(s_斜杠);
sb.追加(ip_命令);
URL=新字符串(sb);
mv=(MjpegView)llLayout.findviewbyd(R.id.mv);
如果(mv!=null){
mv.设置分辨率(宽度、高度);
}
super.getActivity().setTitle(R.string.title\u连接);
新建DoRead().execute(URL);
返回布局;
}
恢复时公开作废(){
if(DEBUG)Log.d(标记“onResume()”);
super.onResume();
如果(mv!=null){
如果(暂停){
新建DoRead().execute(URL);
暂停=假;
}
}
}
public void onStart(){
if(DEBUG)Log.d(标记“onStart()”);
super.onStart();
}
公共无效暂停(){
if(DEBUG)Log.d(标记“onPause()”);
super.onPause();
如果(mv!=null){
if(mv.isStreaming()){
mv.stopPlayback();
暂停=真;
}
}
}
公共void onStop(){
if(DEBUG)Log.d(标记“onStop()”);
super.onStop();
}
公共空间{
if(DEBUG)Log.d(标记“onDestroy()”);
如果(mv!=null){
mv.FreeCameramery();
}
super.ondestory();
}
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
开关(请求代码){
案例请求设置:
if(resultCode==Activity.RESULT\u确定){
宽度=数据.getIntExtra(“宽度”,宽度);
高度=数据。getIntExtra(“高度”,高度);
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_端口=data.getIntExtra(“ip_端口”,ip_端口);
ip_命令=data.getStringExtra(“ip_命令”);
如果(mv!=null){
mv.设置分辨率(宽度、高度);
}
SharedReferences首选项=getActivity().GetSharedReferences(“保存的值”,Context.MODE\u PRIVATE);
SharedReferences.Editor=首选项.edit();
编辑器.putInt(“宽度”,宽度);
编辑:putInt(“高度”,高度);
编辑:putInt(“ip_ad1”,ip_ad1);
编辑:putInt(“ip_ad2”,ip_ad2);
编辑:putInt(“ip_ad3”,ip_ad3);
编辑:putInt(“ip_ad4”,ip_ad4);
编辑器.putInt(“ip_端口”,ip_端口);
putString(“ip_命令”,ip_命令);
commit();
新建RestarApp().execute();
}
打破
}
}
public void setImageError(){
handler.post(新的Runnable(){
@凌驾
公开募捐{
getActivity().setTitle(R.string.title\u imageerror);
回来
}
});
}
公共类DoRead扩展异步任务{
受保护的MjpegInputStream doInBackground(字符串…url){
//TODO:如果相机有身份验证,请处理它,不要只是不工作
HttpResponse res=null;
DefaultHttpClient httpclient=新的DefaultHttpClient();
HttpParams HttpParams=httpclient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams,5*1000);
HttpConnectionParams.setSoTimeout(httpParams,5*1000);
if(DEBUG)Log.d(标记“1.发送http请求”);
试一试{
res=httpclient.execute(新的HttpGet(URI.create(url[0]))
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)));
}
}
}