Android 可视化工具 - 3 错误

Android Visualizer - 3 error

我到处寻找解决办法,但没有任何帮助。如我所见,一切都已设置好,但可视化工具无法正常工作。

Activity

package com.example.daber.testapplication;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

 public class MainActivity extends AppCompatActivity {
 private MediaPlayer mMediaPlayer;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      initAudio();
  }

  private void initAudio() {
      System.out.println("Im initializing !");
      setVolumeControlStream(AudioManager.STREAM_MUSIC);
      mMediaPlayer = MediaPlayer.create(this, R.raw.test);
      mMediaPlayer.setLooping(true);
      mMediaPlayer.start();
      new Visualizer(mMediaPlayer.getAudioSessionId());
   }
 }

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.daber.testapplication">

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
 </application>

</manifest>

File Schema

错误代码

09-03 08:28:58.341 13452-13452/com.example.daber.testapplication           
E/AudioEffect: set(): AudioFlinger could not create effect, status: -1
09-03 08:28:58.341 13452-13452/com.example.daber.testapplication    
E/visualizers-JNI: Visualizer initCheck failed -3
09-03 08:28:58.341 13452-13452/com.example.daber.testapplication   
E/Visualizer-JAVA: Error code -3 when initializing Visualizer.
09-03 08:28:58.341 13452-13452/com.example.daber.testapplication  
E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.daber.testapplication, PID: 13452

java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.example.daber.testapplication/com.example.daber.testapplication.MainActivity}: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
at android.media.audiofx.Visualizer.<init>(Visualizer.java:218)
at com.example.daber.testapplication.MainActivity.initAudio(MainActivity.java:25)
at com.example.daber.testapplication.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我也尝试 "new Visualizer(0)" ,但给出了同样的错误。我们将不胜感激。

确保您记得在 android 6+

上请求许可
List<String> permissions = new ArrayList<String>();

private boolean askPermission() {


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        int RECORD_AUDIO = checkSelfPermission(Manifest.permission.RECORD_AUDIO );

        if (RECORD_AUDIO != PackageManager.PERMISSION_GRANTED) {
            permissions.add(Manifest.permission.RECORD_AUDIO);
        }


        if (!permissions.isEmpty()) {
            requestPermissions(permissions.toArray(new String[permissions.size()]), 1);
        } else
            return false;
    } else
        return false;
    return true;

}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == 1) {

        boolean result = true;
        for (int i = 0; i < permissions.length; i++) {
            result = result && grantResults[i] == PackageManager.PERMISSION_GRANTED;
        }
        if (!result) {

            Toast.makeText(this, "..", Toast.LENGTH_LONG).show();
           // askPermission();
        } else {
        }
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}