-
一、不用service实现后台录音功能
1.在onPause()方法内实现:
01.
if
(mRecorder.state() == Recorder.RECORDING_STATE) {
02.
03.
if
(mMode ==
true
) {
04.
mRecorder.stop();
05.
return
;
06.
}
07.
08.
Intent notificationIntent =
new
Intent(
"android.provider.MediaStore.RECORD_SOUND"
);
09.
10.
PendingIntent contentIntent = PendingIntent.getActivity(
this
,
0
, notificationIntent,
0
);
11.
12.
long
when = System.currentTimeMillis();
// notification time
13.
14.
Context context = getApplicationContext();
15.
16.
Notification notification =
new
Notification(R.drawable.ic_launcher_soundrecorder,
null
, when);
17.
18.
Resources res = getResources();
19.
20.
String message1 = res.getString(R.string.app_name);
21.
22.
String message2 = res.getString(R.string.recording_in_progress);
23.
notification.setLatestEventInfo(context, message1, message2, contentIntent);
24.
String ns = Context.NOTIFICATION_SERVICE;
25.
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
26.
mNotificationManager.notify(NOTIFICATION_ID, notification);
27.
mInNotification =
true
;
28.
}
2.onResume()方法内去掉通知栏内的通知
01.
if
(mInNotification ==
true
) {
02.
03.
String ns = Context.NOTIFICATION_SERVICE;
04.
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
05.
06.
mNotificationManager.cancel(NOTIFICATION_ID);
07.
08.
mInNotification =
false
;
09.
}
3.在androidmanifext.xml中设置activity mode为singleTop。
二、录音文件使用当前时间
01.
public
String getCurrentTime(){
02.
SimpleDateFormat sdf=
new
SimpleDateFormat(
"yyyyMMddhhmmss"
);
03.
String nowTime=sdf.format(
new
Date());
04.
return
nowTime;
05.
}
06.
public
File createTempFile_test(String prefix, String suffix, File directory)
07.
throws
IOException {
08.
// Force a prefix null check first
09.
if
(prefix.length() <
3
) {
10.
throw
new
IllegalArgumentException(
"prefix must be at least 3 characters"
);
11.
}
12.
if
(suffix ==
null
) {
13.
suffix =
".tmp"
;
14.
}
15.
File tmpDirFile = directory;
16.
if
(tmpDirFile ==
null
) {
17.
String tmpDir = System.getProperty(
"java.io.tmpdir"
,
"."
);
18.
tmpDirFile =
new
File(tmpDir);
19.
}
20.
File result;
21.
result =
new
File(tmpDirFile, prefix + getCurrentTime() + suffix);
22.
if
(result.exists()){
23.
result.delete();
24.
}
25.
if
(!result.exists()){
26.
result.createNewFile();
27.
}
28.
return
result;
29.
}
三。解决先开启一个录音播放器,按home键进去后台播放,此时通过短信附件开启录音机,这时应该关闭后台的那个录音机
解决办法使用广播进行关闭,代码如下:
01.
//处理广播
02.
03.
public
class
SoundRecorder
extends
Activity
04.
implements
Button.OnClickListener, Recorder.OnStateChangedListener, OnSeekBarChangeListener {
05.
private
BroadcastReceiver mReceiver=
new
BroadcastReceiver() {
06.
@Override
07.
public
void
onReceive(Context context, Intent intent) {
08.
// TODO Auto-generated method stub
09.
if
(mRecorder!=
null
){
10.
mRecorder.stop();
11.
saveSample();
12.
mRecorder.clear();
13.
}
14.
if
(mInNotification ==
true
) {
15.
16.
17.
String ns = Context.NOTIFICATION_SERVICE;
18.
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
19.
20.
21.
mNotificationManager.cancel(NOTIFICATION_ID);
22.
23.
24.
mInNotification =
false
;
25.
}
26.
27.
28.
}
29.
};
30.
31.
//onCreate()中先发送广播再注册广播,第一次就接受不到广播,等第三方应用调用时起作用
32.
33.
Intent intent=
new
Intent();
34.
intent.setAction(ACTION_STOP);
35.
sendBroadcast(intent);
36.
37.
38.
IntentFilter filter =
new
IntentFilter();
39.
filter.addAction(ACTION_STOP);
40.
this
.registerReceiver(mReceiver, filter);
录音机的一些问题
最新推荐文章于 2024-07-16 08:34:09 发布