Kill the last execution of the operation when resuming

I am developing a music application that fetches music files from the internet.

I have two actions

  • MainActivity
  • PlayerActivity

when someone clicks on an item in MainActivity

, it loads the second one PlayerActivity

and starts playing the song

the first time it works really well. the problem occurs when someone presses the back button and clicks on another item, this time the song starts playing without stopping the currently playing song,

here is my code that i tried.

MainActivity

            Intent intent = new Intent(v.getContext(), PlayerActivity.class);
            EditText albumID = (EditText) findViewById(R.id.albumID);
            String message = albumID.getText().toString();
            intent.putExtra(ALBUMID, message);
            startActivity(intent);

      

PlayerActivity

 try {

        if(player.isPlaying()) {
            player.release();
        }

        player.reset();
        player.setDataSource(urls.get(position));
        player.prepare();
        player.start();
        seekbar.setMax(player.getDuration());
    } catch (IOException e) {
        e.printStackTrace();
    }

      

NOTE: I can post the complete code if needed.

+3


source to share


1 answer


I'm guessing because you have a different player instance, and so when you stop it, it "stops" a different player than the one you want. The player is not tied to the Deed and is not destroyed when the activity is destroyed ...

You should only have one copy of your player. Maybe use singleton or static. Can you stop the player when the action is stopped? like, in it for destruction?



If this is not clear, you can try downloading more code so we can see a few more.

0


source







All Articles