我刚刚将我的应用程序更新为targetSdkVersion 26,并且出现了以下问题:
java.lang.RuntimeException: Unable to start service
java.lang.RuntimeException: setAudioSource failed.
At the logcat it says that the problem is in my service class, in the
遵循两行代码:
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
and
public int onStartCommand(Intent intent, int flags, int startId) {
startRecording();
RecordingService
public class RecordingService extends Service {
private static final String LOG_TAG = "RecordingService";
private String mFileName = null;
private String mFilePath = null;
private MediaRecorder mRecorder = null;
private DBHelper mDatabase;
private long mStartingTimeMillis = 0;
private long mElapsedMillis = 0;
private int mElapsedSeconds = 0;
private OnTimerChangedListener onTimerChangedListener = null;
private static final SimpleDateFormat mTimerFormat = new SimpleDateFormat("mm:ss", Locale.getDefault());
private Timer mTimer = null;
private TimerTask mIncrementTimerTask = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
public interface OnTimerChangedListener {
void onTimerChanged(int seconds);
}
@Override
public void onCreate() {
super.onCreate();
mDatabase = new DBHelper(getApplicationContext());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startRecording();
return START_STICKY;
}
@Override
public void onDestroy() {
if (mRecorder != null) {
stopRecording();
}
super.onDestroy();
}
public void startRecording() {
setFileNameAndPath();
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setOutputFile(mFilePath);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
try {
mRecorder.prepare();
mRecorder.start();
mStartingTimeMillis = System.currentTimeMillis();
//startTimer();
//startForeground(1, createNotification());
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
public void setFileNameAndPath(){
int count = 0;
File f;
do{
count++;
mFileName = getString(R.string.default_file_name)
+ " #" + (mDatabase.getCount() + count) + ".mp4";
mFilePath = Environment.getExternalStorageDirectory().getAbsolutePath();
mFilePath += "/SoundRecorder/" + mFileName;
f = new File(mFilePath);
}while (f.exists() && !f.isDirectory());
}
public void stopRecording() {
mRecorder.stop();
mElapsedMillis = (System.currentTimeMillis() - mStartingTimeMillis);
mRecorder.release();
Toast.makeText(this, getString(R.string.toast_recording_finish) + " " + mFilePath, Toast.LENGTH_LONG).show();
//remove notification
if (mIncrementTimerTask != null) {
mIncrementTimerTask.cancel();
mIncrementTimerTask = null;
}
mRecorder = null;
try {
mDatabase.addRecording(mFileName, mFilePath, mElapsedMillis);
} catch (Exception e){
Log.e(LOG_TAG, "exception", e);
}
}
private void startTimer() {
mTimer = new Timer();
mIncrementTimerTask = new TimerTask() {
@Override
public void run() {
mElapsedSeconds++;
if (onTimerChangedListener != null)
onTimerChangedListener.onTimerChanged(mElapsedSeconds);
NotificationManager mgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mgr.notify(1, createNotification());
}
};
mTimer.scheduleAtFixedRate(mIncrementTimerTask, 1000, 1000);
}
//TODO:
private Notification createNotification() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.notification_recording))
.setContentText(mTimerFormat.format(mElapsedSeconds * 1000))
.setOngoing(true);
mBuilder.setContentIntent(PendingIntent.getActivities(getApplicationContext(), 0,
new Intent[]{new Intent(getApplicationContext(), MainActivity.class)}, 0));
return mBuilder.build();
}
}
问题出在哪里?
这是完整的Logcat
08-10 10:29:24.357 12325-12325/com.anastasios.notesrecording E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.anastasios.notesrecording, PID: 12325
java.lang.RuntimeException: Unable to start service com.anastasios.notesrecording.RecordingService@fb79b63 with Intent { cmp=com.anastasios.notesrecording/.RecordingService }: java.lang.RuntimeException: setAudioSource failed.
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3491)
at android.app.ActivityThread.-wrap20(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.RuntimeException: setAudioSource failed.
at android.media.MediaRecorder.setAudioSource(Native Method)
at com.anastasios.notesrecording.RecordingService.startRecording(RecordingService.java:84)
at com.anastasios.notesrecording.RecordingService.onStartCommand(RecordingService.java:65)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3474)
at android.app.ActivityThread.-wrap20(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)