应用程序在包含一个写入文件的函数( )后冻结。

huangapple go评论77阅读模式
英文:

App freezes on including a function( ) that writes file from buffer

问题

我是Android开发的初学者,遇到了困难。在这里,我使用audiorecorder而不是mediarecorder来录制声音,以使用噪音消除功能,并且我正在从缓冲区将PCM文件写入输出文件,但是当我包括从缓冲区写文件的函数时,应用程序不会响应。它会录制并保存文件,但会冻结。
有人可以告诉我哪里出了问题吗?

private void writeAudioDataToFile() {
    // 将输出音频以字节形式写入
    bufferSizeInBytes = AudioRecord.getMinBufferSize(
      RECORDER_SAMPLERATE,
      RECORDER_CHANNELS,
      RECORDER_AUDIO_ENCODING
    );

    String filePath = "/sdcard/voice8K16bitmono.wav";
    short sData[] = new short[bufferSizeInBytes / 2];

    FileOutputStream os = null;
    try {
        os = new FileOutputStream(filePath);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    while (isRecording) {
        // 从麦克风获取声音输出并转换为字节格式

        ar.read(sData, 0, bufferSizeInBytes / 2);
        Log.d("eray", "Short writing to file" + sData.toString());
        try {
            // 从缓冲区将数据写入文件
            // 存储声音缓冲区
            byte bData[] = short2byte(sData);
            os.write(bData, 0, bufferSizeInBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private byte[] short2byte(short[] sData) {
    int shortArrsize = sData.length;
    byte[] bytes = new byte[shortArrsize * 2];
    for (int i = 0; i < shortArrsize; i++) {
       bytes[i * 2] = (byte) (sData[i] & 0x00FF);
       bytes[(i * 2) + 1] = (byte) (sData[i] >> 8);
       sData[i] = 0;
    }
    return bytes;
} 
英文:

I am a beginner in android development and I am stuck. Here, I am recording voice using audiorecorder instead of mediarecorder to use noise cancellation feature and I am writing a PCM file from the buffer to an output file and when I include the function for writing the file from the buffer the app just don't respond. it records and saves the file but freezes.
can somebody tell me what's wrong?

private void writeAudioDataToFile() {
// Write the output audio in byte
bufferSizeInBytes = AudioRecord.getMinBufferSize(
RECORDER_SAMPLERATE,
RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING
);
String filePath = &quot;/sdcard/voice8K16bitmono.wav&quot;;
short sData[] = new short[bufferSizeInBytese/2];
FileOutputStream os = null;
try {
os = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (isRecording) {
// gets the voice output from microphone to byte format
ar.read(sData, 0, bufferSizeInBytese/2);
Log.d(&quot;eray&quot;,&quot;Short wirting to file&quot; + sData.toString());
try {
// // writes the data to file from buffer
// // stores the voice buffer
byte bData[] = short2byte(sData);
os.write(bData, 0, bufferSizeInBytes);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private byte[] short2byte(short[] sData) {
int shortArrsize = sData.length;
byte[] bytes = new byte[shortArrsize * 2];
for (int i = 0; i &lt; shortArrsize; i++) {
bytes[i * 2] = (byte) (sData[i] &amp; 0x00FF);
bytes[(i * 2) + 1] = (byte) (sData[i] &gt;&gt; 8);
sData[i] = 0;
}
return bytes;
} 
</details>
# 答案1
**得分**: 1
我认为,你正在主线程(UI 线程)中进行文件写入操作,这就是为什么应用程序会冻结的原因。请在另一个线程中执行文件操作,例如 ***AsyncTask***。
请记住“不要执行需要超过5秒并导致应用程序冻结的操作”。
在这里,只分享要添加的概念概述:
将这个内部类添加到你的活动文件中。
```java
static class FileAsyncTask extends AsyncTask {
@Override
protected Object doInBackground(Object[] objects) {
// 在这里执行你的文件写入操作....
return null;
}
@Override
protected void onProgressUpdate(Object[] values) {
super.onProgressUpdate(values);
// 更新用户界面 - 如果文件写入成功/失败
}
}

在需要写入文件时,在你的活动文件中调用这个异步任务:

new FileAsyncTask().execute(...);
英文:

I think, you are doing file writing operation in main thread (UI thread), that's why app freezes. Kindly do your file operation in another thread Eg. AsyncTask.

Keep in mind that "you do not do the operations which takes more than 5seconds & freezes app".

Here, just sharing the overview of concept to be added:

Add this innerclass inside your activity file.

static class FileAsyncTask extends AsyncTask{
@Override
protected Object doInBackground(Object[] objects) {
// do your file writing stuff here....
return null;
}
@Override
protected void onProgressUpdate(Object[] values) {
super.onProgressUpdate(values);
// update UI - if the file writig is success / failed 
}
}

call this async task in your activity file when file need to be written:

 new FileAsyncTask().execute(...);

huangapple
  • 本文由 发表于 2020年8月8日 14:51:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63312605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定