⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameeffects.java

📁 J2ME的游戏原代码!希望能帮助有需要帮助的师兄弟们!
💻 JAVA
字号:
/*
 * @(#)GameEffects.java, 1.7
 *
 * Copyright (c) 2001-2004 Sony Ericsson Mobile Communication AB (SEMC).
 *
 * German Branch
 * Heisenbergbogen 1, Aschheim 85609, Munich, Germany.
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * SEMC. ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with SEMC.
 *
 * @author  Rohit
 * @version 1.7, 10/10/2003
 */

package com.sonyericsson.erix;

import java.io.InputStream;
import javax.microedition.media.*;

    /**
     * The GameEffects class contains the functionality to provide Sound Effects. 
     */

public class GameEffects {

    /**
     * The different sounds that can be requested to be played, 
     * and their relative priority numbers. 
     * Lower numbers indicate higher priority.
     */

    /**
     * Game Start Sound.
     */
     public static final int GAME_START_SOUND = 1;

    /**
     * Game Win Sound
     */
     public static final int GAME_WIN_SOUND = 2;

    /**
     * Game Lose Sound
     */
     public static final int GAME_LOSE_SOUND = 3;

   	// The "Level Start" sound has been eliminated as of 20/06/2003
    /**
     * Level Start Sound
     */
     //public static final int LEVEL_START_SOUND = 4;

    /**
     * Level Complete Sound
     */
     public static final int LEVEL_COMPLETE_SOUND = 5;

    /**
     * Lose Life Sound
     */
     public static final int LIFE_LOSE_SOUND = 6;

    /**
     * Region Secured Sound
     */
     public static final int REGION_SECURED_SOUND = 7;
     
    /**
     * Used to indicate that in the current iteration,
     * there are no requests for playing any sounds has been received.
     */
     public static final int NO_SOUND = 99;

    /*
     * Input streams for each of the audio files present in the application.
     */

    /**
     * Input stream for the Game Start audio file.
     */
     private InputStream GameStartSoundInputStream;
     
    /**
     * Input stream for the Game Win audio file.
     */
     private InputStream GameWinSoundInputStream;
     
    /**
     * Input stream for the Game Lose audio file.
     */
     private InputStream GameLoseSoundInputStream;

   	// The "Level Start" sound has been eliminated as of 20/06/2003
    /**
     * Input stream for the Level Start audio file.
     */
     //private InputStream LevelStartSoundInputStream;

    /**
     * Input stream for the Level Complete audio file.
     */
     private InputStream LevelCompleteSoundInputStream;
     
    /**
     * Input stream for the Life Lose audio file.
     */
     private InputStream LifeLoseSoundInputStream;
     
    /**
     * Input stream for the Region Secured audio file.
     */
     private InputStream RegionSecuredSoundInputStream;
     
    /*
     * "Player"s for each of the audio files present in the application.
     */

    /**
     * Player for the Game Start audio file.
     */
     private Player GameStartSoundPlayer;

    /**
     * Player for the Game Win audio file.
     */
     private Player GameWinSoundPlayer;

    /**
     * Player for the Game Lose audio file.
     */
     private Player GameLoseSoundPlayer;

   	// The "Level Start" sound has been eliminated as of 20/06/2003
    /**
     * Player for the Level Start audio file.
     */
     //private Player LevelStartSoundPlayer;

    /**
     * Player for the Level Complete audio file.
     */
     private Player LevelCompleteSoundPlayer;

    /**
     * Player for the Life Lose audio file.
     */
     private Player LifeLoseSoundPlayer;

    /**
     * Player for the Region Secured audio file.
     */
     private Player RegionSecuredSoundPlayer;
     
     /**
      * Records whether or not the required media players 
      * were created successfully.
      * If not, then all requests to play sounds are ignored.
      */
      private boolean m_bMediaPlayersAvailable = false;

    /**
     * Reference of the ErixMIDlet object.
     */
     public ErixMIDlet m_objMidlet = null;

    /**
     * GameEffects Constructor
     */
     public GameEffects(ErixMIDlet objMidlet) {
        m_objMidlet = objMidlet;
        try {
            // Create the players needed for playing the different sounds.
            GameStartSoundInputStream = getClass().getResourceAsStream("/GameStart.mid");
            GameStartSoundPlayer = Manager.createPlayer(GameStartSoundInputStream, "audio/midi");
            GameStartSoundPlayer.realize();
        
            GameWinSoundInputStream = getClass().getResourceAsStream("/GameWin.mid");
            GameWinSoundPlayer = Manager.createPlayer(GameWinSoundInputStream, "audio/midi");
            GameWinSoundPlayer.realize();

            GameLoseSoundInputStream = getClass().getResourceAsStream("/GameLose.mid");
            GameLoseSoundPlayer = Manager.createPlayer(GameLoseSoundInputStream, "audio/midi");
            GameLoseSoundPlayer.realize();
        
            // The "Level Start" sound has been eliminated as of 20/06/2003
            //LevelStartSoundInputStream = getClass().getResourceAsStream("/LevelStart.mid");
            //LevelStartSoundPlayer = Manager.createPlayer(LevelStartSoundInputStream, "audio/midi");
            //LevelStartSoundPlayer.realize();
            
            LevelCompleteSoundInputStream = getClass().getResourceAsStream("/LevelComplete.mid");
            LevelCompleteSoundPlayer = Manager.createPlayer(LevelCompleteSoundInputStream, "audio/midi");
            LevelCompleteSoundPlayer.realize();
       
            LifeLoseSoundInputStream = getClass().getResourceAsStream("/LifeLose.mid");
            LifeLoseSoundPlayer = Manager.createPlayer(LifeLoseSoundInputStream, "audio/midi");
            LifeLoseSoundPlayer.realize();

            RegionSecuredSoundInputStream = getClass().getResourceAsStream("/RegionSecured.mid");
            RegionSecuredSoundPlayer = Manager.createPlayer(RegionSecuredSoundInputStream, "audio/midi");
            RegionSecuredSoundPlayer.realize();
            
            // All media players created successfully
            m_bMediaPlayersAvailable = true;
        }
        catch (Exception e) {
            System.out.println("Exception in GameEffects(): " + e.toString());
        }
    }

    /**
     * This method plays a specific type of sound requested. 
     * <p>
     * @param nSoundType Indicates the type of sound to be played. 
     * The type can be one of the following:
     * <UL> 
     *     <LI>GAME_START_SOUND</LI>
     *     <LI>GAME_WIN_SOUND</LI>
     *     <LI>GAME_LOSE_SOUND</LI>
     *     <LI>LEVEL_COMPLETE_SOUND</LI>
     *     <LI>LOSE_LIFE_SOUND</LI>
     *     <LI>REGION_SECURED_SOUND</LI>
     * </UL>
     */
    public void playSound(int nSoundType) {
        //System.out.println("*******************************");
        //System.out.println("Sound " + nSoundType + " requested");
        // If the user has set the Audio preference to "Off",
        // ignore the requests to play sounds.
        // A value of 0 (zero) for m_nAudioSetting indicates that the 
        // Audio preference is set to "On".
        //System.out.println("m_objMidlet.m_nAudioSetting = " + m_objMidlet.m_nAudioSetting);
        if (m_objMidlet.m_nAudioSetting != 0) {
            // Audio preference is not "On"; i.e., it is "Off".
            return;
        }
        
        // If the media players were not created successfully,
        // ignore the requests to play sounds.
        //System.out.println("m_bMediaPlayersAvailable = " + m_bMediaPlayersAvailable);
        if (m_bMediaPlayersAvailable == false) {
            return;
        }
        try {
            switch (nSoundType)
            {
            	case GAME_START_SOUND:
            	   GameStartSoundPlayer.start();
            	   break;
            	case GAME_WIN_SOUND:
            	   GameWinSoundPlayer.start();
            	   break;
            	case GAME_LOSE_SOUND:
            	   GameLoseSoundPlayer.start();
            	   break;
            	// The "Level Start" sound has been eliminated as of 20/06/2003
            	//case LEVEL_START_SOUND:
            	   //LevelStartSoundPlayer.start();
            	   //break;
            	case LEVEL_COMPLETE_SOUND:
            	   LevelCompleteSoundPlayer.start();
            	   break;
            	case LIFE_LOSE_SOUND:
            	   LifeLoseSoundPlayer.start();
            	   break;
            	case REGION_SECURED_SOUND:
            	   RegionSecuredSoundPlayer.start();
            	   break;
           	    default:
            	   break;
            } // end of switch statement which playes the requested sound.
        } // end of try block in which playing of sounds is tried.
        catch (Exception e) {
        }
    } // end of method playSound()

   /**
    * This method indicates that the objects referred-to by this object
    * are no longer needed.
    */
    public void cleanUp() {
     // Indicate that the various InputStreams are no longer needed.
     GameStartSoundInputStream = null;
     GameWinSoundInputStream = null;
     GameLoseSoundInputStream = null;
     //LevelStartSoundInputStream = null;
     LevelCompleteSoundInputStream = null;
     LifeLoseSoundInputStream = null;
     RegionSecuredSoundInputStream = null;

     // Indicate that the various Players are no longer needed.
     GameStartSoundPlayer = null;
     GameWinSoundPlayer = null;
     GameLoseSoundPlayer = null;
     //LevelStartSoundPlayer = null;
     LevelCompleteSoundPlayer = null;
     LifeLoseSoundPlayer = null;
     RegionSecuredSoundPlayer = null;
     
     // Cease referring to the ErixMIDlet object
     m_objMidlet = null;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -