java - FileOutputStream max_allowed_dequeued_buffers 3

标签 java android buffer fileoutputstream

当我尝试使用 FileOutputStream 将数据写入文件时,出现此错误并且我不知道如何处理它。这是错误来自的 class。添加程序的其余部分以显示调用 process 的内容太长,不适合放在这里。

public class WriterProcessor implements AudioProcessor {
    File output;
    TarsosDSPAudioFormat audioFormat;
    FileOutputStream fos;

    /**
     *
     * @param audioFormat which this processor is attached to
     * @param output randomaccessfile of the output file
     */
    public WriterProcessor(TarsosDSPAudioFormat audioFormat,File output){
        this.output=output;
        this.audioFormat=audioFormat;
        deleteFile();
        openFileStream();
    }
    @Override
    public boolean process(AudioEvent audioEvent) {
        writeIntoOutputfile(audioEvent.getByteBuffer());
        return true;
    }

    @Override
    public void processingFinished() {
        try {
            fos.close();
        } catch (IOException ex) {
        }

        try {
            System.out.println("Buffer size: " + audioFormat.getFrameSize());
            byte[] bytes = new byte[audioFormat.getFrameSize()];


            RandomAccessFile raf = new RandomAccessFile(output.getPath(), "wr");
            raf.read(bytes, 0 ,audioFormat.getFrameSize());


        } catch (Exception ex){

        }
    }


    /**
     * Writes data into file
     * @param data
     */
    private void writeIntoOutputfile(byte[] data) {
        try {
            fos.write(data);
        } catch (IOException ioe) {
            Log.w("Audio processor", "failed writing debug data to file");
            throw new RuntimeException(ioe);
        }
    }

    private void openFileStream() {
        fos = null;
        try {
            fos = new FileOutputStream(output, false);
        } catch (FileNotFoundException e) {
            Log.e("AudioRecorder", e.getMessage());
        }
    }

    private void deleteFile(){
        if (output.exists()) {
            output.delete();
        }
    }

}

方法 process() 调用了 writeIntoOutputFile(),通常此错误来自 IOException

09-28 13:54:24.564 19533-19731/ E/[EGL-ERROR]: void __egl_platform_dequeue_buffer(egl_surface*):1851: failed to dequeue buffer from native window 0x98965808; err = -19, buf = 0x0,max_allowed_dequeued_buffers 3
09-28 13:54:24.569 19533-19731/com.starmenew.com E/CameraDeviceGLThread-1: Received exception on GL render thread: 
    java.lang.IllegalStateException: makeCurrent: EGL error: 0x300d
        at android.hardware.camera2.legacy.SurfaceTextureRenderer.checkEglError(SurfaceTextureRenderer.java:544)
        at android.hardware.camera2.legacy.SurfaceTextureRenderer.makeCurrent(SurfaceTextureRenderer.java:525)
        at android.hardware.camera2.legacy.SurfaceTextureRenderer.drawIntoSurfaces(SurfaceTextureRenderer.java:745)
        at android.hardware.camera2.legacy.GLThreadManager$1.handleMessage(GLThreadManager.java:105)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:203)
        at android.os.HandlerThread.run(HandlerThread.java:61)

也许有一种方法可以释放dequed buffer,或者我真的不明白这个错误。

最佳答案

您在尝试执行后台操作时遇到渲染器错误。这很可能是因为您的应用程序在执行此操作时失去焦点(屏幕锁定或显示关闭)。

  • 如果您需要您的应用在执行后台操作时保持焦点和显示,请尝试添加唤醒锁定权限。来自 https://developer.android.com/training/scheduling/wakelock :

    public class MainActivity extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
      }
    }
    
  • 如果您正在尝试执行异步后台操作,例如音频处理和数据文件写入,请尝试 AsyncTask :

    private class WriteIntoOutputfileTask extends AsyncTask <TarsosDSPAudioFormat, File, AudioEvent, Integer, Long> {
    
        protected Long doInBackground(TarsosDSPAudioFormat audioFormat, File output, AudioEvent audioEvent) {
            long processedFiles = 0;
            publishProgress(0);
            WriterProcessor myWriterProcessor = new WriterProcessor(audioFormat, output);
            myWriterProcessor.process(audioEvent);
            publishProgress(100);
            processedFiles = 1;
            return processedFiles;
        }
    
        protected void onProgressUpdate(Integer... progress) {
            setProgressPercent(progress[0]);
        }
    
        protected void onPostExecute(Long result) {
            showDialog(result + " streams processed successfully");
        }
    
    }
    // Then run this code to start the task:
    new WriteIntoOutputfileTask().execute(audioFormat, output, audioEvent);
    

当然,这只是概念验证。对于实际实现,您应该将 WriterProcessor 重写为 AsyncTask 本身。您的 public boolean process(AudioEvent audioEvent) 方法将成为 AsyncTask 扩展的 doInBackground 方法的一部分。

对于更复杂的异步操作,您可以查看 IntentServiceTaskScheduler .

如果您希望您的任务是同步的(从您的代码看来并非如此),请从 DownloadManager 中找到最适合您需求的替代方案。或 SyncAdapter

关于java - FileOutputStream max_allowed_dequeued_buffers 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52553984/

相关文章:

c - OpenCL 中带有偏移量的固定内存

java - Java输出值随时间的变化

JavaFX HTML 编辑器 : Text is in one single line

java - 允许没有 else 的简单 if 语句在代码风格中没有大括号

android - Android SDK源码部分地方无法设置断点(Eclipse)

java - 缓冲 REST 响应的方法

java - 如何从 Java Card API 访问主机文件系统

android - 具有多个 mp3 文件问题的媒体播放器

java - 推送通知中未显示图像

c - 是否可以覆盖返回指针并直接跳转到另一个已经在同一台计算机上运行的程序中的函数?