0

I'm programming a media player with android studio. It has 3 option: 1.play 2.stop 3.pause... the play and stop works. But when I click on pause it doesn't work and after that the stop not works too and the music play continuously! it's the code on MainActivity:

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1= (Button) findViewById(R.id.play);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this,PlayAndPause.class);
            startService(i);
            a=1;
        }
    });
    Button  b2 = (Button) findViewById(R.id.stop);
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i= new Intent(MainActivity.this,PlayAndPause.class);
            stopService(i);
        }
    });
    Button b3= (Button) findViewById(R.id.pause);
    b3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this,PlayAndPause.class);
            startService(i);
            a=2;
        }
    });
}

and it's the PlayandPause service:

public class PlayAndPause extends Service {
MediaPlayer mp;
@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    mp=MediaPlayer.create(this,R.raw.s);
    if(MainActivity.a==1)
        mp.start();
    if(MainActivity.a==2) {
        mp.pause();
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    mp.stop();
}

please help! thanks.

dashsiamak
  • 59
  • 9

1 Answers1

0

First
You must change your MediaPlayer initialization to your MainActivity or to Service.onCreate() event.

Change this line is the problem:

 mp=MediaPlayer.create(this,R.raw.s); 

To pause() a MediaPlayer yoy must use same instance that your start() so keep the reference.

Second:
As I can see, your app must pause the music if you click pause button twice, yoy check in your PlayAndPause class the a variable of MainActivity so, you must change a variable flag before calling the intent:

b3.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        a=2;   // here!!!
        Intent i = new Intent(MainActivity.this,PlayAndPause.class);
        startService(i);
    }
});

Also in your play button, that one must work because you initialize a=1 somewhere.

b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        a=1;   // here!!!
        Intent i = new Intent(MainActivity.this,PlayAndPause.class);
        startService(i);
    }
});

Also, easier and clearer, you can put an extra String to your intent, for example, after declaring the intent:

i.putExtra("ACTION", "PAUSE");

Then to get the data:

Bundle extras = getIntent().getExtras();
String action = extras.getString("ACTION")
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Uhmmm... I see... this is because you initialize `MediaPlayer` each time you call the service... and you must use same instance all times!!! put `mp=MediaPlayer.create(this,R.raw.s);` in `onCreate()` of the service or declare and initialize it in the MainActivity and `pause()` or `play()` in service – Jordi Castilla Aug 03 '15 at 12:06
  • I used put extra at first but It has error on get intent: cannot resolve method 'getintent()' – dashsiamak Aug 03 '15 at 12:07