日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

[转]将Android变成一个网络摄像机:spydroid-ipcamera;能实时传输,几乎没有延迟...

發布時間:2024/3/12 Android 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转]将Android变成一个网络摄像机:spydroid-ipcamera;能实时传输,几乎没有延迟... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
spydroid-ipcamera這個項目能夠將Android設備變成一個漂亮的網絡攝像機 ip camera。Spydroid是一個很小的app,能夠將手機的攝像頭和麥克風streams至你的瀏覽器或VLC。它是市場上最強大的工具,一種方法用來從智能手機傳輸音頻/視頻到您的電腦。H.264支持分辨率高達1080p和在手機上運行ICS或JB就能夠支持AAC格式。

需要注意的是,此解決方案僅限于局域網,要想在公網環境下實現視頻監控,則需要STUN協議的支持。

源碼片段

public class SpydroidActivity extends FragmentActivity {

static final public String TAG = "SpydroidActivity";

public final int HANDSET = 0x01;
public final int TABLET = 0x02;

// We assume that the device is a phone
public int device = HANDSET;

private ViewPager mViewPager;
private PowerManager.WakeLock mWakeLock;
private SectionsPagerAdapter mAdapter;
private SurfaceView mSurfaceView;
private SpydroidApplication mApplication;
private CustomHttpServer mHttpServer;
private RtspServer mRtspServer;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mApplication = (SpydroidApplication) getApplication();

setContentView(R.layout.spydroid);

if (findViewById(R.id.handset_pager) != null) {

// Handset detected !
mAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.handset_pager);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mSurfaceView = (SurfaceView)findViewById(R.id.handset_camera_view);
SessionBuilder.getInstance().setSurfaceView(mSurfaceView);
SessionBuilder.getInstance().setPreviewOrientation(90);

} else {

// Tablet detected !
device = TABLET;
mAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.tablet_pager);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
SessionBuilder.getInstance().setPreviewOrientation(0);

}

mViewPager.setAdapter(mAdapter);

// Remove the ads if this is the donate version of the app.
if (mApplication.DONATE_VERSION) {
((LinearLayout)findViewById(R.id.adcontainer)).removeAllViews();
}

// Prevents the phone from going to sleep mode
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "net.majorkernelpanic.spydroid.wakelock");

// Starts the service of the HTTP server
this.startService(new Intent(this,CustomHttpServer.class));

// Starts the service of the RTSP server
this.startService(new Intent(this,CustomRtspServer.class));

}

public void onStart() {
super.onStart();

// Lock screen
mWakeLock.acquire();

// Did the user disabled the notification ?
if (mApplication.notificationEnabled) {
Intent notificationIntent = new Intent(this, SpydroidActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Notification notification = builder.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setTicker(getText(R.string.notification_title))
.setSmallIcon(R.drawable.icon)
.setContentTitle(getText(R.string.notification_title))
.setContentText(getText(R.string.notification_content)).build();
notification.flags |= Notification.FLAG_ONGOING_EVENT;
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).notify(0,notification);
} else {
removeNotification();
}

bindService(new Intent(this,CustomHttpServer.class), mHttpServiceConnection, Context.BIND_AUTO_CREATE);
bindService(new Intent(this,CustomRtspServer.class), mRtspServiceConnection, Context.BIND_AUTO_CREATE);

}

@Override
public void onStop() {
super.onStop();
// A WakeLock should only be released when isHeld() is true !
if (mWakeLock.isHeld()) mWakeLock.release();
if (mHttpServer != null) mHttpServer.removeCallbackListener(mHttpCallbackListener);
unbindService(mHttpServiceConnection);
if (mRtspServer != null) mRtspServer.removeCallbackListener(mRtspCallbackListener);
unbindService(mRtspServiceConnection);
}

@Override
public void onResume() {
super.onResume();
mApplication.applicationForeground = true;
}

@Override
public void onPause() {
super.onPause();
mApplication.applicationForeground = false;
}

@Override
public void onDestroy() {
Log.d(TAG,"SpydroidActivity destroyed");
super.onDestroy();
}

@Override
public void onBackPressed() {
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItemCompat.setShowAsAction(menu.findItem(R.id.quit), 1);
MenuItemCompat.setShowAsAction(menu.findItem(R.id.options), 1);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;

switch (item.getItemId()) {
case R.id.options:
// Starts QualityListActivity where user can change the streaming quality
intent = new Intent(this.getBaseContext(),OptionsActivity.class);
startActivityForResult(intent, 0);
return true;
case R.id.quit:
quitSpydroid();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

private void quitSpydroid() {
// Removes notification
if (mApplication.notificationEnabled) removeNotification();
// Kills HTTP server
this.stopService(new Intent(this,CustomHttpServer.class));
// Kills RTSP server
this.stopService(new Intent(this,CustomRtspServer.class));
// Returns to home menu
finish();
}

private ServiceConnection mRtspServiceConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mRtspServer = (CustomRtspServer) ((RtspServer.LocalBinder)service).getService();
mRtspServer.addCallbackListener(mRtspCallbackListener);
mRtspServer.start();
}

@Override
public void onServiceDisconnected(ComponentName name) {}

};

private RtspServer.CallbackListener mRtspCallbackListener = new RtspServer.CallbackListener() {

@Override
public void onError(RtspServer server, Exception e, int error) {
// We alert the user that the port is already used by another app.
if (error == RtspServer.ERROR_BIND_FAILED) {
new AlertDialog.Builder(SpydroidActivity.this)
.setTitle(R.string.port_used)
.setMessage(getString(R.string.bind_failed, "RTSP"))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
startActivityForResult(new Intent(SpydroidActivity.this, OptionsActivity.class),0);
}
})
.show();
}
}

@Override
public void onMessage(RtspServer server, int message) {
if (message==RtspServer.MESSAGE_STREAMING_STARTED) {
if (mAdapter != null && mAdapter.getHandsetFragment() != null)
mAdapter.getHandsetFragment().update();
} else if (message==RtspServer.MESSAGE_STREAMING_STOPPED) {
if (mAdapter != null && mAdapter.getHandsetFragment() != null)
mAdapter.getHandsetFragment().update();
}
}

};

private ServiceConnection mHttpServiceConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mHttpServer = (CustomHttpServer) ((TinyHttpServer.LocalBinder)service).getService();
mHttpServer.addCallbackListener(mHttpCallbackListener);
mHttpServer.start();
}

@Override
public void onServiceDisconnected(ComponentName name) {}

};

private TinyHttpServer.CallbackListener mHttpCallbackListener = new TinyHttpServer.CallbackListener() {

@Override
public void onError(TinyHttpServer server, Exception e, int error) {
// We alert the user that the port is already used by another app.
if (error == TinyHttpServer.ERROR_HTTP_BIND_FAILED ||
error == TinyHttpServer.ERROR_HTTPS_BIND_FAILED) {
String str = error==TinyHttpServer.ERROR_HTTP_BIND_FAILED?"HTTP":"HTTPS";
new AlertDialog.Builder(SpydroidActivity.this)
.setTitle(R.string.port_used)
.setMessage(getString(R.string.bind_failed, str))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
startActivityForResult(new Intent(SpydroidActivity.this, OptionsActivity.class),0);
}
})
.show();
}
}

@Override
public void onMessage(TinyHttpServer server, int message) {
if (message==CustomHttpServer.MESSAGE_STREAMING_STARTED) {
if (mAdapter != null && mAdapter.getHandsetFragment() != null)
mAdapter.getHandsetFragment().update();
if (mAdapter != null && mAdapter.getPreviewFragment() != null)
mAdapter.getPreviewFragment().update();
} else if (message==CustomHttpServer.MESSAGE_STREAMING_STOPPED) {
if (mAdapter != null && mAdapter.getHandsetFragment() != null)
mAdapter.getHandsetFragment().update();
if (mAdapter != null && mAdapter.getPreviewFragment() != null)
mAdapter.getPreviewFragment().update();
}
}

};

private void removeNotification() {
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).cancel(0);
}

public void log(String s) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}

class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int i) {
if (device == HANDSET) {
switch (i) {
case 0: return new HandsetFragment();
case 1: return new PreviewFragment();
case 2: return new AboutFragment();
}
} else {
switch (i) {
case 0: return new TabletFragment();
case 1: return new AboutFragment();
}
}
return null;
}

@Override
public int getCount() {
return device==HANDSET ? 3 : 2;
}

public HandsetFragment getHandsetFragment() {
if (device == HANDSET) {
return (HandsetFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.handset_pager+":0");
} else {
return (HandsetFragment) getSupportFragmentManager().findFragmentById(R.id.handset);
}
}

public PreviewFragment getPreviewFragment() {
if (device == HANDSET) {
return (PreviewFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.handset_pager+":1");
} else {
return (PreviewFragment) getSupportFragmentManager().findFragmentById(R.id.preview);
}
}

@Override
public CharSequence getPageTitle(int position) {
if (device == HANDSET) {
switch (position) {
case 0: return getString(R.string.page0);
case 1: return getString(R.string.page1);
case 2: return getString(R.string.page2);
}
} else {
switch (position) {
case 0: return getString(R.string.page0);
case 1: return getString(R.string.page2);
}
}
return null;
}

}

總結

以上是生活随笔為你收集整理的[转]将Android变成一个网络摄像机:spydroid-ipcamera;能实时传输,几乎没有延迟...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。