在API 16上运行应用程序时出现以下错误。
java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -4
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -4
at android.media.audiofx.Visualizer.<init>(Visualizer.java:199)
at xyz.activity.AudioPreviewActivity.setupVisualizerFxAndUI(AudioPreviewActivity.java:83)
at xyz.activity.AudioPreviewActivity.initAudio(AudioPreviewActivity.java:60)
at xyz.activity.AudioPreviewActivity.onCreate(AudioPreviewActivity.java:40)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
错误行 -
mVisualizer = new Visualizer(mMediaPlayer.getAudioSessionId());
代码
private void setupVisualizerFxAndUI() {
mMediaPlayer = MediaPlayer.create(this, Uri.parse(filePath));
mMediaPlayer
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mVisualizer.setEnabled(false);
}
});
mMediaPlayer.start();
mMediaPlayer.setLooping(true);
mVisualizer = new Visualizer(mMediaPlayer.getAudioSessionId());
mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
mVisualizer.setDataCaptureListener(
new Visualizer.OnDataCaptureListener() {
public void onWaveFormDataCapture(Visualizer visualizer,
byte[] bytes, int samplingRate) {
mVisualizerView.updateVisualizer(bytes);
}
public void onFftDataCapture(Visualizer visualizer,
byte[] bytes, int samplingRate) {
}
}, Visualizer.getMaxCaptureRate() / 2, true, false);
}
我已经在清单中添加了以下权限 -
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
答案 0 :(得分:3)
-4
表示你给出了一个错误的参数。虽然可以在任何状态下调用getAudioSessionId,但您是否在正在使用的MediaPlayer上调用了prepare
和start
,以确保它在尝试可视化之前具有良好的状态?
在使用Visualiser时,您可能还需要添加android.permission.RECORD_AUDIO
权限;如果音频源不是本地的,则可能需要android.permission.INTERNET
答案 1 :(得分:3)
为api 23及更高版本添加运行时权限。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
int hasAudioPermission = checkSelfPermission(Manifest.permission.RECORD_AUDIO);
int hasInternetPermission = checkSelfPermission(Manifest.permission.INTERNET);
List<String> permissions = new ArrayList<>();
if (hasAudioPermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.RECORD_AUDIO);
}
if (hasInternetPermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.INTERNET);
}
if (!permissions.isEmpty()) {
requestPermissions(permissions.toArray(new String[permissions.size()]), PERMISSION);
}
}
答案 2 :(得分:1)
您可能有bad in the configuration个展示台,请仔细检查setupVisualizer
方法中的内容。请参阅similar issue。
答案 3 :(得分:1)
最新答案,但仍然如此。
问题与权限无关,如您所述,您以API 16为目标,并在清单中添加了权限,因此A.N.T的答案甚至没有任何意义
问题在于您在准备Visualizer
之前正在呼叫MediaPlayer
。
您可以在准备好mMediaPlayer
之后通过调用它来解决此问题,如下所示:
private void setupMediaPlayer(String filePath) {
mMediaPlayer = MediaPlayer.create(this, Uri.parse(filePath));
//Check when MediaPlayer is completed
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer mediaPlayer) {
mVisualizer.setEnabled(false);
}
});
//Check when MediaPlayer is prepared
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
//MediaPlayer is prepared and playback can now start
startPlaying();
}
});
}
一旦mMediaPlayer
准备好了:
private void startPlaying() {
mMediaPlayer.start();
mMediaPlayer.setLooping(true);
//Now we can call the Visualizer
createVisualizer();
}
然后您可以像这样呼叫Visualizer
:
private void createVisualizer() {
mVisualizer = new Visualizer(mMediaPlayer.getAudioSessionId());
mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
@Override
public void onWaveFormDataCapture(Visualizer visualizer, byte[] waveform, int samplingRate) {
mVisualizerView.updateVisualizer(bytes);
}
public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {
//Do something you like
}
}, Visualizer.getMaxCaptureRate() / 2, true, false);
}