Play sound from assets?


Last Updated:

  1. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    I want to play a sound in an app im writing as part of my learning curve.

    Im using eclipse on Ubuntu for 2.1, and have the track.ogg file in the assets directory.

    How does one go about playing the track when the program starts?

    I know its a combination of
    Code (Text):
    1.  R.raw.soundfile
    but Im unsure how this works if theres more than 1 sound file.

    I found many conflicting samples of code online and id like clarification.
    Some of the different ways of doing this include::

    Code (Text):
    1. aPlayer.play(mContext, Uri.parse("android.resource://"+PACKAGE_NAME+"/"+R.raw.soundfile), false, AudioManager.STREAM_SYSTEM);
    Code (Text):
    1. mSoundManager.addSound(1, R.raw.sound);
    Code (Text):
    1. mp.setDataSource(PATH_TO_FILE);
    Ive found some info at MediaPlayer | Android Developers, android.net.Uri) BUT i cant find any code samples for setDataSource
    Any pointers?

    Thanks in advance,
    GIR
     

    Advertisement
  2. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    SOLVED!

    After an afternoon experimenting I found the correct place to store the sound file, and how to address it from within the java file.
     
  3. peertje888

    peertje888 New Member

    Joined:
    Oct 27, 2010
    Messages:
    1
    Likes Received:
    0
    Would you be so kind and share your solution to the rest of the world?
     
  4. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10

    Sure, I can now that I have my Ubuntu pc fresh back online :)

    First I had to create a directory called 'raw' under 'res', and then paste in the track.ogg file.

    This is all I entered into the .java file to make it play the track when the App starts:

    Code (Text):
    1.  
    2. MediaPlayer player = MediaPlayer.create(this, R.raw.track);
    3.         try {
    4.             player.prepare();
    5.         } catch (IllegalStateException e) {
    6.             // TODO Auto-ge
    7.             e.printStackTrace();
    8.        
    9.         } catch (IOException e) {
    10.             // TODO Auto-generated catch block
    11.             e.printStackTrace();
    12.         }
    13.        
    14.         player.start();
    15.        
    16.        
    17.     }
    18.  
    Hope this helps,
    GIR
     
  5. valientinx

    valientinx New Member

    Joined:
    Nov 30, 2010
    Messages:
    1
    Likes Received:
    0
    MediaPlayer mp = new MediaPlayer();
    AssetFileDescriptor descriptor = getAssets().openFd( FILE_NAME );
    mp.setDataSource( descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength() );
    descriptor.close();

    mp.prepare();
    mp.start();

    android API stinks, somebody should fired thous guys.
     

Share This Page

Loading...