https://github.com/GautamChibde/android-audio-visualizer/wiki/Line-Bar-Visualizer I use this liblary. This my code: XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"
xmlns:aw="http://schemas.android.com/apk/res-auto"
xmlns:tools ="http://schemas.android.com/tools"
android:layout_width ="match_parent"
android:layout_height ="match_parent"
android:id ="@+id/chart_layout"
tools:context =".MRecorder"
android:orientation ="vertical"
tools:ignore="MissingClass">
<com.chibde.visualizer.LineBarVisualizer
android:id="@+id/visualizer"
android:layout_width="match_parent"
android:layout_height="250dp"/>
</RelativeLayout >
Code class .MRecorder:
public class MRecorder extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spectr_sound);
LineBarVisualizer lineBarVisualizer = findViewById(R.id.visualizer);
//то что он должен проиграть. my music
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.sample_src_main_res_raw_red_e);
// set custom color to the line.
lineBarVisualizer.setColor(ContextCompat.getColor(this, R.color.av_red));
// define custom number of bars you want in the visualizer between (10 - 256).
lineBarVisualizer.setDensity(100);
// Set you media player to the visualizer.
lineBarVisualizer.setPlayer(mediaPlayer.getAudioSessionId()); <---Error
}
}
and this my error:
E/AudioEffect: set(): AudioFlinger could not create effect, status: -1
E/visualizers-JNI: Visualizer initCheck failed -3
E/Visualizer-JAVA: Error code -3 when initializing Visualizer.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.imagewithcamera, PID: 24377
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imagewithcamera/com.example.imagewithcamera.MRecorder}: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
at android.media.audiofx.Visualizer.<init>(Visualizer.java:218)
at com.chibde.BaseVisualizer.setPlayer(BaseVisualizer.java:81)
at com.example.imagewithcamera.MRecorder.onCreate(MRecorder.java:32)
at android.app.Activity.performCreate(Activity.java:7183)
How can I fix this? I used code how written in documentation, but it give me error
My manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.imagewithcamera"
xmlns:tools="http://schemas.android.com/tools">
<!--Добавление прав на камеру-->
<uses-permission android:name="android.permission.CAMERA"/>
<!--для записи картинки в файл-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--для получения доступа к микрофону-->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.hardware.camera2.full"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
tools:replace="android:label"
android:label="Camera2"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity
android:name=".MRecorder"
android:exported="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
If you need look my manifest///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////