objectuser.blog(brain)

objectuser.getBlog().contains(anything)

  • a place for thoughts, explanations, etc.

    this is just a place to write things out so I can make sure i understand them, keep track of them, and to use them again as links, etc.
  • Subscribe

Android Music Sync

Posted by objectuser on July 25, 2010

I’ve recently switched from an iPhone to a Samsung Captivate phone (which runs Android).  It’s been very interesting to note the trade-offs between the two devices.  The iPhone just works; a very integrated experience.  The Android phone is powerful, far more powerful than the iPhone.

One huge gap in the Android experience for me was iTunes.  There is no standard Android equivalent, but there are quite a few third party solutions (one of which I’ll likely adopt).  In the mean time, while I’m doing research for the best solution, I wanted to copy over my music to my device.

Unfortunately, this is a bit time consuming.  Selecting stuff and dragging it over to copy is a pain.  I started doing this and quickly gave up.

Fortunately, I know how to write shell scripts (I use a Mac) and am, admittedly, a geek.  These scripts are a very primitive way to maintain your device library.  If you aren’t comfortable with a command line and shell scripting, and don’t know exactly what you’re doing, I recommend you do not try this.  Caveat scriptor.

The first one builds a file that drives the synchronization.

function find_music {
  ls -l "${1}"/*.mp3 1>/dev/null 2>/dev/null
  file_count=$?
  if [ "${file_count}" == "0" ]; then
    echo "x ${1#./}"
  fi
  for file in "${1}"/*; do
    if [ -d "${file}" ]; then
      find_music "${file}"
    fi
  done
}

find_music .

The output looks like this:

x 16 Volt/Letdowncrush
x 16 Volt/Skin
x 16 Volt/Supercoolnothing
x 16 Volt/Wisdom
x AC_DC/Back In Black
x AC_DC/High Voltage
x AC_DC/Highway To Hell
x Alice In Chains/Alice In Chains
x Alice In Chains/Dirt
x Alice In Chains/Facelift
x Alice In Chains/Jar Of Flies
x Alien Ant Farm/ANThology
x Alien Ant Farm/truANT
x Anthrax/Sound Of White Noise
...

The x behaves like iTunes when you check a song. Next to the x is a directory name. If you want something to be copied over, leave the x. Otherwise, replace the x with an o; the latter is not copied over.

The following script reads that file and copies music as specified by the x or o.

device_music="/Volumes/CAPTIVATE/Music"

while read input_line; do
  checked="${input_line%% *}"
  directory="${input_line##[xo] }"

  if [ "$checked" == "x" ]; then
    echo "syncing $directory to $device_music"
    mkdir -p "${device_music}/${directory}"
    cp -nR "$directory" "${device_music}/${directory}"
  elif [ -d "${device_music}/${directory}" ]; then
    echo "removing ${device_music}/${directory}"
    rm -r "${device_music}/${directory}"
  else
    echo "skipping $directory"
  fi
done

For each line, if it has an x, then the files are copied over.  If the files are already there, they are not replaced.  So after the first run, this runs very fast. Also, if at first you specified x and then changed it to o, the script will remove those files from the device.

In summary, here are the steps to using the scripts.

First, save the first script to a file. I call mine find-music.sh and put it in the base directory that contains all of my music. You may want to change it if your music isn’t in MP3 format (use whatever extensions make sense for you).

Open a shell and go to the base directory of your music collection.

%sh find-music.sh > AndroidMusic.txt

Next, edit that file and, for each directory you don’t want copied, replace x with o.

Now save the second script to a file.  I call mine sync.sh and it’s in the same directory with find-music.sh.  Change the value of device_music to wherever you want your music copied on your device.  Then run the script with the previously created AndroidMusic.txt:

%sh sync.sh < AndroidMusic.txt

The script writes to the console for each directory it’s considering, whether it’s copying it over, skipping it, or removing the copy from the device.

This is just a stop-gap solution for me, so I  don’t plan on enhancing it.  But I thought it might be useful to someone else that just wants to copy music to their device and is equally geeky.

One Response to “Android Music Sync”

  1. […] the scripts yourself for your systems If you're not deterred, I posted the source in a blog post here (mods, if it's not cool linking to my blog, sorry about that, feel free to delete or whatever). […]

Leave a comment