A patch for a few additions

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

A patch for a few additions

Postby cpasjuste » Tue Aug 31, 2010 2:31 pm

Hello, here is a patch to add a few things to andengine :

1 - Ability to use "onCompletionListener" for Music and Sound

- A simple addition to retrieve the MediaPlayer instance so we can play with it, i use it to get the end of a music/sound :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. diff -r 22bfc9c28a25 src/org/anddev/andengine/audio/music/Music.java
  2. --- a/src/org/anddev/andengine/audio/music/Music.java   Sun Aug 29 20:34:41 2010 +0200
  3. +++ b/src/org/anddev/andengine/audio/music/Music.java   Tue Aug 31 14:25:26 2010 +0200
  4. @@ -98,6 +98,11 @@
  5.         public void seekTo(final int pMilliseconds) {
  6.                 this.mMediaPlayer.seekTo(pMilliseconds);
  7.         }
  8. +      
  9. +       public MediaPlayer getMediaPlayer()
  10. +       {
  11. +               return this.mMediaPlayer;
  12. +       }
  13.  
  14.         // ===========================================================
  15.         // Inner and Anonymous Classes
  16.  
Parsed in 0.011 seconds, using GeSHi 1.0.8.4


Example :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mMusic = MusicFactory.createMusicFromPath( this.mEngine.getMusicManager(), mCtx, "I seem to have lost my ticket mix.mp3" );
  2.                         mMusic.getMediaPlayer().setOnCompletionListener( new OnCompletionListener()
  3.                         {
  4.                 @Override
  5.                 public void onCompletion( final MediaPlayer pMediaPlayer )
  6.                 {
  7.                         Log.d( "MotoGame", "!MusicEnd!" );
  8.                 }
  9.                         });
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


2 - Ability to load a music/sound from filesystem ( sdcard ) :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. diff -r 22bfc9c28a25 src/org/anddev/andengine/audio/music/MusicFactory.java
  2. --- a/src/org/anddev/andengine/audio/music/MusicFactory.java    Sun Aug 29 20:34:41 2010 +0200
  3. +++ b/src/org/anddev/andengine/audio/music/MusicFactory.java    Tue Aug 31 14:25:26 2010 +0200
  4. @@ -1,5 +1,8 @@
  5.  package org.anddev.andengine.audio.music;
  6.  
  7. +import java.io.File;
  8. +import java.io.FileDescriptor;
  9. +import java.io.FileInputStream;
  10.  import java.io.IOException;
  11.  
  12.  import android.content.Context;
  13. @@ -42,6 +45,19 @@
  14.         // Methods
  15.         // ===========================================================
  16.  
  17. +       public static Music createMusicFromPath(final MusicManager pMusicManager, final Context pContext, final String pAssetPath) throws IOException {
  18. +               final MediaPlayer mediaPlayer = new MediaPlayer();
  19. +
  20. +               final File mFile = new File( MusicFactory.sAssetBasePath + pAssetPath );
  21. +               mediaPlayer.setDataSource( new FileInputStream( mFile ).getFD() );
  22. +               mediaPlayer.prepare();
  23. +
  24. +               final Music music = new Music(pMusicManager, mediaPlayer);
  25. +               pMusicManager.add(music);
  26. +
  27. +               return music;
  28. +       }
  29. +      
  30.         public static Music createMusicFromAsset(final MusicManager pMusicManager, final Context pContext, final String pAssetPath) throws IOException {
  31.                 final MediaPlayer mediaPlayer = new MediaPlayer();
  32.  
  33. diff -r 22bfc9c28a25 src/org/anddev/andengine/audio/sound/SoundFactory.java
  34. --- a/src/org/anddev/andengine/audio/sound/SoundFactory.java    Sun Aug 29 20:34:41 2010 +0200
  35. +++ b/src/org/anddev/andengine/audio/sound/SoundFactory.java    Tue Aug 31 14:25:26 2010 +0200
  36. @@ -1,7 +1,12 @@
  37.  package org.anddev.andengine.audio.sound;
  38.  
  39. +import java.io.File;
  40. +import java.io.FileDescriptor;
  41. +import java.io.FileInputStream;
  42.  import java.io.IOException;
  43.  
  44. +import org.anddev.andengine.audio.music.MusicFactory;
  45. +
  46.  import android.content.Context;
  47.  
  48.  /**
  49. @@ -39,6 +44,13 @@
  50.         // Methods
  51.         // ===========================================================
  52.  
  53. +       public static Sound createSoundFromPath(final SoundManager pSoundManager, final Context pContext, final String pAssetPath) throws IOException {
  54. +               final int soundID = pSoundManager.getSoundPool().load( SoundFactory.sAssetBasePath + pAssetPath, 1);
  55. +               final Sound sound = new Sound(pSoundManager, soundID);
  56. +               pSoundManager.add(sound);
  57. +               return sound;
  58. +       }
  59. +      
  60.         public static Sound createSoundFromAsset(final SoundManager pSoundManager, final Context pContext, final String pAssetPath) throws IOException {
  61.                 final int soundID = pSoundManager.getSoundPool().load(pContext.getAssets().openFd(SoundFactory.sAssetBasePath + pAssetPath), 1);
  62.                 final Sound sound = new Sound(pSoundManager, soundID);
Parsed in 0.013 seconds, using GeSHi 1.0.8.4


Example :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 MusicFactory.setAssetBasePath( "/mnt/sdcard/AndroMoto/" );
  2.                
  3.                 try
  4.                 {
  5.                         mMusic = MusicFactory.createMusicFromPath( this.mEngine.getMusicManager(), mCtx, "I seem to have lost my ticket mix.mp3" );
  6.                 }
  7.                 catch (final IOException e)
  8.                 {
  9.                         Debug.e("Error", e);
  10.                 }
Parsed in 0.011 seconds, using GeSHi 1.0.8.4


3 - Add Culling for scaled shape :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. diff -r 22bfc9c28a25 src/org/anddev/andengine/entity/shape/RectangularShape.java
  2. --- a/src/org/anddev/andengine/entity/shape/RectangularShape.java       Sun Aug 29 20:34:41 2010 +0200
  3. +++ b/src/org/anddev/andengine/entity/shape/RectangularShape.java       Tue Aug 31 14:25:26 2010 +0200
  4. @@ -108,14 +108,29 @@
  5.        
  6.         @Override
  7.         protected boolean isCulled(final Camera pCamera) {
  8. -               final float x = this.mX;
  9. -               final float y = this.mY;
  10. -               return x > pCamera.getMaxX()
  11. -                       || y > pCamera.getMaxY()
  12. -                       || x + this.getWidth() < pCamera.getMinX()
  13. -                       || y + this.getHeight() < pCamera.getMinY();
  14. +              
  15. +               float x, y;
  16. +              
  17. +               if ( this.isScaled() )
  18. +               {
  19. +                       x = this.mX - ( ( this.getScaleCenterX() * this.mScaleX ) / 2 );
  20. +                       y = this.mY - ( ( this.getScaleCenterY() * this.mScaleY ) / 2 );
  21. +                       return x > pCamera.getMaxX()
  22. +                               || y > pCamera.getMaxY()
  23. +                               || x + ( this.getWidth() * this.getScaleX() ) < pCamera.getMinX()
  24. +                               || y + ( this.getHeight() * this.getScaleY() ) < pCamera.getMinY();
  25. +               }
  26. +               else
  27. +               {
  28. +                       x = this.mX;
  29. +                       y = this.mY;
  30. +                       return x > pCamera.getMaxX()
  31. +                               || y > pCamera.getMaxY()
  32. +                               || x + this.getWidth() < pCamera.getMinX()
  33. +                               || y + this.getHeight() < pCamera.getMinY();
  34. +               }
  35.         }
  36. -
  37. +      
  38.         @Override
  39.         protected void drawVertices(final GL10 pGL, final Camera pCamera) {
  40.                 pGL.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
Parsed in 0.012 seconds, using GeSHi 1.0.8.4
cpasjuste
 
Posts: 63
Joined: Tue Jun 22, 2010 3:18 pm

Re: A patch for a few additions

Postby Nicolas Gramlich » Wed Sep 01, 2010 1:02 am

I merged in the first two patches, but still I'm not sure about the Culling :( I think "stupid" culling is better :?

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: A patch for a few additions

Postby cpasjuste » Wed Sep 01, 2010 9:36 am

Ok Nicolas, thanks :)
cpasjuste
 
Posts: 63
Joined: Tue Jun 22, 2010 3:18 pm

Re: A patch for a few additions

Postby mjmeyer23@gmail.com » Wed Dec 15, 2010 7:51 pm

Good stuff, however, I notice that there is no OnCompletionListener for Sound, only for Music. I think that's a limitation on the Android side of things.

I have dealt with this in the past by maintaining a map of sounds Ids to their duration and then in the Play() of my wrapper, have it returning the time at which the sound should be finished based on it's duration.

Crude, but it met my needs.

I obtain the duration during init of sounds by using a temporary MediaPlayer, loading my sound to it, and calling MediaPlayer.getDuration(), then releasing the mediaPlayer, never actually using it to play.

If you dont object to this approach I would be willing to try adding it to the org.anddev.andengine.audio.sound.Sound class.

I figure computing duration should be optional so as to avoid any overhead for apps that wont need it. When duration has not been pre-determined, Play() would return 0, otherwise play would return now + duration.

I'm going to need this, so I either contribute it to the engine or handle it in my app.
mjmeyer23@gmail.com
 
Posts: 7
Joined: Wed Nov 03, 2010 1:12 am


Return to Features

Who is online

Users browsing this forum: Google [Bot] and 4 guests