Media Player doesn't play/pause on same button Click Listener in android?

Viewed 116

I am working with android MediaPlayer in which i got the music files from storage using MediaStore.Audio.Media.EXTERNAL_CONTENT_URI and displaying it in a listView which is working fine. When i click on list item i am sending the path of current position song to another activity using Intent and that activity start the service with that path using myMediaPlayer.setDataResource(path). Problem is when i go back at list view and click on another song item it should again start player activity and set Data resource in service and play that song but thats not happening in my case and activity crashes. Also when i press the stop button where song should be paused but again activity crashed happening.

Here is my mainActivity Code starting Player activity for playing song :

lvSongs.setOnItemClickListener((parent, view, position, id) -> {

            Intent intentStart = new Intent(MainActivity.this,Player.class);
            intentStart.putExtra(EXTRA_CURRENT_SONG,list.get(position));
            intentStart.putExtra(EXTRA_CURRENT_SONG_PATH,paths.get(position));
            startActivity(intentStart);
        });

Here Is my Player Class Code

public class Player extends AppCompatActivity {
    TextView tvCurrentSong;
    String title,path;
    public int checker = 0;
    Intent serviceIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        tvCurrentSong = findViewById(R.id.currentSongTitle);

        Intent currentSongIntent = getIntent();
        title = currentSongIntent.getStringExtra(MainActivity.EXTRA_CURRENT_SONG);
        path = currentSongIntent.getStringExtra(MainActivity.EXTRA_CURRENT_SONG_PATH);
        tvCurrentSong.setText(title);

        serviceIntent = new Intent(this,MusicPlayer.class);
        serviceIntent.putExtra(MainActivity.EXTRA_CURRENT_SONG_PATH,path);
        startService(serviceIntent);
    }

    public void playPause(View view) {
        if (checker == 0){
            Intent actionStopIntent =  new Intent(this,MusicPlayer.class);
            actionStopIntent.setAction("ACTION_STOP");
            startService(actionStopIntent);
            checker = 1;
        }else {
            startService(serviceIntent);
            checker = 0;
        }

    }
}

And Playing Service is here

public class MusicPlayer extends Service {
    MediaPlayer mediaPlayer = new MediaPlayer();

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            mediaPlayer.setDataSource(intent.getStringExtra(MainActivity.EXTRA_CURRENT_SONG_PATH));
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (intent.getAction() !=null && intent.getAction().equals("ACTION_STOP")) {
            Log.d("music","Intent Stop is Called");
            stopSelf();
        }
        else {
            try {
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

i am beginner and any help will be highly appreciated.

0 Answers
Related