其他分享
首页 > 其他分享> > android-录制回放问题(MediaRecorder MediaPlayer)

android-录制回放问题(MediaRecorder MediaPlayer)

作者:互联网

我正在尝试记录用户的声音并在按钮的onClick上播放它,但是我收到“ WARN / System.err(25236):java.io.IOException:准备失败.:状态= 0x1”.
(请参见下面的“ LogCat”)

我像这样录制音频:

recorder = new MediaRecorder();

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getApplicationContext().getFileStreamPath("audio_voicePrint").getAbsolutePath());
recorder.setOnErrorListener(this);
recorder.setOnInfoListener(this);

try {
    recorder.prepare();
    recorder.start();
} catch (IllegalStateException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

然后像这样释放它:

recorder.stop();
recorder.reset();
recorder.release();
recorder = null;

这是我尝试播放的方式:

mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

try {
mp.setDataSource(getApplicationContext(), Uri.parse("file://"+getApplicationContext().getFileStreamPath("audio_voicePrint").getAbsolutePath()+".3gp"));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

mp.start();

使用Uri Uri.parse(getApplicationContext().getFileStreamPath(“ audio_voicePrint”).getAbsolutePath())和Uri.parse(“ file://” getApplicationContext().getFileStreamPath(“ audio_voicePrint”)时,出现相同的错误. getAbsolutePath()“ .3gp”).

LogCat:

08-04 12:28:56.780: INFO/StagefrightPlayer(68): setDataSource('/data/data/com.abc.mobile/files/audio_voicePrint.3gp')
08-04 12:28:56.780: ERROR/MediaPlayer(25236): error (1, -2147483648)
08-04 12:28:56.780: WARN/System.err(25236): java.io.IOException: Prepare failed.: status=0x1
08-04 12:28:56.790: WARN/System.err(25236):     at android.media.MediaPlayer.prepare(Native Method)
08-04 12:28:56.790: WARN/System.err(25236):     at com.abc.mobile.Step4.onClick(Step4.java:228)
08-04 12:28:56.790: WARN/System.err(25236):     at android.view.View.performClick(View.java:2485)
08-04 12:28:56.790: WARN/System.err(25236):     at android.view.View$PerformClick.run(View.java:9080)
08-04 12:28:56.790: WARN/System.err(25236):     at android.os.Handler.handleCallback(Handler.java:587)
08-04 12:28:56.790: WARN/System.err(25236):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-04 12:28:56.790: WARN/System.err(25236):     at android.os.Looper.loop(Looper.java:130)
08-04 12:28:56.790: WARN/System.err(25236):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-04 12:28:56.790: WARN/System.err(25236):     at java.lang.reflect.Method.invokeNative(Native Method)
08-04 12:28:56.790: WARN/System.err(25236):     at java.lang.reflect.Method.invoke(Method.java:507)
08-04 12:28:56.800: WARN/System.err(25236):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-04 12:28:56.800: WARN/System.err(25236):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-04 12:28:56.800: WARN/System.err(25236):     at dalvik.system.NativeStart.main(Native Method)
08-04 12:28:56.800: ERROR/MediaPlayer(25236): start called in state 0
08-04 12:28:56.800: ERROR/MediaPlayer(25236): error (-38, 0)
08-04 12:28:56.810: ERROR/MediaPlayer(25236): Error (-38,0)

根据this,错误-38为:

214 const PVMFStatus PVMFErrDrmClockError = (-38);
215 /*
216 DRM license store is corrupted
217 */

解决方法:

使用此代码:

mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

try {

FileInputStream fis = new FileInputStream(yourFilename);

mp.setDataSource(fis.getFD);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

mp.start();

标签:media-player,android-mediaplayer,mediarecorder,android
来源: https://codeday.me/bug/20191208/2088840.html