lundi 22 décembre 2014

Change Button on Event that comes from a class

I got following problem to fight with. I got a class SoundManager in which i create a SoundManger and let it play!


This is the function from the class where the sound plays:



public void mediaPlayerStart() {
Uri alert = getAlarmUri();
Boolean mute = mute();

if(mMediaPlayer == null && !mute) {
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.setDataSource(context, alert);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();

} catch (Exception e) {
e.printStackTrace();
}
}
}


I got a Button with an Image Background in my Main Activity! Now I want, that the Button changes to another background while the sound is playing.


But first I could not change the button from MainActivity in the SoundManager class file and second I can't pass from the SoundManager to the MainActivity, whether the sound is playing or not?


This is the button in my main:



protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button stopButton = (Button)findViewById(R.id.StopButton);

stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SoundManager.stop();
stopButton.setBackgroundResource(R.drawable.normal);
}
});
}


The sound is created in a class called GcmIntentService. There I build a notification and play the sound. Heres is the excerpt with the function:



private void sendNotification(String msg) {
//is media player playing a sound?

mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);


long when = System.currentTimeMillis();
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Boolean notifyAlways = preferences.getBoolean("ring24h",true);
if(notifyAlways){
System.out.println("Immer");

Handler handler = new Handler(getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
SoundManager soundManager = new SoundManager(getApplicationContext());
soundManager.acquire();
soundManager.mediaPlayerStart();
}
});


So the sound is created in the GcmIntentService and in my main I stop it.


So just to be clear: I want another image background on the button when the sound is playing. And the standard normal background when the sound is not playing.


I'm happy for any suggestion that would help me out.


Thanky ahead. And please, even if it's maybe a stupid question, please don't downvote it directly without answering it. That would be great.


Aucun commentaire:

Enregistrer un commentaire