Issue
I am trying to visualize an file in android.I am using the following library to do it com.chibde:audiovisualizer:2.1.0
. But my app crashes saying
java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3 at android.media.audiofx.Visualizer
I have written this code by reading this blog https://mindorks.com/android/store/Equalizers-and-Visualizations/gautamchibde/android-audio-visualizer.
The following is my MainActivity.java
public class MainActivity extends AppCompatActivity {
ImageButton playPauseBtn;
private static final String TAG = "MainActivity";
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playPauseBtn = (ImageButton) findViewById(R.id.ib_play_pause);
setPlayer();
}
public void playPause(View view){
if(mediaPlayer != null){
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
playPauseBtn.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.ic_play_red_48dp));
}
else
{
mediaPlayer.start();
playPauseBtn.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.ic_pause_red_48dp));
}
}
}
public void setPlayer(){
Log.d(TAG, "setPlayer: setPlayer() called");
mediaPlayer = MediaPlayer.create(this,R.raw.normal);
mediaPlayer.setLooping(false);
init();
}
public void init(){
Log.d(TAG, "init: init() called");
LineVisualizer lineVisualizer = findViewById(R.id.lineVisualizer);
// set custom color to the line.
lineVisualizer.setColor(ContextCompat.getColor(this, R.color.custom));
// set the line with for the visualizer between 1-10 default 1.
lineVisualizer.setStrokeWidth(1);
// Set you media player to the visualizer.
lineVisualizer.setPlayer(mediaPlayer);
}
}
//The following is my ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="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:orientation="vertical"
tools:context="com.techmate.asifa.waves.MainActivity">
<com.chibde.visualizer.LineVisualizer
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="@+id/lineVisualizer"/>
<include layout="@layout/layout_audio_buttons"/>
</LinearLayout>
the following is my gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion "29.0.0"
defaultConfig {
applicationId "com.techmate.asifa.waves"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:28.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.chibde:audiovisualizer:2.1.0'
testCompile 'junit:junit:4.12'
}
My app crashes when i run it and the following error is shown in my logcat
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.chibde.BaseVisualizer.setPlayer(BaseVisualizer.java:77)
at com.techmate.asifa.waves.MainActivity.init(MainActivity.java:81)
at com.techmate.asifa.waves.MainActivity.setPlayer(MainActivity.java:66)
at com.techmate.asifa.waves.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
Solution
Try doing this, it appeared that you were not supplying audio session ID to the visualizer.
public void init(){
Log.d(TAG, "init: init() called");
LineVisualizer lineVisualizer = findViewById(R.id.lineVisualizer);
// set custom color to the line.
lineVisualizer.setColor(ContextCompat.getColor(this, R.color.custom));
// set the line with for the visualizer between 1-10 default 1.
lineVisualizer.setStrokeWidth(1);
// Set you media player to the visualizer.
lineVisualizer.setPlayer(mediaPlayer.getAudioSessionId()); //Added session ID
}
Answered By - CrokDyle
Answer Checked By - Mildred Charles (JavaFixing Admin)