0

Here is my code. I'm very new to Java and I know that this question is already been posted but still I didn't get the expected outpost so I had to post.

public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final MediaPlayer policeSound = MediaPlayer.create(this, R.raw.police);
        Button policeSounds = (Button) this.findViewById(R.id.police);
        policeSounds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (policeSound.isPlaying()){
                    policeSound.stop();
                }
                else {
                    policeSound.start();
                }
            }
        });
    }
}

I tried adding onBackPressed() code to this but it couldn't detect the 'policeSound' as it was detected in the previous method!

And someone please even teach me how to use @Override annotations!

Robert
  • 7,394
  • 40
  • 45
  • 64

2 Answers2

0

To detect the 'policeSound' in other methods you need to make it be a field of a class:

private MediaPlayer policeSound;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    policeSound = MediaPlayer.create(this, R.raw.police);
    Button policeSounds = (Button) this.findViewById(R.id.police);
    policeSounds.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (policeSound.isPlaying()) {
                        policeSound.stop();
                    } else {
                        policeSound.start();
                    }
                }
            }
    );
}
Oleg Sokolov
  • 1,134
  • 1
  • 12
  • 19
  • Thanks a lot Oleh for the reply! – Vipul Priyadarshi Sep 05 '18 at 17:11
  • @VipulPriyadarshi if you found my answer useful, please vote it – Oleg Sokolov Sep 05 '18 at 17:13
  • As told by you, I made it as a field of a class and it worked perfectly. Pressing the back button pauses the music and upon reopening the app if pressed again, it plays back. But that's not the case with home button or recent button, upon pressing them the music pauses but if I open the app again and press the play button, it doesn't play until the app is closed or removed from recents and launched again. FYI, I'm using onPause() and onStop() methods. – Vipul Priyadarshi Sep 05 '18 at 17:38
  • @VipulPriyadarshi please, create new question with new case and publish there your new code – Oleg Sokolov Sep 05 '18 at 19:09
0

In your codes policeSound is a local variable which is be seen only in Oncreate() method,as Oleh Sokolov said, you should declare policeSound as a field of class.

About @Override , you could see this good explanation

and in android studio , when you press ctrl + o in java class file, you can override superclass method, and IDE will add @Override automatically for you.

navylover
  • 12,383
  • 5
  • 28
  • 41