Picking random music on sd card

All your problems with Audio, Video and Images.

Picking random music on sd card

Postby brybam » Mon Jun 14, 2010 11:13 am

Hi all,

I'm trying to something very very simple and i feel as tho I may be overthinking it. Right now I'm simply trying to pick a random song from the users sd card and have it automaticly start playing when they go to a specific activity screen. and if possible make it pick another random song after 60 seconds. I'm getting all sorts of errors. I figured out how to just link it to a mp3 I supplied in the app. But i need it to just randomly find any mp3 file on the users sd card or audio file in their media player doesnt matter. Anyone mind lending a hand?

here's some code I started with in a Music.java file

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static void play(Context context, int resource) {
  2.       stop(context);
  3.  
  4.  
  5.       if (Prefs.getMusic(context)) {
  6.          mp = MediaPlayer.create(context, resource);
  7.          mp.start();
  8.       }
  9.    }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4



and i have this on the activity page i want the song to start on
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. protected void onResume() {
  3.    super.onResume();
  4.    Music.play(this);
  5. }
  6.  
  7. @Override
  8. protected void onPause() {
  9.    super.onPause();
  10.    Music.stop(this);
  11. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
brybam
Developer
Developer
 
Posts: 27
Joined: Fri May 21, 2010 1:03 am

Top

Re: Picking random music on sd card

Postby brybam » Tue Jun 15, 2010 6:37 am

Anyone have any ideas? at least on how to make the source be a random song file on the users SD card?
brybam
Developer
Developer
 
Posts: 27
Joined: Fri May 21, 2010 1:03 am

Re: Picking random music on sd card

Postby fbarnard » Tue Jun 15, 2010 12:42 pm

Hi

you can use the media store to get all the audio and then just do a random on index

Something like this excuse my JAVA its a bit rusty
Code: Select all
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; 
   
String[] projection = { 
     MediaStore.Audio.Media._ID, 
     MediaStore.Audio.Media.ARTIST, 
     MediaStore.Audio.Media.TITLE, 
     MediaStore.Audio.Media.DATA, 
     MediaStore.Audio.Media.DISPLAY_NAME, 
     MediaStore.Audio.Media.DURATION 
}; 
   
cursor = this.managedQuery( 
     MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
     projection, 
     selection, 
     null, 
     null);

int rndIndex = new java.util.Random(cursor.getCount());
cursor.moveToPosition(rndIndex );

log.d("something",cursor.getString(0) + "||" + cursor.getString(1) + "||" +   cursor.getString(2) + "||" +   cursor.getString(3) + "||" +  cursor.getString(4) + "||" +  cursor.getString(5));


fbarnard
Developer
Developer
 
Posts: 44
Joined: Tue Jun 15, 2010 12:31 pm

Re: Picking random music on sd card

Postby JKumar » Tue Jun 15, 2010 1:31 pm

Hi,

As suggested by fbarnard , your application should have the full content and then only you can access them randomly using java random method.
Hope the suggestion helps you
Keep posting .It makes us better to understand Android
User avatar
JKumar
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Thu May 13, 2010 8:17 am
Location: INDIA

Re: Picking random music on sd card

Postby brybam » Sun Jun 20, 2010 11:27 pm

Alright, well here's what im trying now.

However, on line 58
cursor.moveToPosition(rndIndex );

I get this error:
The method moveToPosition(int) in the type Cursor is not applicable for the arguments (Random)

so I tried changing the random to int..

But then i get an error on line 57
new java.util.Random(cursor.getCount());

and the error is:
Type mismatch: cannot convert from Random to int



again just to reiterate so there's no confusion...
I'm simply trying to make an activity that when started loads random music from the users music library and plays them on 1 minute intervals. Sounds easy enough right?




Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.mypackage.name;
  2.  
  3.  
  4. import java.io.File;
  5. import java.io.FilenameFilter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Random;
  10. import android.app.Activity;
  11. import android.app.AlertDialog;
  12. import android.app.ListActivity;
  13. import android.content.Context;
  14. import android.content.DialogInterface;
  15. import android.content.Intent;
  16. import android.media.MediaPlayer;
  17. import android.os.Bundle;
  18. import android.database.Cursor;
  19. import android.provider.MediaStore;
  20. import android.util.Log;
  21. import android.view.Menu;
  22. import android.view.MenuInflater;
  23. import android.view.MenuItem;
  24. import android.view.View;
  25. import android.view.View.OnClickListener;
  26. import android.widget.ArrayAdapter;
  27. import android.widget.ListView;
  28.  
  29. public class Randommusic extends Activity {
  30.         MediaPlayer mp = new MediaPlayer();
  31.        
  32.         String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
  33.        
  34.         String[] projection = {  
  35.                      MediaStore.Audio.Media._ID,  
  36.                      MediaStore.Audio.Media.ARTIST,  
  37.                      MediaStore.Audio.Media.TITLE,  
  38.                      MediaStore.Audio.Media.DATA,  
  39.                      MediaStore.Audio.Media.DISPLAY_NAME,  
  40.                      MediaStore.Audio.Media.DURATION  
  41.                 };
  42.        
  43.         private Cursor cursor;
  44.                    @Override
  45.                    public void onCreate(Bundle savedInstanceState) {
  46.                       super.onCreate(savedInstanceState);
  47.                       setContentView(R.layout.randommusic);
  48.                      
  49.                      
  50.                       cursor = this.managedQuery(  
  51.                                      MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,  
  52.                                      projection,  
  53.                                      selection,  
  54.                                      null,  
  55.                                      null);
  56.  
  57.                       Random rndIndex = new java.util.Random(cursor.getCount());
  58.                                 cursor.moveToPosition(rndIndex );
  59.  
  60.                                 Log.d("something",cursor.getString(0) + "||" + cursor.getString(1) + "||" +   cursor.getString(2) + "||" +   cursor.getString(3) + "||" +  cursor.getString(4) + "||" +  cursor.getString(5));
  61.  
  62.  }
  63. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
brybam
Developer
Developer
 
Posts: 27
Joined: Fri May 21, 2010 1:03 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 14 guests