java.lang.RuntimeException:无法初始化 Visualizer 引擎,错误:-4
Posted
技术标签:
【中文标题】java.lang.RuntimeException:无法初始化 Visualizer 引擎,错误:-4【英文标题】:java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -4 【发布时间】:2017-08-02 17:20:21 【问题描述】:在 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"/>
【问题讨论】:
请记录 mMediaPlayer.getAudioSessionId() 的值 @NickCardoso 的 mMediaPlayer.getAudioSessionId() 值为 5 这似乎是有效的,但你每次都得到相同的值?这不是我所期望的 【参考方案1】:-4
表示您提供了错误的参数。虽然 getAudioSessionId 可以在任何状态下调用,但您是否在正在使用的 MediaPlayer 上调用了prepare
和start
,以确保它在尝试可视化之前具有良好的状态?
您可能还需要在以这种方式使用 Visualiser 时添加 android.permission.RECORD_AUDIO
权限,如果音频源不是本地的,则可能还需要添加 android.permission.INTERNET
【讨论】:
我正在使用mMediaPlayer.start();
和权限 .. 使用媒体播放器代码查看我更新的问题【参考方案2】:
为 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);
【讨论】:
【参考方案3】:很可能您有 Visualizer 的 bad in the configuration 的内容,请仔细检查您的 setupVisualizer
方法中的内容。请参考similar issue。
【讨论】:
它在其他版本的模拟器中工作..只有当我签入 api 16 的模拟器时它才会工作 我正在使用mMediaPlayer = MediaPlayer.create(this, Uri.parse(filePath));
【参考方案4】:
迟到的答案,但仍然。
问题与权限无关,正如您所说,您针对 API 16 并在清单中添加了权限,因此 A.N.T 的回答甚至没有意义。
问题是你在MediaPlayer
准备好之前调用了Visualizer
。
您可以在准备好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);
【讨论】:
以上是关于java.lang.RuntimeException:无法初始化 Visualizer 引擎,错误:-4的主要内容,如果未能解决你的问题,请参考以下文章