1

I can not reproduce the sound when pressing the button, any possible solution? I use SDK 30.

if(playingSongPosition !=position)
                imageView.setImageResource(R.drawable.icon_play);
            else
                imageView.setImageResource(R.drawable.icon_pause);

            constraintLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(TrimAudioActivity.this,SelectSectionActivity.class);
                    intent.putExtra(getResources().getString(R.string.music_file_name),getItem(position));
                    intent.putExtra(getResources().getString(R.string.selected_song_uri),getUri(position));
                    intent.putExtra(getResources().getString(R.string.duration),getDuration(position));
                    startActivity(intent);
                }
            });
            final View finalView = view;
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    currentTrack=position;
                    if(prevTrack==currentTrack){
                        if(mediaPlayer.isPlaying()){
                            mediaPlayer.pause();
                            mediaPlayer.seekTo(0);
                            imageView.setImageResource(R.drawable.icon_play);
                            playingSongPosition =-1;

                        }else{
                            mediaPlayer.start();
                            imageView.setImageResource(R.drawable.icon_pause);
                        }
                    }else{
                        if(prevTrack!=-1) {
                            isCallExplicit=true;
//                          Toast.makeText(ContactsRingtoneActivity.this, "prev:"+prevTrack+" current:"+currentTrack, Toast.LENGTH_SHORT).show();
                            ImageView prevImageView = (ImageView) getView(prevTrack, prevView, parent).findViewById(R.id.imageViewPlayPauseMyAudioItem);
                            prevImageView.setImageResource(R.drawable.icon_play);
                            isCallExplicit=false;
                        }
                        mediaPlayer.stop();
                        mediaPlayer.reset();
                        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer mp) {
                                mediaPlayer.start();
                                playingSongPosition =position;
                                imageView.setImageResource(R.drawable.icon_pause);
                            }
                        });
                        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                imageView.setImageResource(R.drawable.icon_play);
                                mediaPlayer.seekTo(0);
                                playingSongPosition =-1;
                            }
                        });
                        try {
                          mediaPlayer.setDataSource(TrimAudioActivity.this,getUri(position));
                            mediaPlayer.prepare();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        prevTrack=currentTrack;
                        prevView= finalView;
                    }
                }
            });
            return view;
        }
    }
    @Override
    protected void onStop() {
        super.onStop();
        mediaPlayer.stop();
        mediaPlayer.reset();
        mediaPlayer.release();
        playingSongPosition =-1;
    }
}
Michael Rovinsky
  • 6,807
  • 7
  • 15
  • 30
  • 1
    If you don't share your code, best thing we can do is guess! – jonas May 01 '21 at 15:02
  • you're right, I tried to upload it but it gave me an error, I'm new to this community, I'll upload it right away – Franco Nahuel May 01 '21 at 15:42
  • 1
    I already published it – Franco Nahuel May 01 '21 at 15:44
  • 1
    It is great that you shared your code. It would be even better if you can remove the parts that you are sure are not related to your question. As you can guess it is pretty hard to focus on the potential issues with a lot of noise in your code snippet. – Efe Budak May 01 '21 at 22:23
  • It's okay brother, thank you very much of course and sorry for my mistakes as I said I am new to this platform, the error is simply that it does not reproduce the sound when pressing the button – Franco Nahuel May 01 '21 at 23:53

0 Answers0