Monday 13 January 2014

Check if music is already playing on Android from within an Activity

Problem:

How do I check if music is already playing on an Android device from within an Activity?

Solution:

AudioManager aMan = (AudioManager)this.getSystemService(
                                         Context.AUDIO_SERVICE);
if(aMan.isMusicActive()) {
  // do stuff here if music is playing
}

Notes:

This check can be useful if your app needs to play music. This is because there are instances where attempting to play music using MediaPlayer while music is already playing will corrupt an audio stream on an Android device.

References:

Tuesday 7 January 2014

Disable Mac start-up chime in OS X 10.9 Mavericks ("Band-Aid" workaround only, for now)

Problem:

I would like to disable the start-up chime my OS X Mavericks-based Mac makes so that restarting the computer will not be so loud.

Notes:

  • The solutions below were tested on OS X 10.9 Mavericks and may work on other versions, but there is no guarantee.
  • This how-to was written as a reminder for myself in case this needs to be replicated or undone later. It is not a full solution and still a bit advanced for the average OS X user to follow, which makes it unacceptable to post as a "solution" (in my opinion).
  • You're welcomed to follow the solutions below, but use the more permanent one at your own risk.

Solution 1 (temporary):

Turn the volume of your computer down before shutdown or restart. The start-up chime should match your system's volume.

Note that muting seems to have mixed reported results in forums and other online posts, where some systems will chime at the same volume as the system volume, even when muted (resulting in lots of surprise chimes), yet others report that muting works.

Solution 2 (advanced workaround, but a bit more permanent):

This solution goes through the steps to write a script that turns the system volume to 0, and rigs it to run upon the user selecting to shutdown or restart the machine via the apple menu.

Warnings:

  • Make sure you are comfortable with Terminal, shell scripts, file permissions, and reading manual pages (if you get stuck). If you are not comfortable using Terminal (or are prone to making typos), you should probably not try this. Attempt at your own risk.
  • Every time you start up OS X, the volume will be set to 0 (which may not be a bad thing if you're already trying to avoid surprises by turning the chime sound off to begin with)

This is a modification of the solution found here, but modified in a way that should work on more OS X setups. The modified steps are posted below as a reminder in case the link ever goes down.

Steps:

  1. Run the Terminal app with an administrator account
  2. Create a script for muting (replacing "/path/to/" with a valid path -- if you're not sure what this means, do not proceed with the rest of the steps)
    sudo nano /path/to/volume-off.sh
    
  3. Write the volume-off script (or copy and paste the following into it):
    #!/bin/bash
    
    osascript -e "set Volume 0"
    
  4. Make file executable with following command (replacing "/path/to/" with where the script is saved):
    sudo chmod u+x /path/to/volume-off.sh
    
  5. Check that we can hook this to the OS X logout (see if any hooks exist - only one or none can exist, not more). In Terminal, use the command:
    sudo defaults read com.apple.loginwindow LogoutHook
    

    You should end up with something similar to "The domain/default pair of (com.apple.loginwindow, LogoutHook) does not exist".

  6. Add hook to run script at logout, replacing "/path/to/" with where the script is saved:
    sudo defaults write com.apple.loginwindow LogoutHook /path/to/volume-off.sh
    

To Undo Solution 2:

  1. Check that the logout hook exists in Terminal with the following command:
    sudo defaults read com.apple.loginwindow LogoutHook
    

    You should end up with something similar to "/path/to/volume-off.sh" with "/path/to/" being where your script is saved.

  2. Delete the logout hook in Terminal with the following command:
    sudo defaults delete com.apple.loginwindow LogoutHook
    

Notes on solution 2:

  • Just like in the original solution, the volume-off.sh script is also saved in /Library/Scripts/
  • Make sure the permissions of the script is set so that only the owner can write to the file, since the script is run as sudo (and would be an obvious security risk if it can be re-written by just anyone).
  • The script must be owned by root.
  • Again, this will turn the volume to 0 upon logout rather than simply mute. This is because many people online seem to report mixed success with muting.
  • The commands for setting both a login and a logout hook, as well as removing them are as follows:
    • sudo defaults write com.apple.loginwindow LoginHook /path/to/your-login-script.sh
    • sudo defaults write com.apple.loginwindow LogoutHook /path/to/your-logout-script.sh
    • sudo defaults delete com.apple.loginwindow LoginHook
    • sudo defaults delete com.apple.loginwindow LogoutHook
  • The commands for checking if a hook exists for login or logout are as follows:
    • sudo defaults read com.apple.loginwindow LoginHook
    • sudo defaults read com.apple.loginwindow LogoutHook

Very brief discussion on problems with other solutions found online:

The following will only make sense to anyone who has Googled how to disable the start-up Mac chime and read through the various proposed solutions. It briefly outlines why these workarounds weren't posted here. You can skip this section if you'd like.

nvram SystemAudioVolume method:

  1. too dangerous for most users (you can easily accidentally turn your computer into an expensive paperweight with the nvram command)
  2. many mixed results and uninformed posts online on why that solution used to work (read: everyone seems to be guessing. If you don't want to be one of those guessers, you can read up on nvram in the slightly out-of-date book "Mac OS X for Unix Geeks (Leopard)" in Google Books as that section appears to be viewable for free, at least as of January 2014)
  3. the solution doesn't work on all OS X and Mac combinations
  4. the solution does NOT work in Mavericks (tested) as the system will change the SystemAudioVolume value automatically

rc startup/shutdown script method:

  1. does not work in Mavericks as start-up has been moved to launchd instead (see Apple developer documentation on Launch Daemons and Agents
  2. any daemon-supporting mechanism may change in future versions of OS X, making this solution possibly unreliable (and worse, it will leave potentially unwanted files dangling deep within the system)
  3. this solution is way too complicated to post for the average user

existing software:

  1. many don't work on all versions of OS X (mixed results reported in forums, etc.)
  2. all software Googled do not describe exactly what system-wide changes are being made, making it hard to determine how permanent they are, as well as how well do they handle OS upgrades
  3. Mavericks seems to have broken a lot of the existing software, so it's hard to recommend a good one

the solution 2 in this blog post:

  1. it's not possible to do if you are not the system administrator
  2. it's not possible to do if you or your system administrator has already hooked something to com.apple.loginwindow LogoutHook
  3. it's possible that it won't work if you shut down your computer using a different mechanism than the usual Apple UI ways
  4. it's still inaccessible to most users
  5. if permissions are not set correctly, you could potentially create a security hole

an official Apple "turn off start-up sound" preferences item:

  1. does not exist/was eliminated
  2. if you're from Apple, perhaps you could help out with this? =)

References:

Wednesday 1 January 2014

First one-line post of 2014! Thanks for over 50,000 views!

Happy New Year and Thanks for over 50,000 views (I know it's not many views, but it still feels great that some of you have found the posts useful)! =D