我有一个表面视图来显示来自 IP 摄像机的 MJPEG 流。除了在一种情况下,它工作得很好。
在显示表面视图的活动中,我有以下标志:
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
如果我正常启动活动(从应用程序),它会显示相机图片。但是,如果我在手机处于睡眠状态时从后台服务启动活动,则活动启动,手机解锁,屏幕打开,一切看起来都很好,除了表面视图上没有绘制任何内容。MJPEG 流开始正确接收,它似乎在画布上绘制它,但没有显示任何内容。如果我旋转手机,则会显示图片,从那时起一切正常。
我试着把相关的代码片段放在这里,但是如果你需要别的东西,我也会放在这里。MjpegView.class 它是在网上找到的类(在 Stackoverflow 上也看到了这里的代码),我对它做了一些小改动(主要是在 MjpegInputStream 中)。
单独线程中的绘图代码:
while (mRun && mIn != null)
{
if (surfaceDone)
{
try
{
try
{
bm = mIn.readMjpegFrame();
}
catch (IOException e1)
{
bm = null;
}
if (bm == null) continue;
c = mSurfaceHolder.lockCanvas();
synchronized (mSurfaceHolder)
{
destRect = destRect(bm.getWidth(), bm.getHeight());
c.drawColor(Color.BLACK);
c.drawBitmap(bm, null, destRect, p);
if (requestscreenshot)
{
requestscreenshot = false;
try
{
FileOutputStream out = new FileOutputStream(requestfile);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
}
catch (Exception ex)
{
// ignore
}
requestfile = "";
}
if (showFps)
{
p.setXfermode(mode);
if (ovl != null)
{
// false indentation to fix forum layout
height = ((ovlPos & 1) == 1) ? destRect.top : destRect.bottom - ovl.getHeight();
width = ((ovlPos & 8) == 8) ? destRect.left : destRect.right - ovl.getWidth();
c.drawBitmap(ovl, width, height, null);
}
p.setXfermode(null);
frameCounter++;
if ((System.currentTimeMillis() - start) >= 1000)
{
fps = String.valueOf(frameCounter) + "fps";
frameCounter = 0;
start = System.currentTimeMillis();
ovl = makeFpsOverlay(overlayPaint);
}
}
}
}
finally
{
if (c != null) mSurfaceHolder.unlockCanvasAndPost(c);
}
}
else
{
try
{
Thread.sleep(100);
}
catch (Exception ex)
{
}
}
}
然后在surfaceview中回调:
public void surfaceChanged(SurfaceHolder holder, int f, int w, int h)
{
Log.d(Constants.TAG, "MjpegView.surfaceChanged");
if (thread != null)
{
thread.mSurfaceHolder = holder;
thread.setSurfaceSize(w, h);
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
Log.d(Constants.TAG, "MjpegView.surfaceDestroyed");
surfaceDone = false;
stopPlayback();
}
public void surfaceCreated(SurfaceHolder holder)
{
Log.d(Constants.TAG, "MjpegView surfaceCreated");
surfaceDone = true;
}
public void startPlayback()
{
if (thread == null)
{
thread = new MjpegViewThread(getHolder(), context);
if (URL != null)
{
mRun = true;
thread.start();
}
}
else
{
thread.mSurfaceHolder = getHolder();
}
}
public void stopPlayback()
{
mRun = false;
boolean retry = true;
while (retry && thread != null)
{
try
{
thread.join();
retry = false;
}
catch (InterruptedException e)
{
}
}
thread = null;
}
在活动中,我只在 onCreate 中使用了一些初始化:
mv.setSource(device.getCameraURL());
mv.setMessageHandler(messageHandler);
mv.setDisplayMode(MjpegView.SIZE_BEST_FIT);
mv.showFps(false);
(其中 mv 是 MjpegView)
在 onResume 我调用 MjpegView 的 startPlayback 方法
我想就是这样。
感谢任何帮助!谢谢!
更新:
可能会找到解决方法:
PowerManager pm = (PowerManager) MjpegtestActivity.this.getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE | PowerManager.ACQUIRE_CAUSES_WAKEUP, Constants.TAG);
wl.acquire();
Intent intent = new Intent(MjpegtestActivity.this, Camera.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("PLAYSOUND", false);
intent.putExtra("DEVICEID", "a73d3ffd-bebb-aae4-f133-39a81eba6e");
MjpegtestActivity.this.startActivity(intent);
wl.release();
wl = null;
如果我在获得唤醒锁后开始活动,则 IP 摄像机图片会正确显示在表面视图上。
但我不知道为什么:)