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. melani78
    Replies:
    0
    Views:
    109
  2. melani78
    Replies:
    2
    Views:
    241
  3. userzero55
    Replies:
    3
    Views:
    260
  4. startac4
    Replies:
    3
    Views:
    297
  5. MATTHEW GLYNNDWR
    Replies:
    2
    Views:
    260
  6. usuario10
    Replies:
    4
    Views:
    798
  7. Vya
    Replies:
    0
    Views:
    473
  8. rdpinr
    Replies:
    9
    Views:
    1,070
  9. Adi Inbar
    Replies:
    2
    Views:
    1,377
  10. Androiiiidd
    Replies:
    2
    Views:
    716

Share This Page

Loading...