android - 背景录音会自行停止

标签 android audio recording

我正在设计一个后台录音代码,如下所示

            mediaRecorder = new MediaRecorder();
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setOutputFormat(outputformat);
            mediaRecorder.setAudioEncoder(audioencoder);
            mediaRecorder.setOutputFile(mRecFile.getAbsolutePath());
            mediaRecorder.prepare();
            mediaRecorder.start();

            Thread.sleep(mDuration);

            mediaRecorder.stop();
            mediaRecorder.reset();
            mediaRecorder.release();

此代码在线程的 run() 中,当服务被主 Activity 绑定(bind)时,线程将启动。如果我按“返回”键离开 Activity (将释放绑定(bind)),我发现录制将继续。但是,当我将 mDuration 设置为非常大的 num(3600000 (1 小时)) 时,录制会在一段时间后停止,而 mediaRecorder.stop();永远不会执行。

如果我不离开 Activity 。录音总是会正常停止。我想知道的是为什么我的背景音录音时间长了不能做?

最佳答案

您应该使用 ForegroundService在后台录制音频。

A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.


您可以获取更多关于 Android 前台服务的信息 HERE

关于android - 背景录音会自行停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18633124/

相关文章:

android - 了解 ImageView Matrix 的用法

c++ - SFML 内部 OpenAL 错误

捕捉软件缺陷的录屏软件

带 MIC 的 Android AudioRecord 耳机

java - HTTPURLConnection 断开与关闭

java - Android 内部存储 VS 共享首选项

android - ADB shell 命令在一行/bash 脚本上?

java - 手动更改扬声器位置

c++ - Portaudio + Opus 编码/解码音频输入

用于输入表面和音频的 Android MediaMuxer