How do you test if a sound or video is playing?

All your problems with Audio, Video and Images.

How do you test if a sound or video is playing?

Postby emadeva » Mon Sep 05, 2011 6:10 pm

Hi Everyone,

I made an app that plays sounds but I don't want it to play any sounds if a video or some other sound file is playing.

Can you show me all the code I need to use so I can test if any sounds are being played.

I would like to place this coding inside the onReceive of my broadcast receiver.

Here are some code fragments of what I tried but it crashes the app:

It crashes on when theMediaPlayer.isPlaying() is executed:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.content.BroadcastReceiver;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.widget.Toast;
  5.  
  6. import android.app.Notification;
  7. import android.app.NotificationManager;
  8. import android.net.Uri;
  9.  
  10. import android.content.SharedPreferences;
  11. import android.preference.PreferenceManager;
  12.  
  13. import android.media.MediaPlayer;
  14.  
  15. public class AlarmReceiverQuarterBeforeTheHour extends BroadcastReceiver {
  16.  
  17.         private MediaPlayer theMediaPlayer;
  18.                 @Override
  19.         public void onReceive(Context context, Intent intent) {
  20.  
  21.                 /*
  22.                  * Get all the settings from the settings xml file.
  23.                  */
  24.                 SharedPreferences clockSettings = PreferenceManager
  25.                                 .getDefaultSharedPreferences(context);
  26.  
  27.                 /*
  28.                  * Calculate how many minutes past the hour the current time is.
  29.                  */
  30.                 CharSequence theMinutes = android.text.format.DateFormat.format("mm",
  31.                                 new java.util.Date());
  32.                 String stringMinutesNow = theMinutes.toString();
  33.                 int intMinutesNow = Integer.valueOf(stringMinutesNow);
  34.  
  35.                 /*
  36.                  * Calculate how many seconds past the hour the current time is.
  37.                  */
  38.                 CharSequence theSeconds = android.text.format.DateFormat.format("ss",
  39.                                 new java.util.Date());
  40.                 String stringSecondsNow = theSeconds.toString();
  41.                 int intSecondsNow = Integer.valueOf(stringSecondsNow);
  42.  
  43.                 /*
  44.                  * Load these settings into variables.
  45.                  */
  46.                 boolean booleanChimeOn45Past = clockSettings.getBoolean(
  47.                                 "ChimeOn45Past", false);
  48.  
  49.                 boolean booleanPhoneInUse = clockSettings.getBoolean("PhoneInUse",
  50.                                 false);
  51.                
  52.                
  53.                 if (theMediaPlayer.isPlaying()) {
  54.                         Toast.makeText(
  55.                                         context,
  56.                                         "A sound is playing",
  57.                                         Toast.LENGTH_SHORT).show();
  58.                 }
  59.                
  60.                
  61.                 if (booleanPhoneInUse == false) {
  62.  
  63.                         /*
  64.                          * Sound the chime if the setting is set to allow that.
  65.                          */
  66.                         if (booleanChimeOn45Past == true
  67.                                         && (intMinutesNow == 45 && (intSecondsNow >= 0 && intSecondsNow < 5))) {
  68.  
  69.                                 try {
  70.  
  71.                                         NotificationManager nm = (NotificationManager) context
  72.                                                         .getSystemService(Context.NOTIFICATION_SERVICE);
  73.                                         Notification notification = new Notification();
  74.                                         notification.sound = Uri
  75.                                                         .parse("android.resource://ChimeMe.BigBen/raw/quarter_before");
  76.                                         nm.notify(0, notification);
  77.  
  78.                                 } catch (Exception e) {
  79.                                         Toast.makeText(
  80.                                                         context,
  81.                                                         "There was an error somewhere, but we still received an alarm",
  82.                                                         Toast.LENGTH_SHORT).show();
  83.                                         e.printStackTrace();
  84.  
  85.                                 }
  86.                         } else {
  87.  
  88.                         }
  89.                 }
  90.         }
  91.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Thanks.

Truly,
Emad
emadeva
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Aug 24, 2011 7:37 pm

Top

Re: How do you test if a sound or video is playing?

Postby khaitt » Tue Sep 06, 2011 5:45 pm

Before create media player to play new audio, video you need tell the music playback to pause
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // TODO: these constants need to be published somewhere in the framework.
  2. Intent i = new Intent("com.android.music.musicservicecommand");
  3. i.putExtra("command", "pause");
  4. mContext.sendBroadcast(i);
  5.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
khaitt
Freshman
Freshman
 
Posts: 6
Joined: Sat Feb 26, 2011 5:32 am

Re: How do you test if a sound or video is playing?

Postby emadeva » Tue Sep 06, 2011 6:31 pm

Hi khaltt,

Thanks for the code. I will use that when I need to stop the music but I need to know if the music is actually playing so I can make sure my sound file won't play while the user is playing music or a video.

Is there a way to stop my app from crashing when theMediaPlayer.isPlaying() is executed?

Truly,
Emad
emadeva
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Aug 24, 2011 7:37 pm

Re: How do you test if a sound or video is playing?

Postby khaitt » Sun Sep 11, 2011 3:30 am

do you try with Audio Manager class? I found that method
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public boolean isMusicActive ()
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
may help you solve this problem
khaitt
Freshman
Freshman
 
Posts: 6
Joined: Sat Feb 26, 2011 5:32 am

Re: How do you test if a sound or video is playing?

Postby ramesh.kec.85 » Fri Sep 23, 2011 6:16 am

khaitt wrote:do you try with Audio Manager class? I found that method
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public boolean isMusicActive ()
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
may help you solve this problem


Thank you khaitt ,it is very helpful to me.
Thanks

Regards
--R@mesh
ramesh.kec.85
Developer
Developer
 
Posts: 27
Joined: Sat Jul 30, 2011 10:05 am
Location: Ahmedabad

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 20 guests