1. Are you ready for the Galaxy S20? Here is everything we know so far!

Play sound from assets?

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

  1. GIR

    GIR Member
    Thread Starter

    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
     



    1. Download the Forums for Android™ app!


      Download

       
  2. GIR

    GIR Member
    Thread Starter

    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 Lurker

    Would you be so kind and share your solution to the rest of the world?
     
  4. GIR

    GIR Member
    Thread Starter


    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 Lurker

    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.
     
Loading...
Similar Threads - Play sound assets
  1. Sleep Sounds
    Replies:
    2
    Views:
    76
  2. Rgarner
    Replies:
    11
    Views:
    306
  3. Callandha
    Replies:
    0
    Views:
    230
  4. bkelly13
    Replies:
    7
    Views:
    411
  5. Androiiiidd
    Replies:
    2
    Views:
    431
  6. MoodyBlues
    Replies:
    12
    Views:
    2,054
  7. Gigue
    Replies:
    5
    Views:
    741
  8. xander119
    Replies:
    0
    Views:
    423
  9. Nixie14
    Replies:
    5
    Views:
    4,279
  10. Rachel120
    Replies:
    4
    Views:
    2,984

Share This Page

Loading...