mt6735 FM录音过程中删除录音文件
时间:10-02
整理:3721RD
点击:
[DESCRIPTION]
1.FM录音;
2.进入文件管理器删除正在录音的文件;
3.进入FM主界面,观察手机反应:录音还在继续;
4.点击停止后保存;
5.进入文件管理器查看,没有录音文件;
[SOLUTION]
利用FileObserver增加对文件的监听,如果文件有被删除/重命名,则显示错误Toast
一. FMRadioActivity.Java replace this function
private void updateRecordingState(int recorderState) {
mRecordState = recorderState;
refreshRecordingStatus(recorderState);
switch (recorderState) {
case FMRecorder.STATE_RECORDING:
showToast(getString(R.string.toast_start_recording));
LogUtils.d(TAG, "updateRecordingState:startRecording");
mHandler.sendEmptyMessage(FMRadioListener.MSGID_REFRESH);
break;
case FMRecorder.STATE_PLAYBACK:
mHandler.sendEmptyMessage(FMRadioListener.MSGID_REFRESH);
break;
case FMRecorder.STATE_IDLE:
LogUtils.d(TAG, "updateRecordingState:remove message");
mHandler.removeMessages(FMRadioListener.MSGID_REFRESH);
break;
//添加如下
case FMRecorder.STATE_FILE_NOT_EXIST:
LogUtils.d(TAG, "updateRecordingState: dismiss dialog for file not
exist");
dismissSaveRecordingDialog();
changeRecordingMode(false);
changeRecordingMode(true);
mIsNeedShowRecordDlg = false;
break;
//添加如上
default:
mHandler.removeMessages(FMRadioListener.MSGID_REFRESH);
break;
}
}
二. FMRecorder.java
1.添加class RecordingFileObserver
private class RecordingFileObserver extends FileObserver {
public RecordingFileObserver(String path) {
super(path);
}
public RecordingFileObserver(String path, int mask) {
super(path, mask);
}
@Override
public void onEvent(int event, String path) {
LogUtils.d(TAG, "onEvent event:" + event + ", path:" + path);
switch (event) {
case FileObserver.ATTRIB:
case FileObserver.DELETE:
case FileObserver.DELETE_SELF:
LogUtils.d(TAG, "onEvent recording file is delete or moved");
stopWatching();
setState(STATE_FILE_NOT_EXIST);
stopRecording();
setError(ERROR_RECORDER_INTERNAL);
break;
default:
break;
}
}
}
2.添加两个方法
private void startWatching() {
if (mFileObserver != null) {
mFileObserver.stopWatching();
}
String path = mRecordFile.getAbsolutePath();
mFileObserver = new RecordingFileObserver(path,
FileObserver.DELETE_SELF | FileObserver.ATTRIB);
LogUtils.d(TAG, "FileObserver start watching");
mFileObserver.startWatching();
}
private void stopWatching() {
if (mFileObserver != null) {
LogUtils.d(TAG, "FileObserver stop watching");
mFileObserver.stopWatching();
mFileObserver = null;
}
}
3. 修改startRecording
public void startRecording(Context context) {
。
try {
if (mRecordFile.createNewFile()) {
startWatching();//添加此行
}
} catch (IOException ioex) {
LogUtils.e(TAG, "IOException while createTempFile: " + ioex);
ioex.printStackTrace();
setError(ERROR_SDCARD_WRITE_FAILED);
return;
}
// set record parameter and start recording
。
4.修改saveRecording
public void saveRecording(Context context, String newName) {
。
stopWatching();//添加此行
LogUtils.d(TAG, "<< saveRecording(" + newName + ")");
}
5.修改discardRecording
public void discardRecording() {
LogUtils.d(TAG, ">> discardRecording");
stopWatching();//添加此行
。
6.修改onError
public void onError(MediaRecorder mr, int what, int extra) {
LogUtils.e(TAG, "MediaRecorder.onError: what=" + what + ", extra=" +
extra);
stopWatching();//添加此行
。
1.FM录音;
2.进入文件管理器删除正在录音的文件;
3.进入FM主界面,观察手机反应:录音还在继续;
4.点击停止后保存;
5.进入文件管理器查看,没有录音文件;
[SOLUTION]
利用FileObserver增加对文件的监听,如果文件有被删除/重命名,则显示错误Toast
一. FMRadioActivity.Java replace this function
private void updateRecordingState(int recorderState) {
mRecordState = recorderState;
refreshRecordingStatus(recorderState);
switch (recorderState) {
case FMRecorder.STATE_RECORDING:
showToast(getString(R.string.toast_start_recording));
LogUtils.d(TAG, "updateRecordingState:startRecording");
mHandler.sendEmptyMessage(FMRadioListener.MSGID_REFRESH);
break;
case FMRecorder.STATE_PLAYBACK:
mHandler.sendEmptyMessage(FMRadioListener.MSGID_REFRESH);
break;
case FMRecorder.STATE_IDLE:
LogUtils.d(TAG, "updateRecordingState:remove message");
mHandler.removeMessages(FMRadioListener.MSGID_REFRESH);
break;
//添加如下
case FMRecorder.STATE_FILE_NOT_EXIST:
LogUtils.d(TAG, "updateRecordingState: dismiss dialog for file not
exist");
dismissSaveRecordingDialog();
changeRecordingMode(false);
changeRecordingMode(true);
mIsNeedShowRecordDlg = false;
break;
//添加如上
default:
mHandler.removeMessages(FMRadioListener.MSGID_REFRESH);
break;
}
}
二. FMRecorder.java
1.添加class RecordingFileObserver
private class RecordingFileObserver extends FileObserver {
public RecordingFileObserver(String path) {
super(path);
}
public RecordingFileObserver(String path, int mask) {
super(path, mask);
}
@Override
public void onEvent(int event, String path) {
LogUtils.d(TAG, "onEvent event:" + event + ", path:" + path);
switch (event) {
case FileObserver.ATTRIB:
case FileObserver.DELETE:
case FileObserver.DELETE_SELF:
LogUtils.d(TAG, "onEvent recording file is delete or moved");
stopWatching();
setState(STATE_FILE_NOT_EXIST);
stopRecording();
setError(ERROR_RECORDER_INTERNAL);
break;
default:
break;
}
}
}
2.添加两个方法
private void startWatching() {
if (mFileObserver != null) {
mFileObserver.stopWatching();
}
String path = mRecordFile.getAbsolutePath();
mFileObserver = new RecordingFileObserver(path,
FileObserver.DELETE_SELF | FileObserver.ATTRIB);
LogUtils.d(TAG, "FileObserver start watching");
mFileObserver.startWatching();
}
private void stopWatching() {
if (mFileObserver != null) {
LogUtils.d(TAG, "FileObserver stop watching");
mFileObserver.stopWatching();
mFileObserver = null;
}
}
3. 修改startRecording
public void startRecording(Context context) {
。
try {
if (mRecordFile.createNewFile()) {
startWatching();//添加此行
}
} catch (IOException ioex) {
LogUtils.e(TAG, "IOException while createTempFile: " + ioex);
ioex.printStackTrace();
setError(ERROR_SDCARD_WRITE_FAILED);
return;
}
// set record parameter and start recording
。
4.修改saveRecording
public void saveRecording(Context context, String newName) {
。
stopWatching();//添加此行
LogUtils.d(TAG, "<< saveRecording(" + newName + ")");
}
5.修改discardRecording
public void discardRecording() {
LogUtils.d(TAG, ">> discardRecording");
stopWatching();//添加此行
。
6.修改onError
public void onError(MediaRecorder mr, int what, int extra) {
LogUtils.e(TAG, "MediaRecorder.onError: what=" + what + ", extra=" +
extra);
stopWatching();//添加此行
。