我最近(几天前)开始使用 JAVA 工作,所以我很确定我犯了一些基本错误。
我创建了一个 Activity ,它通过一个按钮启动一个录制音频的服务。我在其中添加了一个计时器,该计时器通过 stopself() 停止录制并退出服务。希望这是关闭它的正确方法。
但是,当我运行该应用程序时,它会在录制期间生成多个音频文件(我将其设置为 90 分钟),并且它们以不合理的方式被切断(其中一些是 30 分钟,一些是 20 分钟, ETC)。而且服务有时似乎并没有停止。
我的服务类:
@SuppressLint("SimpleDateFormat")
public class myservice extends Service {
private MediaRecorder myRecorder;
private String outputFile = null;
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
// super.onCreate(); (not sure about this one)
SimpleDateFormat s = new SimpleDateFormat("ddMMyyyy_hhmmss");
String format = s.format(new Date());
outputFile = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/" + format + ".3gpp";
myRecorder = new MediaRecorder();
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myRecorder.setOutputFile(outputFile);
try {
myRecorder.prepare();
myRecorder.start();
} catch (IllegalStateException e) {
// start:it is called before prepare()
// prepare: it is called after start() or before setOutputFormat()
e.printStackTrace();
} catch (IOException e) {
// prepare() fails
e.printStackTrace();
}
Timer myTimer;
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
try {
myRecorder.stop();
myRecorder.reset();
myRecorder.release();
myRecorder = null;
stopSelf();
//onDestroy(); I added this one to test it out. not sure.
} catch (IllegalStateException e) {
// it is called before start()
e.printStackTrace();
} catch (RuntimeException e) {
// no valid audio/video data has been received
e.printStackTrace();
}
}
}, 60000 * 90); //90 minutes timer to stop the recording after 90 minutes
}
public int onStartCommand(Intent intent, int flags, int startId) {
//onCreate();
// return 1;
return START_STICKY; //not sure about this one
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
}
public void onPause() {
}
//what about this one ?
// @Override
// public void onDestroy() {
// myRecorder.stop();
// myRecorder.reset();
// myRecorder.release();
//}
}
我的主要 Activity :
public class MainActivity extends Activity {
private Button startBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startBtn = (Button)findViewById(R.id.start);
startBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent svc=new Intent(getBaseContext(), myservice.class);
startService(svc);
finish(); //because I want to close the UI after service started
}
});
}
}
在使用 android 服务之前,我使用线程编写应用程序。它可以工作,但计时器只能工作很短的时间,10 分钟或更短)。录音会在某个时刻停止(通常是 30 分钟或更短)并且这个长度不固定(尽管计时器设置为 90 分钟)。仍然不知道为什么,但我假设 Android 操作系统由于缺乏资源而关闭了线程。
帮助将不胜感激。
最佳答案
自从我解决后,如果有人遇到类似问题,我会发布我的解决方案。显然,服务(当处于非 Activity 状态 [无交互] 约 30 分钟时)也可以由 Android 操作系统关闭。我需要的是前台服务。官方文档中的示例:
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);
关于java - 将音频录制为 Android 服务并使用计时器停止它。录音时长不一,服务似乎没有停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24405208/