- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是安卓新手。我创建了一个屏幕录像机应用程序。所有事情都已完成,但问题是当我停止屏幕录像机时,它不会停止,并且会发生此错误。当在谷歌上搜索此错误时,我发现 MediaRecorder 未启动,但当我检查我的 SD 卡时,然后捕获了视频已保存。我遵循本教程 here .
当我开始录制视频时,点击后弹出一个通知然后通知停止消息然后使用服务我尝试停止 mediaRecorder 停止但它没有停止并且发生错误。如果有人可以帮助我那么谢谢提前。
问题MediaRecorder.stop() 不工作。
MainActivity.java
public class MainActivity extends Activity {
private static final int REQUEST_CODE = 1000;
private static final String TAG = "MainActivity";
private ScreenRecorder mScreenRecorder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mScreenRecorder = RecorderApplication.getApplication(this).getRecorder();
shareScreen();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != REQUEST_CODE) {
Log.e(TAG, "Unknown request code: " + requestCode);
return;
}
if (resultCode != RESULT_OK) {
Toast.makeText(this, "Screen Cast Permission Denied", Toast.LENGTH_SHORT).show();
return;
}
mScreenRecorder.initRecorder();
mScreenRecorder.initShareScreen(resultCode, data);
finish();
}
private void shareScreen() {
if (mScreenRecorder.getMediaProjection() == null) {
Log.i("getMediaProjection()..."," is null");
startActivityForResult(mScreenRecorder.getMediaProjectionManager().createScreenCaptureIntent(), REQUEST_CODE);
return;
}
mScreenRecorder.initRecorder();
mScreenRecorder.shareScreen();
finish();
}
}
ScreenRecorder.java
public class ScreenRecorder {
private Context mContext;
private WindowManager mWindowManager;
private int mScreenDensity;
private MediaProjectionManager mProjectionManager;
private MediaProjection mMediaProjection;
private VirtualDisplay mVirtualDisplay;
private Surface surface;
private MediaProjectionCallback mMediaProjectionCallback;
public MediaRecorder mMediaRecorder;
private String mTargetRecordFileName;
private String mTargetRecordFilePath;
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
public static final String NOTIFICATION_EXTRA = "Extra";
public static final int EXTRA_PLAY = 0;
public static final int EXTRA_PAUSE = 1;
public static final int EXTRA_STOP = 2;
public int flag = 0;
static {
ORIENTATIONS.append(Surface.ROTATION_0, 90);
ORIENTATIONS.append(Surface.ROTATION_90, 0);
ORIENTATIONS.append(Surface.ROTATION_180, 270);
ORIENTATIONS.append(Surface.ROTATION_270, 180);
}
public ScreenRecorder(Context context, WindowManager windowManager) {
mContext = context;
mWindowManager = windowManager;
DisplayMetrics metrics = new DisplayMetrics();
mWindowManager.getDefaultDisplay().getMetrics(metrics);
mScreenDensity = metrics.densityDpi;
mMediaRecorder = new MediaRecorder();
mProjectionManager = (MediaProjectionManager) mContext.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
}
public MediaProjection getMediaProjection() {
return mMediaProjection;
}
public MediaProjectionManager getMediaProjectionManager() {
return mProjectionManager;
}
public void initShareScreen(int resultCode, Intent data) {
mMediaProjectionCallback = new MediaProjectionCallback();
mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
mMediaProjection.registerCallback(mMediaProjectionCallback, null);
shareScreen();
}
public void shareScreen() {
mVirtualDisplay = createVirtualDisplay();
mMediaRecorder.start();
flag = 1;
Log.i("shareScreen()...","mMediaRecorder.start()");
Log.i("shareScreen()...",".."+flag);
// showRunningNotification();
showControlNotification();
}
private VirtualDisplay createVirtualDisplay() {
int screen_width = mContext.getResources().getDisplayMetrics().widthPixels;
int screen_height = mContext.getResources().getDisplayMetrics().heightPixels;
surface = mMediaRecorder.getSurface();
return mMediaProjection.createVirtualDisplay("MainActivity",
screen_width, screen_height, mScreenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mMediaRecorder.getSurface(), null, null);
}
public void initRecorder() {
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
int screen_width = mContext.getResources().getDisplayMetrics().widthPixels;
int screen_height = mContext.getResources().getDisplayMetrics().heightPixels;
Log.i("initRecorder()...", " Initialization is Completed");
try {
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setVideoSize(screen_width, screen_height);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setOutputFile(getSaveVideoFolder());
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.prepare();
System.out.println("..........Media Recorder is Ready for Recording......");
} catch (IOException e) {
e.printStackTrace();
}
}
private String getSaveVideoFolder() {
File folder = new File(Environment.getExternalStorageDirectory()
+ "/ScreenRecorder");
if (!folder.exists()) {
folder.mkdirs();
}
mTargetRecordFileName = Utils.convertDateToString(new Date()) + ".mp4";
mTargetRecordFilePath = folder.getAbsolutePath() + "/"
+ mTargetRecordFileName;
return mTargetRecordFilePath;
}
private class MediaProjectionCallback extends MediaProjection.Callback {
@Override
public void onStop() {
mMediaRecorder.stop();
mMediaRecorder.reset();
Log.v("ScreenRecorder...", "Recording Stopped");
mMediaProjection = null;
stopScreenSharing();
}
}
private void stopScreenSharing() {
if (mVirtualDisplay == null) {
return;
}
mVirtualDisplay.release();
// mMediaRecorder.release(); //If used: mMediaRecorder object cannot
// be reused again
destroyMediaProjection();
}
private void destroyMediaProjection() {
Log.i("destroyMediaProjection()...","called");
if (mMediaProjection != null) {
mMediaProjection.unregisterCallback(mMediaProjectionCallback);
mMediaProjection.stop();
// mMediaProjection = null;
Log.i("destroyMediaProjection()...","not null");
}
}
private static int NOTIFICATION_RUNNING = 109;
private static int NOTIFICATION_CONTROL = 110;
private static int NOTIFICATION_DONE = 111;
private void showControlNotification() {
Intent intentPause = new Intent(mContext, FloatingViewService.class);
intentPause.setAction("PAUSE");
PendingIntent pIntentPause = PendingIntent.getService(mContext, 0,
intentPause, 0);
Intent intentStop = new Intent(mContext, FloatingViewService.class);
intentStop.setAction("STOP");
PendingIntent pIntentStop = PendingIntent.getService(mContext, 0, intentStop, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
mBuilder.setSmallIcon(R.drawable.ic_notification);
mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
mBuilder.setContentTitle("Recording controls");
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setOngoing(true);
mBuilder.setWhen(0);
// if (!isPauseRecorder) {
// mBuilder.addAction(R.drawable.ic_action_pause, "PAUSE",
// pIntentPause);
// } else {
// mBuilder.addAction(R.drawable.ic_action_video, "RESUME",
// pIntentPause);
// }
mBuilder.addAction(R.drawable.ic_action_stop, "STOP", pIntentStop);
Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
NotificationManager mNotificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_CONTROL, notification);
}
public void stopRecorder() {
try {
if (mMediaRecorder != null){
Log.i("flag...","..."+flag);
mMediaRecorder.stop();
mMediaRecorder.reset();
// mMediaProjection.stop();
stopScreenSharing();
}else{
Log.i("mMediaRecorder...in stopRecorder()"," is null");
}
}catch (Exception e){
Log.e("Error to stop media recorder in stopRecorder() method... :- "," "+e.getMessage());
e.printStackTrace();
}
NotificationManager mNotificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(NOTIFICATION_CONTROL);
mNotificationManager.cancel(NOTIFICATION_RUNNING);
// showDoneNotification();
// TODO save info to database
DatabaseHelper mDatabaseHelper = new DatabaseHelper(mContext);
mDatabaseHelper.addVideo(mTargetRecordFilePath);
// appendToFile(mTargetRecordFileName, getTemporaryFileName());
}
private String getTemporaryFileName() {
return mContext.getExternalCacheDir().getAbsolutePath()
+ File.separator + "tmprecord.mp4";
}
private void appendToFile(@NonNull final String targetFileName,
@NonNull final String newFileName) {
Mp4ParserWrapper.append(targetFileName, newFileName);
}
}
FloatingViewService.java
public class FloatingViewService extends Service {
private WindowManager windowManager;
private View floatingView;
private TextView mTextCoolDown;
private WindowManager.LayoutParams params;
private WindowManager.LayoutParams paramsCoolDown;
private ImageView btnCamera;
private ImageView btnSettings;
private ImageView btnAlbum;
private ImageView btnExit;
public static final String NOTIFICATION = "com.example.screenrecorder";
private ScreenRecorder mScreenRecorder ;
private ScreenRecorder mScreenRecorder1 ;
private int cooldown = 3;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
windowManager = (WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
mScreenRecorder = new ScreenRecorder(getApplicationContext(), windowManager);
if (intent != null) {
if (intent.getAction() != null) {
Log.i("intent Action...",intent.getAction());
if (intent.getAction().equals("PAUSE")) {
} else if (intent.getAction().equals("STOP")) {
mScreenRecorder.stopRecorder();
try{
showFloatingView();
}catch (RuntimeException e){
}
}
}
}
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
initView();
}
@Override
public void onDestroy() {
super.onDestroy();
if (floatingView != null)
windowManager.removeView(floatingView);
}
private void initView() {
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
int screen_height = getResources().getDisplayMetrics().heightPixels;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
floatingView = inflater.inflate(R.layout.floating_view, null);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
btnCamera = (ImageView) floatingView.findViewById(R.id.btn_camera);
btnSettings = (ImageView) floatingView.findViewById(R.id.btn_settings);
btnAlbum = (ImageView) floatingView.findViewById(R.id.btn_album);
btnExit = (ImageView) floatingView.findViewById(R.id.btn_close);
btnCamera.setOnTouchListener(new OnItemTouchListener(0));
btnSettings.setOnTouchListener(new OnItemTouchListener(1));
btnAlbum.setOnTouchListener(new OnItemTouchListener(2));
btnExit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
stopSelf();
}
});
setFloatingViewMove();
}
private void showFloatingView() {
windowManager.addView(floatingView, params);
}
private void hideFloatingView() {
windowManager.removeView(floatingView);
}
private int mAction = -1;
long onTouchTime = -1;
long TIME_CLICK = 200;
private class OnItemTouchListener implements OnTouchListener {
private int action;
public OnItemTouchListener(int action) {
this.action = action;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mAction = action;
v.setBackgroundResource(R.drawable.bg_item_selected);
onTouchTime = System.currentTimeMillis();
break;
}
return false;
}
}
private void setFloatingViewMove() {
floatingView.setOnTouchListener(new OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
Log.d("phucdl", "start recorder " + (System.currentTimeMillis() - onTouchTime));
if (System.currentTimeMillis() - onTouchTime < TIME_CLICK) {
Intent intent;
switch (mAction) {
case 0:
if (RecorderApplication.getApplication(FloatingViewService.this).getRecorder() == null) {
mScreenRecorder = new ScreenRecorder(FloatingViewService.this, windowManager);
RecorderApplication.getApplication(FloatingViewService.this).setRecorder(mScreenRecorder);
}
intent = new Intent(FloatingViewService.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
hideFloatingView();
break;
case 1:
intent = new Intent(FloatingViewService.this, PreferencesActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
hideFloatingView();
break;
case 2:
intent = new Intent(FloatingViewService.this, AlbumActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
hideFloatingView();
break;
default:
break;
}
}
btnCamera.setBackgroundDrawable(null);
btnSettings.setBackgroundDrawable(null);
btnAlbum.setBackgroundDrawable(null);
btnExit.setBackgroundDrawable(null);
break;
case MotionEvent.ACTION_MOVE:
params.x = initialX
+ (int) (event.getRawX() - initialTouchX);
params.y = initialY
+ (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(floatingView, params);
break;
}
return false;
}
});
}
}
日志是
08-17 20:43:07.537 13733-13733/com.example.screenrecorder E/MediaRecorder: stop called in an invalid state: 1
08-17 20:43:07.537 13733-13733/com.example.screenrecorder E/Error to stop media recorder in stopRecorder() method... :-: null
08-17 20:43:07.537 13733-13733/com.example.screenrecorder W/System.err: java.lang.IllegalStateException
08-17 20:43:07.537 13733-13733/com.example.screenrecorder W/System.err: at android.media.MediaRecorder.stop(Native Method)
08-17 20:43:07.537 13733-13733/com.example.screenrecorder W/System.err: at com.gameapp.screenrecorder.ScreenRecorder.stopRecorder(ScreenRecorder.java:371)
08-17 20:43:07.537 13733-13733/com.example.screenrecorder W/System.err: at com.gameapp.screenrecorder.FloatingViewService.onStartCommand(FloatingViewService.java:59)
08-17 20:43:07.537 13733-13733/com.example.screenrecorder W/System.err: at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3010)
08-17 20:43:07.537 13733-13733/com.example.screenrecorder W/System.err: at android.app.ActivityThread.-wrap17(ActivityThread.java)
08-17 20:43:07.537 13733-13733/com.example.screenrecorder W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)
08-17 20:43:07.538 13733-13733/com.example.screenrecorder W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
08-17 20:43:07.538 13733-13733/com.example.screenrecorder W/System.err: at android.os.Looper.loop(Looper.java:148)
08-17 20:43:07.538 13733-13733/com.example.screenrecorder W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
08-17 20:43:07.538 13733-13733/com.example.screenrecorder W/System.err: at java.lang.reflect.Method.invoke(Native Method)
08-17 20:43:07.538 13733-13733/com.example.screenrecorder W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
08-17 20:43:07.538 13733-13733/com.example.screenrecorder W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-17 20:43:07.732 13733-13750/com.example.screenrecorder W/EGL_emulation: eglSurfaceAttrib not implemented
08-17 20:43:07.732 13733-13750/com.example.screenrecorder W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xed6bf5a0, error=EGL_SUCCESS
08-17 20:43:09.433 13733-13750/com.example.screenrecorder E/Surface: getSlotFromBufferLocked: unknown buffer: 0xee9722a0
最佳答案
您第一次在 FloatingViewService 类中的 setFloatingViewMove() 方法中创建了一个 ScreenRecorder 类的对象,这没问题,但是当您单击 Stop 时您再次创建 ScreenRecorder 类对象,然后调用它的构造函数,并且您之前的 MediaRacoder 对象是新初始化的,然后在您调用 stop 方法之后,但未调用 mediarecoder start 方法新初始化的 mediarecorder 实例,这就是发生此异常的原因.
只需在 FloatingViewService 类的 onStartCommand() 方法中命令或删除一行。我希望这能解决您的问题。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
windowManager = (WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
// mScreenRecorder = new ScreenRecorder(getApplicationContext(), windowManager);
}
关于android - 媒体记录器 : stop called in an invalid state: 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39001111/
有没有办法配置 python 记录器来调用自定义函数并在它记录时将记录消息传递给它? 谢谢! 最佳答案 子类 logging.Handler 并实现 emit方法: import logging cl
我有一个这样的记录器设置: import logging from logging.handlers import RotatingFileHandler import sys # root logg
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我一直在使用 Netbeans 作为 Java IDE 进行开发。当 Netbeans 在 try-catch block 中包含一条语句时,它在捕获区域中使用类似 Logger.getLogger(
我正在记下远程日志记录的功能从头开始构建时可能需要库。我查了这个:http://www.aggsoft.com/serial-data-logger.htm 我想知道a之间有什么区别远程日志记录库和远
我需要跟踪包含数千个 JAR 和 .CLASS 文件的已编译 Java 应用程序,您知道有什么合适的工具可以附加到 JVM 来跟踪函数调用(无需源代码)吗? 最佳答案 是的。 Jprofiler无需源
我想使用记录器并设置其属性,但不想在每个对象类中创建记录器,我的目标是:在Spring中创建一个bean,创建属性文件我如何使用注释,可以做到吗? 最佳答案 是的,你可以做到, 在你的
首先,我已经阅读了real python article on the subject . 了解到记录器具有层次结构后,我想在这样的层次结构中创建一个名为 MyProjectLogger 的新记录器:
我设置了一个记录器。像这样: def initLogger(self): self.logger = logging.getLogger('MyApp') if not self.lo
是否允许为您的日志创建一个静态类? public final class Log { public static final Logger LOGGER = Logger.getLogger(
我对 java.util.logging 有一些无法解释的行为。让我们看一下这两个示例: 首先: boolean var = false; log.log( Level.WARNING, "Cant
我正在尝试开始在 python 中使用日志记录并阅读了几篇博客。一个让我感到困惑的问题是是按功能还是按模块创建记录器。在这个Blog: Good logging practice in Python建
我正在使用 https://pub.dartlang.org/packages/logging ,但它不会在我的日志中显示任何内容。这是我的代码: class Test { final Logge
public static Logger getLogger() { final Throwable t = new Throwable(); final StackTraceElem
我是 Poco 的新手,我在 Poco 在线帮助中看到了以下示例: int main(int argc, char** argv) { AutoPtr pChannel(new SimpleF
我有一个带有函数、文档字符串和文档测试的 Julia 模块文件。我加载它并在 Julia 帮助中显示文档字符串,但 Documenter.jl 找不到文档字符串。 一个示例模块文件,src/my_mo
我正在尝试在我的 Django 项目( django 1.11 、 Python 3.6 )中实现日志记录。我正在使用默认的 django 记录器。 获取 username在日志中,我使用了 djan
是否可以有多个 serilog 记录器?目前,在我的 WebApi 中,我可以调用 Log.Information 来记录信息事件,但是有没有一种方法可以改为制作不同的日志并从我的 Controlle
我有一个 python 应用程序,其文件结构类似于以下内容: /Project file1.py file2.py file3.py ... 该应用程序使用 Pytho
如何访问 python Bokeh 记录器? 我尝试使用 basicConfig包装器,但它似乎不起作用。 from bokeh.util import logconfig logconfig.bas
我是一名优秀的程序员,十分优秀!