2

I'm making an application that makes a sound when a particulate thing happens, I made an other Layout that contains a button to stop that sound but the problem is that whenever I say MediaPlayer.stop();the application on the emulator or even on my mobile says

the application has stopped

The MediaPlayer were imported from the original class,so what I'm thinking about is making a function that shuts all the sounds whether from my app or from outside of my app,any suggestion will be appreciated ;)

here is my code from the original class(which plays the sound)

public void alarm(){
    alarm = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    alarm_player = MediaPlayer.create(this, alarm);
   if (Alarm_is_on == false){
        alarm_player.setLooping(true);
        alarm_player.start();
       startActivity(new Intent(MainActivity.this, Cancel_alarm.class));
       Alarm_is_on = true;

   }


}

and this is the code from the class // where I stop/pause the sound

public void onClick(View button){

    MainActivity access = new MainActivity();
    access.alarm_player.pause();

}
Saket Mittal
  • 3,726
  • 3
  • 29
  • 49
Eyad Lotfy
  • 67
  • 8
  • 4
    **Don't do this!!!** `new MainActivity()` – codeMagic Jun 18 '15 at 16:33
  • what should I do then? – Eyad Lotfy Jun 18 '15 at 16:35
  • 2
    Not sure because I'm confused on what you've got going on. But you probably want to control the sound in a service – codeMagic Jun 18 '15 at 16:39
  • 1
    You might want to check out [this answer on playing sound in different activities](http://stackoverflow.com/questions/18871288/android-media-player-sound-continue-in-all-activities) or check out [these google results on using a service](https://www.google.com/search?q=play+sound+in+service+android&oq=play+sound+in+a+service&aqs=chrome.1.69i57j0l3.4472j0j7&sourceid=chrome&es_sm=122&ie=UTF-8) – codeMagic Jun 18 '15 at 16:46

2 Answers2

1

First please remove new MainActivity :) If you would like to start a new Activity, use Intents. http://developer.android.com/training/basics/firstapp/starting-activity.html

Second, I would create a separated service (started from a new thead) which is responsible for music player. You can communicate with this Service (NOT IntentService) with Broadcast or with Bind-ing it. In you case I think broadcast are better ways.

Here is a link with a similar project: Android background music service

Good example: http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/

I would like to kindly recommend to read it please, very very useful and can save you life: https://developer.android.com/training/run-background-service/create-service.html

Community
  • 1
  • 1
narancs
  • 5,234
  • 4
  • 41
  • 60
  • It`s my pleasure. Please mind that, you HAVE TO START your service from a NEW Thread, because the service going to belongs to the same thread which created it by default, which is UI. :) – narancs Jun 18 '15 at 16:53
1

you want to set both ringer & media to zero.So You need to use the AudioManager class

Using AudioManager you can control the volume of media player.

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);

also for MediaPlayer

public void  setVolume  (float leftVolume, float rightVolume)

for more details - http://developer.android.com/reference/android/media/AudioManager.html

Saket Mittal
  • 3,726
  • 3
  • 29
  • 49