- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试创建一个应用程序,使用户能够记录他的智能手机的屏幕。这是我的起始代码:
import android.content.Context;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.MediaRecorder;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity
{
private static final int CAST_PERMISSION_CODE = 22;
private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
private MediaProjection mMediaProjection;
private VirtualDisplay mVirtualDisplay;
private MediaRecorder mMediaRecorder;
private MediaProjectionManager mProjectionManager;
private Button startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) findViewById( R.id.recordButton );
mMediaRecorder = new MediaRecorder();
mProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
getWindowManager().getDefaultDisplay().getMetrics(this.mDisplayMetrics);
prepareRecording();
startRecording();
}
private void startRecording() {
// If mMediaProjection is null that means we didn't get a context, lets ask the user
if (mMediaProjection == null) {
// This asks for user permissions to capture the screen
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), CAST_PERMISSION_CODE);
return;
}
mVirtualDisplay = getVirtualDisplay();
mMediaRecorder.start();
}
private void stopRecording() {
if (mMediaRecorder != null) {
mMediaRecorder.stop();
mMediaRecorder.reset();
}
if (mVirtualDisplay != null) {
mVirtualDisplay.release();
}
if (mMediaProjection != null) {
mMediaProjection.stop();
}
prepareRecording();
}
public String getCurSysDate() {
return new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());
}
private void prepareRecording() {
try {
mMediaRecorder.prepare();
} catch (Exception e) {
e.printStackTrace();
return;
}
final String directory = Environment.getExternalStorageDirectory() + File.separator + "Recordings";
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
Toast.makeText(this, "Failed to get External Storage", Toast.LENGTH_SHORT).show();
return;
}
final File folder = new File(directory);
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
String filePath;
if (success) {
String videoName = ("capture_" + getCurSysDate() + ".mp4");
filePath = directory + File.separator + videoName;
} else {
Toast.makeText(this, "Failed to create Recordings directory", Toast.LENGTH_SHORT).show();
return;
}
int width = mDisplayMetrics.widthPixels;
int height = mDisplayMetrics.heightPixels;
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(width, height);
mMediaRecorder.setOutputFile(filePath);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != CAST_PERMISSION_CODE) {
// Where did we get this request from ? -_-
//Log.w(TAG, "Unknown request code: " + requestCode);
return;
}
if (resultCode != RESULT_OK) {
Toast.makeText(this, "Screen Cast Permission Denied :(", Toast.LENGTH_SHORT).show();
return;
}
mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
// TODO Register a callback that will listen onStop and release & prepare the recorder for next recording
// mMediaProjection.registerCallback(callback, null);
mVirtualDisplay = getVirtualDisplay();
mMediaRecorder.start();
}
private VirtualDisplay getVirtualDisplay()
{
int screenDensity = mDisplayMetrics.densityDpi;
int width = mDisplayMetrics.widthPixels;
int height = mDisplayMetrics.heightPixels;
return mMediaProjection.createVirtualDisplay(this.getClass().getSimpleName(), width, height, screenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mMediaRecorder.getSurface(), null /*Callbacks*/, null /*Handler*/);
}
}
在显示一条通知用户有关屏幕捕获功能的消息后,我的应用崩溃了。
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=22, result=-1, data=Intent { (has extras) }} to activity {gr.awm.clrecorder/gr.awm.clrecorder.MainActivity}: java.lang.IllegalStateException: failed to get surface
at android.app.ActivityThread.deliverResults(ActivityThread.java:3974)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4017)
at android.app.ActivityThread.access$1400(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1471)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.IllegalStateException: failed to get surface
at android.media.MediaRecorder.getSurface(Native Method)
at gr.awm.clrecorder.MainActivity.getVirtualDisplay(MainActivity.java:148)
at gr.awm.clrecorder.MainActivity.onActivityResult(MainActivity.java:135)
有没有办法解决这个问题?任何建议都会有所帮助并深表感谢。提前致谢
最佳答案
顺便说一句,别介意评论。
我深入研究了文档和您的代码并获得了以下结果。
这是您调用 mMediaRecorder 方法获取表面的顺序。
mMediaRecorder.prepare();
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(width, height);
mMediaRecorder.setOutputFile(filePath);
这就是documentation说
//Call this method before prepare().
setVideoEncodingBitRate(); //no exception thrown
//Must be called after setVideoSource(). Call this after setOutFormat() but before prepare().
setVideoSize(width, height); //IllegalStateException if it is called after prepare() or before setOutputFormat()
//Call this only before setOutputFormat().
setAudioSource(); //IllegalStateException if it is called after setOutputFormat()
setVideoSource(); //IllegalStateException if it is called after setOutputFormat()
//Call this after setOutputFormat() and before prepare().
setVideoEncoder(); //IllegalStateException if it is called before setOutputFormat() or after prepare()
setAudioEncoder(); //IllegalStateException if it is called before setOutputFormat() or after prepare().
//Call this after setAudioSource()/setVideoSource() but before prepare().
setOutputFormat(); //IllegalStateException if it is called after prepare() or before setAudioSource()/setVideoSource().
//Call this after setOutputFormat() but before prepare().
setOutputFile(); //IllegalStateException if it is called before setOutputFormat() or after prepare()
//Must be called after setVideoSource(). Call this after setOutFormat() but before prepare().
setVideoFrameRate(); //IllegalStateException if it is called after prepare() or before setOutputFormat().
//This method must be called after setting up the desired audio and video sources, encoders, file format, etc., but before start()
prepare() //IllegalStateException if it is called after start() or before setOutputFormat().
因此,为了使 mMediaRecorder 处于正确状态,您必须按以下顺序调用方法:
setAudioSource()
setVideoSource()
setOutputFormat()
setAudioEncoder()
setVideoEncoder()
setVideoSize()
setVideoFrameRate()
setOutputFile()
setVideoEncodingBitRate()
prepare()
start()
我想当我在 setSource 方法之前调用 setEncoder 方法时我也遇到了一个未记录的错误
编辑:我以为我得到了工作代码,但我仍然得到 IllegalStateExceptions,尽管代码是按文档的顺序排列的。
Edit2:我现在开始工作了。可能也不起作用的事情和其他错误消息:
我必须创建一个应用程序可以写入的目录。我无法让外部存储工作,所以我用了数据目录。但这与 mMediaRecorder
代码无关
此代码有效:
private void prepareRecording() {
//Deal with FileDescriptor and Directory here
//Took audio out because emulator has no mic
//mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
//mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setVideoSize(width, height);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setOutputFile(filePath);
try {
mMediaRecorder.prepare();
} catch (Exception e) {
e.printStackTrace();
return;
}
//Field variable to hold surface object
//Deal with it as you see fit
surface = mMediaRecorder.getSurface();
注意 虽然上面的代码可以正确创建 MediaRecorder
并写入存储,但是当 mMediaRecorder.stop()
被调用。
关于java.lang.IllegalStateException : failed to get surface 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35711723/
Surface Go是一款微软推出笔记本电脑,但不少小伙伴在使用一段时间之后总会想重装系统从而使电脑更加流畅,那么Surface Go要如何重装Win10系统,下面就和小编一起来看看如何操作吧。
在我的第二个项目中,我使用 opengl 和 SDL2。我想使用 SDL2 将文本渲染到表面,然后进行转换。 我已经在使用 SDL 从文件中加载纹理,所以我有一个可以转换曲面的函数。当我提供由 TTF
我最近更改了表面对象算法的表面引用。然后,我注意到程序运行速度变慢了。 这是一个简单示例的比较,其中我用常量值填充 3D float 数组 [400*400*400]。 表面引用API 时间:9.06
有时,当我退出我的 openGL ES 1 应用程序时,以下错误消息会不断张贴在 logcat 中,应用程序会卡住,并在某个时候终止。 E/EglHelper(4284): Surface is no
这可能是一个愚蠢的问题,但我无法在任何地方找到答案。 Microsoft Surface SDK 2.0 是否仅适用于 Microsoft Surface 产品,还是我可以将其用于其他触摸屏?我真的很
我已经将 camera2 与 textureVIew 集成在一起。它适用于所有设备,但在平板电脑上,当我们第一次捕获图像时,它会崩溃并显示以下日志。 Fatal Exception: java.la
在我正在处理的一个 pygame 项目中,角色和对象的 Sprite 会在地形上转换阴影。阴影和地形都是正常的 pygame 表面,因此,为了显示它们,阴影被 blit 到地形上。当没有其他阴影(只有
在 pygame 中将我的表面涂成黑色之后。我得到错误 pygame.error: display Surface quit. 完整错误: > D:\Programme\Anaconda3\envs\
有谁知道如何用Julia的Pyplot绘制3D曲面,例如 作为面部颜色的表面渐变? 类似于此主题适用于Python:Color matplotlib plot_surface command with
我一直在尝试将 Camera2 api 集成到我的应用程序中。它一开始捕捉图像时工作正常。但是当我第二次拍摄时,预览没有出现。我在 genymotion nexus 5 模拟器中测试了它。尝试了所有示
阅读 pygame 教程 here ,你会发现这个例子:(箭头是我的) for o in objects: screen.blit(background, o.pos, o.pos) # Re
我正在创建两个渲染目标,两者都必须共享后台缓冲区的深度缓冲区,因此我将它们设置为具有相同的多重采样参数很重要,但是 pDevice->CreateTexture(..) 没有给出任何参数设置多重采样类
我正在尝试在屏幕上对文本表面进行 blit,但出现此错误: 类型错误:参数 1 必须是 pygame.Surface,而不是元组。 可能有什么问题?我确信我的文字是一个表面。 import pygam
Joel Spolsky 在 SO 播客中提到了“浮出水面”。 这是什么意思?它是类似于“暴露”的东西吗,比如“暴露接口(interface)”? 最佳答案 这里引用自 podcast 51 的文字记
下面给了我错误 var textSurface = new Surface({ content: 'Some text here', properties:{ color: 'whit
我是一个小机器人,但有一项艰巨的任务要做。我的布局上有一个图像按钮。单击它应该开始显示 camera preview 。 (我有一个视频同时显示在另一个表面上。)两者都很好。 但是当再次按下按钮时,预
我试图每 5 秒画一个红色圆圈。我有一个 draw 和一个 run 方法,run 方法调用绘制圆圈的 draw 方法。但是,当从 run 方法调用 draw 方法时,表面无效,所以我无法绘制任何东西并
我想知道 Surfaceflinger 是否知道当前屏幕上的可点击区域,或者 SurfaceFlinger 只显示最终屏幕,它对可点击区域一无所知,并且 windowmanagerservice 将输
所以这段代码似乎是问题所在: public void surfaceCreated(final SurfaceHolder sh) { loop = new Loop(); loop.
Opengl 纹理坐标 [0,0] 位于左下角。 DDS 文件是否包含有关数据布局的信息(如果数据从左上角或左下角开始)或有一个固定的布局?例如 ktx 文件包含此类信息。 最佳答案 DDS 文件格式
我是一名优秀的程序员,十分优秀!