Android Open Source - gameengine Sound Module






From Project

Back to project page gameengine.

License

The source code is released under:

Apache License

If you think the Android project gameengine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.garrapeta.gameengine.module;
// ww w  .j  a va 2 s. com
import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.SoundPool;

import com.garrapeta.gameengine.utils.LogX;

/**
 * Sound samples manager
 * 
 * TODO: Use {@link SoundPool}
 */
public class SoundModule implements OnCompletionListener {

    private final static String TAG = SoundModule.class.getSimpleName();

    private final SoundModuleDelegate mDelegate;

    private final static short ACTION_PLAY = 0;
    private final static short ACTION_PAUSE = 1;
    private final static short ACTION_RESUME = 2;
    private final static short ACTION_STOP = 3;

    /**
     * Constructor
     * 
     * @param context
     * @param minimumLevel
     */
    public SoundModule(Context context, short minimumLevel) {
        mDelegate = new SoundModuleDelegate(context, minimumLevel);
    }

    public SoundModuleDelegate.ResourceData create(short level, short key) {
        LogX.v(TAG, "Creating: " + key);
        return mDelegate.create(level, key);
    }

    public void play(short key) {
        play(key, false);
    }

    public void play(short key, boolean repeat) {
        LogX.v(TAG, "Play: " + key);
        try {
            short repeatShort = (short) ((repeat) ? 1 : 0);
            mDelegate.executeOverOneResourceForKey(key, ACTION_PLAY, repeatShort);
        } catch (Exception e) {
            LogX.e(TAG, "Error playing resource " + key, e);
        }
    }

    public void stop(short key) {
        LogX.v(TAG, "Stopping: " + key);
        try {
            mDelegate.executeOverAllResourcesForKey(key, ACTION_STOP);
        } catch (Exception e) {
            LogX.e(TAG, "Error stoping resource " + key, e);
        }
    }

    public void pauseAll() {
        LogX.i(TAG, "Pausing all...");
        try {
            mDelegate.executeOverAllResources(ACTION_PAUSE);
        } catch (Exception e) {
            LogX.e(TAG, "Error pausing all the resources", e);
        }
    }

    public void resumeAll() {
        LogX.i(TAG, "Resuming all...");
        try {
            mDelegate.executeOverAllResources(ACTION_RESUME);
        } catch (Exception e) {
            LogX.e(TAG, "Error resuming all the resources", e);
        }

    }

    public void releaseAll() {
        LogX.i(TAG, "Releasing all...");
        mDelegate.releaseAll();
    }

    private void onPlay(MediaPlayer player, boolean repeat) {
        player.setLooping(repeat);
        if (player.isPlaying()) {
            player.seekTo(0);
        }
        player.start();
    }

    private void onPause(MediaPlayer player) {
        if (player.isPlaying()) {
            player.pause();
        }
    }

    private void onResume(MediaPlayer player) {
        if (player.getCurrentPosition() != 0) {
            player.start();
        }
    }

    private void onStop(MediaPlayer player) {
        try {
            if (player.isPlaying()) {
                player.stop();
                player.prepare();
                player.seekTo(0);
            }
        } catch (IOException e) {
            throw new IllegalStateException("Error stopping the player", e);
        }
    }

    @Override
    public void onCompletion(MediaPlayer player) {
        player.seekTo(0);
    }

    /**
     * Delegate used by the module
     * 
     * @author garrapeta
     */
    private class SoundModuleDelegate extends LoadedLevelActionsModule<Integer, MediaPlayer, Short> {

        private final Context mContext;

        private SoundModuleDelegate(Context context, short minimumLevel) {
            super(minimumLevel);
            mContext = context;
        }

        @Override
        protected MediaPlayer obtain(Integer resId) {
            MediaPlayer player = MediaPlayer.create(mContext, resId);
            if (player != null) {
                player.setOnCompletionListener(SoundModule.this);
            } else {
                LogX.e(TAG, "Could not load player for sample id: " + resId);
            }
            return player;
        }

        @Override
        protected void onExecute(MediaPlayer player, Short... params) {
            if (player == null) {
                LogX.e(TAG, "Audio player is null!!");
                return;
            }
            switch (params[0]) {
            case ACTION_PLAY:
                onPlay(player, params[1] > 0);
                break;
            case ACTION_PAUSE:
                onPause(player);
                break;
            case ACTION_RESUME:
                onResume(player);
                break;
            case ACTION_STOP:
                onStop(player);
                break;
            }
        }

        @Override
        protected void onRelease(MediaPlayer player) {
            LogX.v(TAG, "Releasing: " + player);
            player.release();
        }
    }

}




Java Source Code List

com.badlogic.gdx.math.MathUtils.java
com.badlogic.gdx.math.Matrix3.java
com.badlogic.gdx.math.Matrix4.java
com.badlogic.gdx.math.Quaternion.java
com.badlogic.gdx.math.Vector2.java
com.badlogic.gdx.math.Vector3.java
com.badlogic.gdx.physics.box2d.BodyDef.java
com.badlogic.gdx.physics.box2d.Body.java
com.badlogic.gdx.physics.box2d.ChainShape.java
com.badlogic.gdx.physics.box2d.CircleShape.java
com.badlogic.gdx.physics.box2d.ContactFilter.java
com.badlogic.gdx.physics.box2d.ContactImpulse.java
com.badlogic.gdx.physics.box2d.ContactListener.java
com.badlogic.gdx.physics.box2d.Contact.java
com.badlogic.gdx.physics.box2d.DestructionListener.java
com.badlogic.gdx.physics.box2d.EdgeShape.java
com.badlogic.gdx.physics.box2d.Filter.java
com.badlogic.gdx.physics.box2d.FixtureDef.java
com.badlogic.gdx.physics.box2d.Fixture.java
com.badlogic.gdx.physics.box2d.JointDef.java
com.badlogic.gdx.physics.box2d.JointEdge.java
com.badlogic.gdx.physics.box2d.Joint.java
com.badlogic.gdx.physics.box2d.Manifold.java
com.badlogic.gdx.physics.box2d.MassData.java
com.badlogic.gdx.physics.box2d.PolygonShape.java
com.badlogic.gdx.physics.box2d.QueryCallback.java
com.badlogic.gdx.physics.box2d.RayCastCallback.java
com.badlogic.gdx.physics.box2d.Shape.java
com.badlogic.gdx.physics.box2d.Transform.java
com.badlogic.gdx.physics.box2d.WorldManifold.java
com.badlogic.gdx.physics.box2d.World.java
com.badlogic.gdx.physics.box2d.joints.DistanceJointDef.java
com.badlogic.gdx.physics.box2d.joints.DistanceJoint.java
com.badlogic.gdx.physics.box2d.joints.FrictionJointDef.java
com.badlogic.gdx.physics.box2d.joints.FrictionJoint.java
com.badlogic.gdx.physics.box2d.joints.GearJointDef.java
com.badlogic.gdx.physics.box2d.joints.GearJoint.java
com.badlogic.gdx.physics.box2d.joints.MouseJointDef.java
com.badlogic.gdx.physics.box2d.joints.MouseJoint.java
com.badlogic.gdx.physics.box2d.joints.PrismaticJointDef.java
com.badlogic.gdx.physics.box2d.joints.PrismaticJoint.java
com.badlogic.gdx.physics.box2d.joints.PulleyJointDef.java
com.badlogic.gdx.physics.box2d.joints.PulleyJoint.java
com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef.java
com.badlogic.gdx.physics.box2d.joints.RevoluteJoint.java
com.badlogic.gdx.physics.box2d.joints.RopeJointDef.java
com.badlogic.gdx.physics.box2d.joints.RopeJoint.java
com.badlogic.gdx.physics.box2d.joints.WeldJointDef.java
com.badlogic.gdx.physics.box2d.joints.WeldJoint.java
com.badlogic.gdx.physics.box2d.joints.WheelJointDef.java
com.badlogic.gdx.physics.box2d.joints.WheelJoint.java
com.badlogic.gdx.utils.Array.java
com.badlogic.gdx.utils.ComparableTimSort.java
com.badlogic.gdx.utils.Disposable.java
com.badlogic.gdx.utils.GdxRuntimeException.java
com.badlogic.gdx.utils.LongArray.java
com.badlogic.gdx.utils.LongMap.java
com.badlogic.gdx.utils.NumberUtils.java
com.badlogic.gdx.utils.Pool.java
com.badlogic.gdx.utils.Sort.java
com.badlogic.gdx.utils.StringBuilder.java
com.badlogic.gdx.utils.TimSort.java
com.garrapeta.MathUtils.java
com.garrapeta.gameengine.Actor.java
com.garrapeta.gameengine.AsyncGameMessage.java
com.garrapeta.gameengine.BitmapManager.java
com.garrapeta.gameengine.Box2DActor.java
com.garrapeta.gameengine.Box2DWorld.java
com.garrapeta.gameengine.GameMessage.java
com.garrapeta.gameengine.GameView.java
com.garrapeta.gameengine.GameWorld.java
com.garrapeta.gameengine.ShapeDrawer.java
com.garrapeta.gameengine.SyncGameMessage.java
com.garrapeta.gameengine.Viewport.java
com.garrapeta.gameengine.actor.Box2DAtomicActor.java
com.garrapeta.gameengine.actor.Box2DCircleActor.java
com.garrapeta.gameengine.actor.Box2DEdgeActor.java
com.garrapeta.gameengine.actor.Box2DLoopActor.java
com.garrapeta.gameengine.actor.Box2DOpenChainActor.java
com.garrapeta.gameengine.actor.Box2DPolygonActor.java
com.garrapeta.gameengine.actor.IAtomicActor.java
com.garrapeta.gameengine.actor.SimpleActor.java
com.garrapeta.gameengine.module.LevelActionsModule.java
com.garrapeta.gameengine.module.LoadedLevelActionsModule.java
com.garrapeta.gameengine.module.SoundModule.java
com.garrapeta.gameengine.module.VibrationModule.java
com.garrapeta.gameengine.utils.IOUtils.java
com.garrapeta.gameengine.utils.LogX.java
com.garrapeta.gameengine.utils.PhysicsUtils.java
com.garrapeta.gameengine.utils.Pool.java