Loading...
Logo
Processing Forum
Can Processing access the music library on the user's device - Android phone or Android tablet?

I would like to create a simple player app that goes through the music library on the user's device and then plays, for example, 2 rock songs followed by 2 rap songs followed by 2 jazz songs etc.

Is this possible using Processing?

I'd be grateful if anybody can point me to a book or some online resources that explain how to do this as none of the teaching materials I have consulted addresses this topic.

PS I am a Processing nube - but with some Flash ActionScript experience!

Replies(6)

See: https://forum.processing.org/topic/where-to-place-threads-and-other-forum-practices

Removed double post in Programming Questions as Android-related questions belong here.
Do Android devices have a concept of global "music library"? I would think it actually depend on the music player present on the device, itself perhaps depending on the brand of device, custom Android system, etc. I can be wrong, not knowing Android programming.
I dunno whether those mobile OSes got some kinda global music library either.
All I know is that they sure got a global phone contact list library!
Thus those OSes are potentially spy-able from the get-go!  
Here's an example that allows you to drill down from the /SdCard/Music directory into a band, then album, then allows you to play a file. Little bit hacky, as it was quickly developed, but it should be a start.

Copy code
  1. /* 
  2.  *
  3.  *  GrabAudioFromDevice
  4.  *  Uses MediaPlayer Class to play audio files 
  5.  *  Also showcases Directory Functionality
  6.  *  Requires VIBRATE Permsission
  7.  *
  8. */

  9. //-----------------------------------------------------------------------------------------
  10. // IMPORTS
  11. import android.content.Context;
  12. import android.app.Activity;
  13. import android.os.Bundle;
  14. import android.os.Environment;

  15. import android.app.Notification;
  16. import android.app.NotificationManager;

  17. import android.media.MediaPlayer;
  18. import android.media.MediaPlayer.OnCompletionListener;

  19. import java.io.BufferedReader;
  20. import java.io.File;
  21. import java.io.FileReader;
  22. import java.io.IOException;

  23. //-----------------------------------------------------------------------------------------
  24. // DECLARATIONS

  25. MediaPlayer mPlayer;    // Our Player, which we will reset and use over and over
  26. MyCompletionListener listener;  // Our Completion Listener, which will tell us when a song is done

  27. NotificationManager gNotificationManager;  // Vibration Mangager
  28. Notification gNotification; // Our Vibration Action

  29. //-----------------------------------------------------------------------------------------
  30. // GLOBAL VARIABLES

  31. String[] fontList;
  32. PFont androidFont;
  33. long[] gVibrate = {0, 50, 0, 50, 0, 50};  // Our Vibration Pattern
  34. int touchNumber = -1;

  35. ArrayList songs;
  36. ArrayList directories;
  37. boolean isDirectory;
  38. boolean isInitialDirectory = true;
  39. boolean isSecondaryDirectory;
  40. boolean isTertiaryDirectory;
  41. boolean isPlaying;
  42. int yDiv, yFactor;
  43. int ySpacing = 75;
  44. String currentSong;
  45. String currentFolder, initialFolder, secondaryFolder, tertiaryFolder;
  46. String path;
  47.  
  48. //-----------------------------------------------------------------------------------------

  49. void setup() {

  50.   // Screen
  51.   size(displayWidth, displayHeight, P2D);
  52.   orientation(LANDSCAPE); 
  53.   fill(255);

  54.   // Text
  55.   fontList = PFont.list();
  56.   androidFont = createFont(fontList[4], 32, true);
  57.   textFont(androidFont);

  58.   // Player + Listener
  59.   mPlayer = new MediaPlayer();
  60.   listener = new MyCompletionListener();
  61.   mPlayer.setOnCompletionListener(listener);

  62.   // Lists
  63.   songs = new ArrayList();
  64.   directories = new ArrayList();
  65.   print("about to load path");
  66.   // Files
  67.   path = "//sdcard//Music//";

  68.   currentFolder = "//sdcard//Music//";
  69.   initialFolder = "//sdcard//Music//";
  70.   File file = new File(path);
  71.   final File folder = new File(path);
  72.   
  73.   // Call Our List Function
  74.   listFilesForFolder(folder);

  75.   println("-- End Of Setup --");
  76.   
  77. }

  78. //-----------------------------------------------------------------------------------------

  79. public void listFilesForFolder(final File folder) {
  80.   
  81.     for (int i = 0; i <  folder.listFiles().length; i++) {
  82.       // Cast Each Item In The Folder As A File
  83.       File fileEntry = folder.listFiles()[i];
  84.       
  85.       // Test If It Is A Directory
  86.       if(fileEntry.isDirectory()) {
  87.         println("Directory #" + i + " is " + fileEntry.getName());
  88.         // Add To ArrayList
  89.         directories.add(fileEntry.getName());
  90.         // Update Our Scaling 
  91.         yDiv = directories.size();
  92.         yFactor = height / yDiv;
  93.       } 
  94.       
  95.       // Otherwise They're Files
  96.       else {
  97.         println("File #" + i + " is " + fileEntry.getName());
  98.         // Add To ArrayList
  99.         songs.add(fileEntry.getName());
  100.         // Update Our Scaling 
  101.         yDiv = songs.size();
  102.         yFactor = height / yDiv;
  103.       }
  104.     }
  105.     
  106. }

  107. //-----------------------------------------------------------------------------------------

  108. void draw() {
  109.   background(0);
  110.   
  111.   // Our Initial Directory Of Artists
  112.   if(isInitialDirectory) {
  113.     for(int i = 0; i <  directories.size(); i++) {
  114.       // Cast Our Item As A String
  115.       String dir = (String) directories.get(i);
  116.       text(dir , width/8 , yFactor * (i + 1));
  117.     }
  118.   }
  119.   
  120.   // Our Secondary Directory Of Albums
  121.   else if(isSecondaryDirectory) {
  122.     for(int i = 0; i <  directories.size(); i++) {
  123.       // Cast Our Item As A String
  124.       String dir = (String) directories.get(i);
  125.       text(dir , width/8 , ySpacing + (ySpacing * (i + 1)));
  126.     }
  127.   }
  128.   
  129.   // Our Tertiary Directory Of SOngs
  130.   else if(isTertiaryDirectory) {
  131.     for(int i = 0; i <  songs.size(); i++) {
  132.       // Cast Our Item As A String
  133.       String song = (String) songs.get(i);
  134.       // Colour Our Selection
  135.       if(touchNumber == i) {
  136.         fill(255, 0, 0);
  137.       }
  138.       else {
  139.         fill(255);
  140.       }
  141.       text(song , width/8 , yFactor * (i + 1));
  142.     }
  143.   }
  144.   


  145. //-----------------------------------------------------------------------------------------

  146. void mousePressed() {
  147.   println("MOUSE PRESSED");
  148.   
  149.   // Vibrate
  150.   gNotificationManager.notify(1, gNotification);
  151.   
  152.   // If We Are Still In The First Directory
  153.   if(isInitialDirectory) {
  154.     println("INITIAL");
  155.     
  156.     // Get Y Position & Match To Folder ID
  157.     int id = mouseY / yFactor;
  158.     // Cast Our Matching ArrayList Position To A String
  159.     String dir = (String) directories.get(id);
  160.     path += dir;
  161.     println("The Selected Item Is " + path);
  162.     // Store Our Location
  163.     currentFolder = path;
  164.     secondaryFolder = path;
  165.     // Cast Our String As A Folder
  166.     final File folder = new File(path);
  167.     // Clear Our ArrayList
  168.     directories.clear();
  169.     // List Files For Our Folder
  170.     listFilesForFolder(folder);
  171.     println("Loading File Structure For " + folder);
  172.     // Set Our Directories
  173.     isInitialDirectory = false;
  174.     isSecondaryDirectory = true;
  175.     
  176.   }
  177.   
  178.   // If We Are In The Second Directory
  179.   else if(isSecondaryDirectory) {
  180.     println("SECONDARY");
  181.     
  182.     // Get Y Position & Match To Folder ID
  183.     int id = (mouseY / ySpacing) - 1;
  184.     // Make Sure We Are Within Range
  185.     if(id <= directories.size()) {
  186.       // Cast Our Matching ArrayList Position To A String
  187.       String dir = (String) directories.get(id);
  188.       path += "//";
  189.       path += dir;
  190.       println("The Selected Item Is " + path);
  191.       // Store Our Location
  192.       currentFolder = path;
  193.       tertiaryFolder = path;
  194.       // Cast Our String As A Folder
  195.       final File folder = new File(path);
  196.       // Clear Our ArrayList
  197.       directories.clear();
  198.       // List Files For Our Folder
  199.       listFilesForFolder(folder);
  200.       println("Loading File Structure For " + folder);
  201.       // Set Our Directories
  202.       isInitialDirectory = false;
  203.       isSecondaryDirectory = false;
  204.       isTertiaryDirectory = true;
  205.     }
  206.     
  207.   }
  208.   
  209.   // If We Are Looking At Files
  210.   else if(isTertiaryDirectory) {
  211.     println("TERTIARY");
  212.     
  213.     // Get Y Position & Match To Folder ID
  214.     int id = mouseY / yFactor;
  215.     println("ID " + id);
  216.     touchNumber = id;
  217.     
  218.     // Cast Our Matching ArrayList Position To A String
  219.     String song = (String) songs.get(id);
  220.     
  221.     // Set Our Directories
  222.     isInitialDirectory = false;
  223.     isSecondaryDirectory = false;
  224.     isTertiaryDirectory = true;
  225.     
  226.     // Reset
  227.     if(mPlayer.isPlaying()) {
  228.       mPlayer.reset();
  229.     }
  230.     
  231.     // Play
  232.     playFile(song);
  233.     
  234.   }
  235.   
  236. }

  237. //-----------------------------------------------------------------------------------------

  238. @Override
  239. void onResume() {
  240.   super.onResume();

  241.   // Notification Manager
  242.   gNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  243.   gNotification = new Notification();
  244.   gNotification.vibrate = gVibrate;
  245.   
  246. }

  247. //---

  248. @Override
  249. protected void onPause() {
  250.   super.onPause();

  251.   // Release the Media Player
  252.   if(mPlayer != null) {
  253.     mPlayer.release();
  254.     mPlayer = null; 
  255.   }

  256. }


  257. //-----------------------------------------------------------------------------------------

  258. import android.media.MediaPlayer.OnCompletionListener;

  259. public class MyCompletionListener implements MediaPlayer.OnCompletionListener {
  260.   // Resets MediaPlayer So It Doesn't Play Over + Over
  261.  public void onCompletion(MediaPlayer mPlayer) {
  262.   
  263.    // Reset 
  264.    mPlayer.reset();
  265.    
  266.    // Increment Play Count
  267.    touchNumber++;
  268.    
  269.    // Get Path
  270.    String song = (String) songs.get(touchNumber);
  271.    
  272.    // Play
  273.    playFile(song);
  274.    
  275.   }
  276.   

  277. //-----------------------------------------------------------------------------------------

  278. void playFile(String song) {  
  279.     try {
  280.       
  281.       // Store Just The Song
  282.       currentSong = song;
  283.       println("The Selected Item Is " + song);
  284.       
  285.       // Add The Song To The Path
  286.       song = currentFolder + "//" + song;
  287.       println("And Now It Is " + song);
  288.       
  289.       // Set The Player
  290.       mPlayer.setDataSource(song);
  291.       mPlayer.setLooping(false);
  292.       mPlayer.prepare();
  293.       mPlayer.start();
  294.       
  295.     } 
  296.     catch (IOException e) {
  297.       println("#FAIL");
  298.       e.printStackTrace();
  299.     }

  300. }

  301. void stopPlaying() {
  302.   if(mPlayer != null) {
  303.     mPlayer.release();
  304.     mPlayer = null; 
  305.   }
  306. }

Let me know if you have any questions - it's a lot of code.
Thanks Jesse! Looks VERY promising - will play around with it over the next few days and see what I can do with it.
Having problems with the vibrate permission - not sure I understand why you're suggesting using that...?