MediaPlayer.setDataSource(url) not working

All your problems with Audio, Video and Images.

MediaPlayer.setDataSource(url) not working

Postby Danneman » Sun Mar 29, 2009 9:01 pm

MediaPlayer.setDataSource(url) is not working in my app.

Im puttin the file "hello.mp3" in the assets-folder, and Im using the following code:
Code: Select all
        String soundUrl = "file:///android_asset/hello.mp3";
        mp = new MediaPlayer();
        try {
            mp.setDataSource(soundUrl);
            mp.prepare();
            mp.start();
        }
        catch (IOException e) {}
        catch (IllegalArgumentException e) {}
        catch (IllegalStateException e) {}


This code works when I put the "hello.mp3" in the res/raw-folder:
Code: Select all
        mp = new MediaPlayer();
        mp = MediaPlayer.create(getBaseContext(), R.raw.hello);
        mp.start();


Problem with the last code is that I need to load the sound-files dynamically, and apparently you cant create a string or a uri with the correct sound-file at the end and insert that as the second parameter in the MP.create()-function.

Pseudocode - NOT WORKING
Code: Select all
String mySound = "hello";
Uri myUri = "R.raw." + mySound;
...
mp = MediaPlayer.create(getBaseContext(), myUri);


Any ideas?
Danneman
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Mar 26, 2009 12:42 pm

Top

Postby rockhard » Fri May 01, 2009 3:14 pm

I am having the same problem. Does anyone know a way around this?
rockhard
Freshman
Freshman
 
Posts: 2
Joined: Fri May 01, 2009 3:12 pm

Postby padde » Fri May 01, 2009 3:22 pm

The Resources are just int´s .. you could make a map
to associate those int´s with strings.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class Test extends Activity {
  3.  
  4.     public static HashMap<String,Integer> MyResources;
  5.  
  6.        
  7.  
  8.     public void onCreate(Bundle savedInstanceState) {
  9.  
  10.         super.onCreate(savedInstanceState);
  11.  
  12.         setContentView(R.layout.main);
  13.  
  14.         MyResources = getAllResourceIDs(R.raw.class, "sound_");
  15.  
  16.     }
  17.  
  18.  
  19.  
  20.     public static HashMap<String,Integer> getAllResourceIDs(Class<?> c, String start)
  21.  
  22.         throws IllegalArgumentException
  23.  
  24.     {
  25.  
  26.         HashMap<String, Integer> resmap = new HashMap<String,Integer>();
  27.  
  28.         Field[] fields = c.getFields();
  29.  
  30.         try {
  31.  
  32.             for(int i = 0; i < fields.length; i++) {
  33.  
  34.                 if(start != null)
  35.  
  36.                     if(fields[i].getName().startsWith(start))
  37.  
  38.                         resmap.put(fields[i].getName(), fields[i].getInt(null));
  39.  
  40.                 else
  41.  
  42.                     resmap.put(fields[i].getName(), fields[i].getInt(null));
  43.  
  44.             }
  45.  
  46.         } catch (Exception e) { throw new IllegalArgumentException(); }
  47.  
  48.         return resmap;
  49.  
  50.     }
  51.  
  52. }
  53.  
  54.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


In the example you get all files in res/raw folder that starts with "sound_"
Lets say you have "sound_moo.mp3" and "sound_bark.mp3" in res/raw.
You can access them now global with Test.MyResources.get("sound_bark.mp3")
instead of "R.raw.sound_bark" and you can now access them dynamic by simple generate
the key string for the map.

Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby rockhard » Fri May 01, 2009 4:49 pm

Thanks, this helped.
rockhard
Freshman
Freshman
 
Posts: 2
Joined: Fri May 01, 2009 3:12 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 8 guests