Play sound from assets?

Discussion in 'Application Development' started by GIR, Oct 25, 2010.

  1. GIR

    GIR Well-Known Member
    18

    Feb 14, 2010
    96
    10
    18
    Classified.
    UK
    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
    18

    Feb 14, 2010
    96
    10
    18
    Classified.
    UK
    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
    5

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

    GIR Well-Known Member
    18

    Feb 14, 2010
    96
    10
    18
    Classified.
    UK

    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
    5

    Nov 30, 2010
    1
    0
    5
    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...