logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

Music-syncing-on-Android.md (5078B)


  1. ---
  2. date: 2013-08-24
  3. title: Custom Music Syncing on Android
  4. layout: post
  5. tags: [mobile]
  6. ---
  7. I have an HTC One, with CyanogenMod installed. I usually use Spotify, but I've been wanting to move away from it for a while.
  8. The biggest thing keeping me there was the ease of syncing up with my phone - I added music on my PC and it just showed up
  9. on my phone.
  10. So, I finally decided to make it work on my phone without Spotify. You might have success if you aren't using CyanogenMod,
  11. but you definitely need to be rooted and you need to access a root shell on your phone. I was using `adb shell` to start with,
  12. but it has poor terminal emulation. Instead, I ended up installing an SSH daemon on the phone and just using that. Easier to
  13. use vim in such an enviornment.
  14. The end result is that a cronjob kicks off each hour on my phone and runs a script that uses rsync to sync up my phone's music
  15. with my desktop's music. That's another thing - a prerequisite of this working is that you have to expose your music to the
  16. outside world on an SSH server somewhere.
  17. I'll tell you how I got it working, then you can see if it works for you. It might take some effort on your part to tweak
  18. these instructions to fit your requirements.
  19. ## Sanity checks
  20. Get into your phone's shell and make sure you have basic things installed. You'll need to make sure you have:
  21. * bash
  22. * cron
  23. * ssh
  24. * rsync
  25. If you don't have them, you can probably get them by installing busybox.
  26. ## Setting up SSH
  27. We need to generate a key. I tried using ssh-keygen before, but it had problems with rsync on Android. Instead, we use
  28. dropbearkey. Generate your key with `dropbearkey -t rsa -f /data/.ssh/id_rsa`. You'll see the public key echoed to stdout.
  29. It's not saved anywhere for you, so grab it out of your shell and put it somewhere - namely, in the authorized_keys file
  30. on the SSH server you plan to pull music from.
  31. At this point, you can probably SSH into the server you want to pull from. Run `ssh -i /data/.ssh/id_rsa <your server here>`
  32. to double check. Note that this isn't just for fun - you need to do this to get your server into known_hosts, so we can
  33. non-interactively access it.
  34. ## Making Android more sane
  35. Now that this is working, we need to clean up a little before cron will run right. Android is only a "Linux" system in the
  36. sense that `uname` outputs "Linux". It grossly ignores the FHS and you need to fix it a little. Figure out how to do a
  37. nice init.d script on your phone. For my CyanogenMod install, I can add scripts to `/data/local/userlocal.d/` and they'll
  38. be run at boot. Here's my little script for making Android a little more sane:
  39. ```bash
  40. #!/system/bin/sh
  41. # Making /system rw isn't strictly needed
  42. mount -o remount,rw /system
  43. mount -o remount,rw /
  44. ln -s /data/var /var
  45. ln -s /system/bin /bin
  46. ln -s /data/.ssh /.ssh
  47. crond
  48. ```
  49. ## Update script and initial import
  50. The following is the script we'll use to update your phone's music library.
  51. ```bash
  52. #!/system/xbin/bash
  53. # Syncs music between a remote computer and this phone
  54. RHOST=<remote hostname>
  55. EHOST=<fallback, I use this for connecting from outside my LAN>
  56. RPORT=22
  57. RUSER=<username>
  58. ID=/data/.ssh/id_rsa
  59. RPATH=/path/to/your/remote/music
  60. # Omit the final directory. On my setup, this goes to /sdcard/Music, and my remote is /home/sircmpwn/Music
  61. LPATH=/sdcard
  62. echo $(date) >> /var/log/update-music.log
  63. rsync -ruvL --delete --rsh="ssh -p $RPORT -i $ID" $RUSER@$RHOST:$RPATH $LPATH >> /var/log/update-music-rsync-so.log 2>&1
  64. if [[ $? != 0 ]]; then
  65. rsync -ruvL --delete --rsh="ssh -p $RPORT -i $ID" $RUSER@$EHOST:$RPATH $LPATH >> /var/log/update-music-rsync-so.log 2>&1
  66. fi
  67. ```
  68. Save this script to `/data/updateMusic`, make it executable with `chmod +x /data/updateMusic`, then run the initial import
  69. with `/data/updateMusic`. After a while, you'll have all your computer's music on your phone. Now, we just need to make it
  70. update automatically.
  71. Note: I set up a couple of logs for you. `/var/log/update-music.log` has the timestamp of every time it did an update. Also,
  72. `/var/log/update-music-rsync-so.log` has the output of rsync from each run.
  73. ## Cron
  74. Finally, we need to set up a cronjob. If you followed the instructions so far (and if you're lucky), you should have everything
  75. ready for cron. The biggest pain in my ass was getting cron to coorperate, but the init script earlier should take care of
  76. that. Run `crontab -e` and write your crontab:
  77. 0 * * * * /data/updateMusic
  78. Nice and simple. Your phone will now sync up your music every hour, on the hour, with your home computer. Here are some
  79. possible points for improvement:
  80. * Check wlan0 and only sync if it's up
  81. * Log cron somewhere
  82. * Alter the update script to do a little bit better about the "fallback"
  83. * Sync more than just music
  84. After all of this, I now have a nice setup that syncs music to my phone so I can listen to it with Apollo. I might switch
  85. away from Apollo, though, it's pretty buggy. [Let me know](mailto:sir@cmpwn.com) if you can suggest an alternative music
  86. player, or if you get stuck working through this procedure yourself.