More Sound and Music Methods

  ... the case you feel the need for a new feature or want to submit one.

More Sound and Music Methods

Postby tauame » Wed Mar 28, 2012 4:11 pm

I think that Sound and Music classes must receive a bit more attention.

My suggestion is to create methods such as "isPlaying()" and "isLoaded()" for both classes, since sometimes I get the "sample # not ready" problem and is annoying to have boolean variables controlling if each sound is playing.

If I manage to implement some of these methods or others that I might find interesting, I will post it here. :)
tauame
 
Posts: 6
Joined: Thu Mar 15, 2012 4:06 pm

Re: More Sound and Music Methods

Postby tauame » Wed Mar 28, 2012 7:57 pm

Hello, I modified the Sound class to have the methods "isLoaded()" and "isStarted()"

I basically implemented the OnLoadCompleteListener interface into the Sound class for the "isLoaded()" method.

I really think that the "isLoaded()" is needed, but im in doubt about the "isStarted()" (stores in a boolean if the method play() or stop() were called). How can I submit these or (at least one of them) changes to the github repo?

Code changes:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Sound extends BaseAudioEntity implements OnLoadCompleteListener{
  2. //...
  3.         private boolean mLoaded = false;
  4.         private boolean mStarted = false;
  5. //...
  6.         Sound(final SoundManager pSoundManager, final int pSoundID) {
  7.                 super(pSoundManager);
  8.                 this.getSoundPool().setOnLoadCompleteListener(this);
  9.  
  10.                 this.mSoundID = pSoundID;
  11.         }
  12. //...
  13.  
  14.         @Override
  15.         public void play() throws SoundReleasedException {
  16.                 super.play();
  17.  
  18.                 final float masterVolume = this.getMasterVolume();
  19.                 final float leftVolume = this.mLeftVolume * masterVolume;
  20.                 final float rightVolume = this.mRightVolume * masterVolume;
  21.  
  22.                 this.mStreamID = this.getSoundPool().play(this.mSoundID, leftVolume, rightVolume, 1, this.mLoopCount, this.mRate);
  23.                 this.mStarted = true;
  24.         }
  25.  
  26.         @Override
  27.         public void stop() throws SoundReleasedException {
  28.                 super.stop();
  29.  
  30.                 if(this.mStreamID != 0) {
  31.                         this.getSoundPool().stop(this.mStreamID);
  32.                 }
  33.                
  34.                 this.mStarted = false;
  35.                
  36.         }
  37. //...
  38.         @Override
  39.         public void release() throws SoundReleasedException {
  40.                 this.assertNotReleased();
  41.  
  42.                 this.getSoundPool().unload(this.mSoundID);
  43.                 this.mSoundID = 0;
  44.  
  45.                 this.getAudioManager().remove(this);
  46.  
  47.                 super.release();
  48.                
  49.                 this.mStarted = false;
  50.         }
  51.  
  52.         @Override
  53.         public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
  54.                 mLoaded = true;
  55.         }
  56.  
  57. //...
  58.  
  59.         public boolean isLoaded(){
  60.                 return mLoaded;
  61.         }
  62.  
  63.         public boolean isStarted(){
  64.                 return mStarted;               
  65.         }
  66. }
  67.  
Parsed in 0.015 seconds, using GeSHi 1.0.8.4
tauame
 
Posts: 6
Joined: Thu Mar 15, 2012 4:06 pm

Re: More Sound and Music Methods

Postby BenJeremy » Sat May 26, 2012 6:03 pm

I see isLoaded is now implemented, but isStarted is a bit more problematic.

The AndEngine sound classes are built around Android's SoundPool, which is lacking one event I believe is an absolute necessity - onPlaybackCompleted.

To handle things nicely, Sound should support isPlaying() and isPaused(), and the play, stop, pause and resume functions could have a bit more intelligence. isPlaying() needs to know when the sound has completed (if it's not looping), but since SoundPool doesn't seem to support this, we are out of luck there. The Sound class is an excellent opportunity to at least clean up some things. but as it stands, it is still a very thin layer over Android's soundpool api.

I've been trying to get reliable playback on my project, and have been severely disappointed with the way things have been (not) working out. The damn thing seems random!!! The same sound played on one of my screens fails to play on another. Some mystical combination of stops and pauses before playing a sound sometimes works. Very frustrating!!
BenJeremy
 
Posts: 8
Joined: Mon May 14, 2012 4:07 am

Re: More Sound and Music Methods

Postby BenJeremy » Wed May 30, 2012 10:50 pm

I ended up dumping anything related to SoundPool. The random failures were driving me nuts... even tweaking my audio file assets, I could not get consistent results, or I'd get it to work on one tablet, only to have it fail miserably on an older tablet.

I gutted my sound stuff and now use an enhancement of MediaPlayer, which seems to have a more complete API. Just rmeember not to "stop()" audio, only "pause()" if you plan on replaying the audio within the same context.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.io.IOException;
  3.  
  4. import android.content.res.AssetFileDescriptor;
  5. import android.content.res.AssetManager;
  6. import android.media.MediaPlayer;
  7.  
  8. public class MediaPlayerEx extends MediaPlayer
  9. {
  10.         public boolean playRequested = false;
  11.         public boolean isPrepared = false;
  12.         @Override
  13.         public void prepare() throws IOException, IllegalStateException
  14.         {
  15.                 super.prepare();
  16.                 this.isPrepared = true;
  17.         }
  18.         @Override
  19.         public void prepareAsync() throws IllegalStateException
  20.         {
  21.                 this.isPrepared = false;
  22.                 this.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
  23.                 {
  24.                       public void onPrepared(MediaPlayer mp)
  25.                       {
  26.                           isPrepared = true;
  27.                           if ( playRequested )
  28.                           {
  29.                                   start();
  30.                           }
  31.                       }
  32.             });        
  33.                 super.prepareAsync();
  34.         }
  35.         @Override
  36.         public void start() throws IllegalStateException
  37.         {
  38.                 if ( this.isPrepared )
  39.                 {
  40.                         this.playRequested = false;
  41.                         super.start();
  42.                 }
  43.                 else
  44.                 {
  45.                         this.playRequested = true;
  46.                 }
  47.         }
  48.         @Override
  49.         public void stop() throws IllegalStateException
  50.         {
  51.                 if ( this.playRequested )
  52.                 {
  53.                         this.playRequested = false;
  54.                 }
  55.                 super.stop();
  56.         }
  57.        
  58.         public void setDataSourceFromAsset(AssetManager assetMgr, String path) throws IOException
  59.         {
  60.                 AssetFileDescriptor fd = assetMgr.openFd(path);
  61.                 this.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
  62.         }
  63.        
  64.  
  65. }
  66.  
Parsed in 0.012 seconds, using GeSHi 1.0.8.4


Loading the audio:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                        
  2.  
  3.                        this.mpSomeSound = new MediaPlayerEx();
  4.                         try
  5.                         {
  6.                                 this.mpSomeSound.setDataSourceFromAsset(this.gameActivity.getAssets(), assetPath);
  7.                                 this.mpSomeSound.prepareAsync();
  8.                                 this.mpSomeSound.setLooping(true);
  9.                         }
  10.                         catch (IOException e)
  11.                         {
  12.                                 this.mpSomeSound.release();
  13.                                 this.mpSomeSound = null;
  14.                                 e.printStackTrace();
  15.                         }
  16.  
Parsed in 0.010 seconds, using GeSHi 1.0.8.4



Releasing the audio object:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 if ( this.mpSomeSound != null )
  2.                 {
  3.                         this.mpSomeSound.stop();
  4.                         this.mpSomeSound.release();
  5.                         Debug.i("App.PlayScreen", "Released mpSomeSound");
  6.                 }
  7.  
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


Playing, pausing:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                // Playing
  3.                 if ( this.mpSomeSound!= null && this.mpSomeSound.isPlaying()==false)
  4.                 {
  5.                         this.mpSomeSound.start();
  6.                 }
  7.  
  8.                // Pausing (stopping)
  9.                 if ( this.mpSomeSound!= null && this.mpSomeSound.isPlaying())
  10.                 {
  11.                         this.mpSomeSound.pause();
  12.                 }
  13.  
  14.  
  15.  
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


My class handles "start()" as a playback request, so it executes as soon as it's loaded.

This has been very constent for me, working even on my "ancient" Chinese knockoff "Flytouch 3" tablet running Android 2.3.
BenJeremy
 
Posts: 8
Joined: Mon May 14, 2012 4:07 am

Re: More Sound and Music Methods

Postby OzLark » Thu May 31, 2012 2:33 am

Thanks for sharing!
Did this post help you? Feel free to return the gesture by downloading Ninja Bees, creating a level (or more) and sharing it to the Ninja Bees website!

Ninja Bees - Our latest Android game, check it out on Google Play (free) or paid with no ads
OzLark
 
Posts: 1130
Joined: Sun Feb 05, 2012 3:19 am

Re: More Sound and Music Methods

Postby shihab_returns » Thu Dec 20, 2012 12:31 pm

Thanks for sharing....
I have some question.
Qs.1.
When We back from game play scene to main menu scene, does we have to release the sound file which is played in current scene.

2. How to set resume & looping on the music file ?


Wait for somebody's great knock!!! :D
shihab_returns
 
Posts: 105
Joined: Tue Oct 16, 2012 8:48 am
Location: Dhaka


Return to Features

Who is online

Users browsing this forum: No registered users and 7 guests