Cleveroad / WaveInApp

Live Audio Equalizer with wave effect
https://www.cleveroad.com
MIT License
1.79k stars 320 forks source link

Try to set MediaPlayer - error `Attempt to invoke virtual method` #35

Closed salvonos closed 7 years ago

salvonos commented 7 years ago

I am trying to connect the visualiser with a music player from MainActivity.java. The visualiser is set in a fragment and all works well when I call it showing it in the activity however, overtime I try to add the MusicPlayer i get the error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{example.it/example.it.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.MediaPlayer.getCurrentPosition()' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
                      at android.app.ActivityThread.access$1100(ActivityThread.java:221)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:158)
                      at android.app.ActivityThread.main(ActivityThread.java:7224)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.MediaPlayer.getCurrentPosition()' on a null object reference
                      at example.it.AudioVisualizationFragment.onViewCreated(AudioVisualizationFragment.java:60)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1127)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
                      at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                      at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:604)
                      at example.it.MainActivity.onStart(MainActivity.java:1210)

I have called the music player from MusicService.java

AudioVisualizationFragment.java

audioVisualization = (AudioVisualization) view;
        audioVisualization.linkTo(DbmHandler.Factory.newVisualizerHandler(getContext(), MusicService.mPlayer));

MainActivity.java

private void openFragment() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.audio_player_lyt, AudioVisualizationFragment.newInstance())
                .commit();

    }

How could I solve this error?

Iojjj commented 7 years ago

According to the logs, your MediaPlayer instance equals null. Check your AudioVisualizationFragment on line 60. This crash doesn't related to library.

P.S. Do not use static variables for holding a MediaPlayer instance.

salvonos commented 7 years ago

Thank you for your quick replay. I understand that the value is null but If I don't use static variables how could I send the audio from mediaPlayer to the fragment?

Iojjj commented 7 years ago

@salvonos. MediaPlayer is not required for visualizer handler. You can pass audio session ID instead. There are several ways to communicate with service:

salvonos commented 7 years ago

Thanks I will try this. I have also tried to use "0" as session ID which should corresponds to the audio output mix? However, not animation is received

salvonos commented 7 years ago

I have tried almost everything but cannot work it out. I have tried to start the media player direct into the fragment but the visualiser is not animating:

mediaPlayer = MediaPlayer.create(getContext(), R.raw.sound);
        mediaPlayer.start();
        audioVisualization.linkTo(DbmHandler.Factory.newVisualizerHandler(context, mediaPlayer));
Iojjj commented 7 years ago

Have you checked Troubleshooting section in Readme? It's possible that visualizer will not work on some devices. What device are you using?

salvonos commented 7 years ago

I am using Samsung Galaxy S5. I can see the visualiser but not animating