gauravk95 / audio-visualizer-android

🎵 [Android Library] A light-weight and easy-to-use Audio Visualizer for Android.
Apache License 2.0
857 stars 132 forks source link

java.lang.ClassCastException: com.gauravk.audiovisualizer.visualizer.BlastVisualizer #28

Open Doctor32th opened 2 years ago

Doctor32th commented 2 years ago

When I run my music player I receive this error: visualizer_error visualizer_declaration

Doctor32th commented 2 years ago

//I LEAVE MY CODE HERE

public class PlayerActivity extends AppCompatActivity {

Button btnPlay, btnNext, btnPrevious, btnFastForward, btnFastBackward;
TextView txtSongName, txtSongStart, txtSongEnd;
SeekBar seekMusicBar;
BarVisualizer barVisualizer;
ImageView imageView;

String songName;

public static final String EXTRA_NAME = "song_name";
static MediaPlayer mediaPlayer;

int position;
ArrayList<File> mySongs;
Thread updateSeekBar;

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

    btnPrevious = findViewById(R.id.btnPrevious);
    btnNext = findViewById(R.id.btnNext);
    btnPlay = findViewById(R.id.btnPlay);
    btnFastForward = findViewById(R.id.btnFastForward);
    btnFastBackward = findViewById(R.id.btnFastBackward);

    txtSongName = findViewById(R.id.txtSong);
    txtSongStart = findViewById(R.id.txtSongStart);
    txtSongEnd = findViewById(R.id.txtSongEnd);

    seekMusicBar = findViewById(R.id.seekBar);
    barVisualizer = findViewById(R.id.green_wave);

    imageView = findViewById(R.id.imgView);

    if (mediaPlayer != null){
        mediaPlayer.start();
        mediaPlayer.release();
    }

    Intent intent = getIntent();

    Bundle bundle = intent.getExtras();

    mySongs = (ArrayList)bundle.getParcelableArrayList("songs");

    String sName = intent.getStringExtra("songname");

    position = bundle.getInt("pos", 0);

    txtSongName.setSelected(true);

    Uri uri = Uri.parse(mySongs.get(position).toString());
    songName = mySongs.get(position).getName();
    txtSongName.setText(songName);

    mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
    mediaPlayer.start();

    updateSeekBar = new Thread(){
        @Override
        public void run() {
            int totalDuration = mediaPlayer.getDuration();
            int currentPosition = 0;

            while(currentPosition < totalDuration) {

                try {
                    sleep(500);
                    currentPosition = mediaPlayer.getCurrentPosition();
                    seekMusicBar.setProgress(currentPosition);

                } catch (InterruptedException | IllegalStateException e) {
                    e.printStackTrace();
                }
            }

        }
    };

    seekMusicBar.setMax(mediaPlayer.getDuration());

    updateSeekBar.start();

    seekMusicBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.teal_200), PorterDuff.Mode.MULTIPLY);
    seekMusicBar.getThumb().setColorFilter(getResources().getColor(R.color.teal_200), PorterDuff.Mode.SRC_IN);

    seekMusicBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            mediaPlayer.seekTo(seekBar.getProgress());
        }
    });

    String endTime = createTime(mediaPlayer.getDuration());
    txtSongEnd.setText(endTime);

    final Handler handler = new Handler();
    final int delay = 1000;
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            String currentTime = createTime(mediaPlayer.getCurrentPosition());
            txtSongStart.setText(currentTime);
            handler.postDelayed(this, delay);
        }
    }, delay);

    btnPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(mediaPlayer.isPlaying()){
                btnPlay.setBackgroundResource(R.drawable.ic_play);
                mediaPlayer.pause();
            } else {

                btnPlay.setBackgroundResource(R.drawable.ic_pause);
                mediaPlayer.start();

                TranslateAnimation moveAnim = new TranslateAnimation(-25, 25,-25, 25);
                moveAnim.setInterpolator(new AccelerateInterpolator());
                moveAnim.setDuration(600);
                moveAnim.setFillEnabled(true);
                moveAnim.setFillAfter(true);
                moveAnim.setRepeatMode(Animation.REVERSE);
                moveAnim.setRepeatCount(1);

                imageView.startAnimation(moveAnim);

            }
        }
    });

    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            btnNext.performClick();
        }
    });

    int audioSessionId = mediaPlayer.getAudioSessionId();
    if (audioSessionId != -1) {
        barVisualizer.setAudioSessionId(audioSessionId);
    }

    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            mediaPlayer.stop();
            mediaPlayer.release();

            position = ((position + 1)%mySongs.size());

            Uri uri = Uri.parse(mySongs.get(position).toString());

            mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
            songName = mySongs.get(position).getName();
            txtSongName.setText(songName);
            mediaPlayer.start();

            startAnimation(imageView, 360f);
        }
    });

    btnPrevious.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            mediaPlayer.stop();
            mediaPlayer.release();

            position = ((position - 1) < 0)?(mySongs.size() -1):position-1;
            Uri uri = Uri.parse(mySongs.get(position).toString());

            mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
            songName = mySongs.get(position).getName();
            txtSongName.setText(songName);
            mediaPlayer.start();

            startAnimation(imageView, -360f);
        }
    });

    btnFastForward.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mediaPlayer.isPlaying()){
                mediaPlayer.seekTo(mediaPlayer.getCurrentPosition() + 10000);
            }
        }
    });

    btnFastBackward.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mediaPlayer.isPlaying()){
                mediaPlayer.seekTo(mediaPlayer.getCurrentPosition() - 10000);
            }
        }
    });

}

public void startAnimation(View view, Float degree){

    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, degree);

    objectAnimator.setDuration(1000);

    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playTogether(objectAnimator);
    animatorSet.start();
}

public String createTime(int duration){
    String time = "";
    int min = duration/1000/60;
    int sec = duration/1000%60;

    time = time + min + ":";

    if (sec < 10) {
        time+="0";
    }

    time+=sec;
    return time;
}

}

AwesomeDracula commented 2 years ago

In your .xml file, using instead of

Doctor32th commented 2 years ago

Thank you! It works!!