我正在尝试创建一个作为前台服务运行的录音机,因为我不想阻止我的UI线程,所以我在AudioRecorderService
类中创建了一个工作线程。但我面临两个问题:
当我从工作线程中运行记录器时,它无法正常工作,因为它存储了一个空白的mp4文件。但是,如果我在UI Thread上运行它,它可以正常工作。
当我的应用程序在后台并且我从通知抽屉访问我的应用程序时,它会重新启动活动,因为我的计时器显示“0:00”而不是已用时间并且记录按钮设置为其原始状态。
我的AudioRecorderService类代码如下:
public class AudioRecorderService extends Service {
private MediaRecorder mAudioRecorder;
private String fileName;
private Thread thread;
public AudioRecorderService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Recording", Toast.LENGTH_LONG).show();
thread = new Thread(new Runnable() {
@Override
public void run() {
fileName = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/" + "test.mp4";
if (mAudioRecorder == null) {
mAudioRecorder = new MediaRecorder();
mAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mAudioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mAudioRecorder.setAudioEncodingBitRate(256);
mAudioRecorder.setAudioChannels(1);
mAudioRecorder.setAudioSamplingRate(44100);
mAudioRecorder.setOutputFile(fileName);
}
try {
mAudioRecorder.prepare();
mAudioRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
if (thread.isInterrupted()) {
if (mAudioRecorder != null) {
mAudioRecorder.stop();
mAudioRecorder.release();
mAudioRecorder = null;
}
}
}
});
thread.start();
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
//Setup Notification
final Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.addCategory("android.intent.category.LAUNCHER");
final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
final Notification notification = new Notification.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setContentTitle("Audio Recorder")
.setContentText("Click to Access Audio Recorder")
.setContentIntent(pendingIntent).build();
startForeground(1, notification);
}
@Override
public void onDestroy() {
if (!thread.isInterrupted()) {
thread.interrupt();
}
Toast.makeText(this, "Recording Done", Toast.LENGTH_LONG).show();
}
}
Logcat错误消息:
06-20 00:34:45.618 28132-28132/? E/dalvikvm: Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.e.a
06-20 00:34:46.026 28132-28132/? E/WifiManager: mWifiServiceMessenger == null
06-20 00:34:49.945 736-736/? E/Launcher: onNewIntent: intent = Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10600000 cmp=com.android.launcher/com.android.launcher2.Launcher }
06-20 00:35:09.559 28441-28441/? E/Gallery2/MovieActivity: NFC not available!
06-20 00:35:09.887 137-1726/? E/DrmMtkUtil/DrmUtil: checkDcf: not dcf type, dcf version value [0]
06-20 00:35:09.898 137-1726/? E/DrmMtkUtil/DrmUtil: checkDcf: not dcf type, dcf version value [0]
06-20 00:35:09.898 137-1726/? E/DrmMtkUtil/DrmUtil: checkDcf: not dcf type, dcf version value [0]
06-20 00:35:09.932 136-136/? E/DrmMtkUtil/DrmUtil: parseDcf: not dcf type, dcf version value [0]
06-20 00:35:09.932 136-136/? E/DrmMtkPlugIn: onOpenDecryptSession() : failed to parse dcf file.
06-20 00:35:09.972 137-1726/? E/FlvExtractor: [ERROR]:Not an FLV file!!!
06-20 00:35:09.972 137-1726/? E/FlvExtractor: [ERROR]:[FLV]mtk_flv_extractor_recognize OUT
06-20 00:35:09.972 137-1726/? E/DataSource: SniffFLV return 0
06-20 00:35:09.977 137-1726/? E/MediaPlayerService: error: -2147483648
06-20 00:35:09.977 28441-28441/? E/MediaPlayer: Unable to to create media player
06-20 00:35:09.977 28441-28453/? E/MediaPlayer: error (262, 0)
06-20 00:35:10.239 28441-28441/? E/MediaPlayer: Error (262,0)
06-20 00:35:10.973 28441-28441/? E/MediaPlayer: stop called in state 0
06-20 00:35:11.730 736-736/? E/Launcher: onNewIntent: intent = Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10600000 cmp=com.android.launcher/com.android.launcher2.Launcher }
06-20 00:35:16.985 137-457/? E/OMXCodec: @@ [OUT] def.nBufferSize = 8192
06-20 00:35:16.985 137-457/? E/OMXCodec: @@ [OUT] totalSize = 16384
06-20 00:35:30.973 803-1055/? E/CellLocation: create GsmCellLocation
06-20 00:35:30.982 803-1055/? E/CellLocation: create GsmCellLocation
06-20 00:35:31.686 595-595/? E/wpa_supplicant: Sorted scan results
06-20 00:35:31.686 595-595/? E/wpa_supplicant: 48:ee:0c:1c:d6:ce freq=2417 qual=0 noise=0 level=-29 flags=0xb
06-20 00:35:31.686 595-595/? E/wpa_supplicant: IEs
06-20 00:35:31.686 595-595/? E/wpa_supplicant: 0007442d4c696e6b50010882848b960c1218240301022a010032043048606c2d1a6e181fffff0000000000000000000000000000000000000000003d16020000000000000000000000000000000000000000004a0e14000a00b400c8001400050019007f0101dd160050f20101000050f20201000050f20401000050f20230140100000fac020100000fac040100000fac020000dd180050f2020101000003a4000027a4000042435e0062322f00dd1e00904c336e181fffff000000000000000000000000000000000000000000dd1a00904c3402000000000000000000000000000000000000000000dd0600e04c020160
06-20 00:35:31.686 595-595/? E/wpa_supplicant: Beacon IEs
06-20 00:35:31.686 595-595/? E/wpa_supplicant: 0007442d4c696e6b50010882848b960c1218240301020504000100002a010432043048606c2d1a6e181fffff0000000000000000000000000000000000000000003d16020000000000000000000000000000000000000000004a0e14000a00b400c8001400050019007f0101dd160050f20101000050f20201000050f20401000050f20230140100000fac020100000fac040100000fac020000dd180050f2020101000003a4000027a4000042435e0062322f00dd1e00904c336e181fffff000000000000000000000000000000000000000000dd1a00904c3402000000000000000000000000000000000000000000dd0600e04c020160
06-20 00:35:31.686 595-595/? E/wpa_supplicant: 00:17:7c:3a:59:56 freq=2437 qual=0 noise=0 level=-68 flags=0xb
06-20 00:35:31.686 595-595/? E/wpa_supplicant: IEs
06-20 00:35:31.686 595-595/? E/wpa_supplicant: 000d2a2a2a2a76697275732a2a2a2a010882848b960c1218240301062a010032043048606c30140100000fac020100000fac020100000fac020000dd180050f2020101000003a4000027a4000042435e0062322f00dd0600e04c020160
06-20 00:35:31.686 595-595/? E/wpa_supplicant: Beacon IEs
06-20 00:35:31.687 595-595/? E/wpa_supplicant: 000d2a2a2a2a76697275732a2a2a2a010882848b960c1218240301060504000100002a010432043048606c30140100000fac020100000fac020100000fac020000dd180050f2020101000003a4000027a4000042435e0062322f00dd0600e04c020160
06-20 00:35:31.687 595-595/? E/wpa_supplicant: e4:f4:c6:43:dc:88 freq=2412 qual=0 noise=0 level=-74 flags=0x2b
06-20 00:35:31.687 595-595/? E/wpa_supplicant: IEs
06-20 00:35:31.687 595-595/? E/wpa_supplicant: 00094e4554474541523238010882848b961224486c0301012a010032040c1830602d1aee1117ffff0000010000000000000000000000000c00000000003d160105060000000000000000000000000000000000000030140100000fac040100000fac040100000fac020000dd180050f2020101000003a4000027a4000042435e0062322f000b0504001f127a4a0e14000a002c01c800140005001900dd07000c43070000000706455520010d10dda80050f204104a0001101044000102103b0001031047001038833092309218838e48e4f4c643dc881021000d4e4554474541522c20496e632e1023001d4e45544745415220576972656c6573732041636365737320506f696e74102400064a52363135301042000831323334353637381054000800060050f2040001101100144a52363135302028576972656c6573732041502910080002210c103c0001011049000600372a000120
06-20 00:35:31.687 595-595/? E/wpa_supplicant: 0c:d2:b5:60:63:5c freq=2447 qual=0 noise=0 level=-87 flags=0xb
06-20 00:35:31.687 595-595/? E/wpa_supplicant: IEs
06-20 00:35:31.687 595-595/? E/wpa_supplicant: 0015547972696f6e2077696c6c20646965206e6578742e010882848b961224486c03010832040c18306007064d5820010b143308200102030405060733082105060708090a0b05040001001add1a0050f20101000050f20202000050f2020050f20401000050f2022a01042d1a6e1117ff000000010000000000000000000000000c00000000003d16080000000000000000000000000000000000000000004a0e14000a002c01c8001400050019007f0101dd180050f2020101000003a4000027a4000042435e0062322f00dd07000c4304000000
06-20 00:35:31.687 595-595/? E/wpa_supplicant: 0c:d2:b5:46:4d:40 freq=2462 qual=0 noise=0 level=-88 flags=0xb
06-20 00:35:31.687 595-595/? E/wpa_supplicant: IEs
06-20 00:35:31.687 595-595/? E/wpa_supplicant: 00064d756b657368010882848b961224486c03010b2a010432040c1830602d1a6e1117ff000000010000000000000000000000000c00000000003d160b0006000000000000000000000000000000000000003e0100dd1a0050f20101000050f20202000050f2020050f20401000050f202dd180050f2020101000003a5000027a4000042435e0062322f004a0e14000a002c01c8001400050019007f0101dd07000c430400000007064d5820010b10
06-20 00:35:31.687 595-595/? E/wpa_supplicant: Beacon IEs
06-20 00:35:31.687 595-595/? E/wpa_supplicant: 00064d756b657368010882848b961224486c03010b32040c18306007064d5820010b143308200102030405060733082105060708090a0b050400010000dd1a0050f20101000050f20202000050f2020050f20401000050f2022a01042d1a6e1117ff000000010000000000000000000000000c00000000003d160b0006000000000000000000000000000000000000004a0e14000a002c01c8001400050019007f0101dd180050f2020101000003a5000027a4000042435e0062322f00dd07000c4304000000
06-20 00:35:31.687 595-595/? E/wpa_supplicant: 0c:d2:b5:3f:b7:04 freq=2447 qual=0 noise=0 level=-89 flags=0xb
06-20 00:35:31.687 595-595/? E/wpa_supplicant: IEs
06-20 00:35:31.687 595-595/? E/wpa_supplicant: 001041697274656c5f5a65726f746f756368010882848b961224486c0301082a010432040c1830602d1a6e1117ff000000010000000000000000000000000c00000000003d16080406000000000000000000000000000000000000003e0100dd1a0050f20101000050f20202000050f2020050f20401000050f202dd180050f2020101000003a4000027a4000042435e0062322f004a0e14000a002c01c8001400050019007f0101dd07000c430400000007064d5820010b10
06-20 00:35:31.726 595-595/? E/wpa_supplicant: WPS: WFA subelement id=0 len=1
06-20 00:35:31.726 595-595/? E/wpa_supplicant: WPS: WFA subelement id=0 len=1
06-20 00:35:31.726 595-595/? E/wpa_supplicant: WPS: WFA subelement id=0 len=1
06-20 00:35:42.021 28679-28834/? E/NativeCrypto: ssl=0x53324070 cert_verify_callback x509_store_ctx=0x53323a48 arg=0x0
06-20 00:35:42.021 28679-28705/? E/NativeCrypto: ssl=0x53324308 cert_verify_callback x509_store_ctx=0x52e84a48 arg=0x0
06-20 00:35:42.021 28679-28834/? E/NativeCrypto: ssl=0x53324070 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
06-20 00:35:42.022 28679-28705/? E/NativeCrypto: ssl=0x53324308 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
请帮助!!!