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

📄 soundmanager6600.java

📁 一款基于java 的赛车类游戏 一款基于java 的赛车类游戏
💻 JAVA
字号:
/*
 * Created on 2005-06-14
 *
 * Copyright (c) 2005 nanoGames. All Rights Reserved.
 *
 */
//package com.nano.KangooJumper;

import java.io.InputStream;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
//import com.nokia.mid.sound.Sound;


/**
 * @author plumkawka
 * SoundManager6600
 */
public class SoundManager6600
{

	public final static int	MAX_SOUNDS	= 8;
	public final static byte SNDTYPE_OTA  = 1;
	public final static byte SNDTYPE_MIDI  = 2;
	public final static byte SNDTYPE_WAV  = 3;
	

	private static Player[]	mPlayer;
	//private static Sound[]	mSound;
	private static byte[][]	mSoundData;
	private static int[] mSoundVolume;
	private byte[]			nSoundType;
	private	short[]			nSoundPlayTime;
	
	private boolean			bIsPlaying;
	private static boolean	bIsEnabled	= true;
	private int				nSoundCount;
	private int				nCurrentSound;
	private int				nCurrentLoop;
	private long			nStartPlayTime;
	private	long			nPlayTime;



	public SoundManager6600()
	{

		mPlayer = new Player[MAX_SOUNDS];
		//mSound = new Sound[MAX_SOUNDS];
		mSoundData = new byte[MAX_SOUNDS][];
		mSoundVolume = new int[MAX_SOUNDS];
		nSoundType = new byte[MAX_SOUNDS];
		nSoundPlayTime = new short[MAX_SOUNDS];
		bIsPlaying = false;
		nSoundCount = 0;
		nPlayTime = 500;
		nCurrentSound = -1;
	}



	public void releaseSounds()
	{
		if (mPlayer != null)
		{
			for (int i = 0; i < mPlayer.length; i++)
			{
				if (mPlayer[i] != null)
					mPlayer[i].deallocate();
				mPlayer[i] = null;
			}
		}
/*
		if (mSound != null)
		{
			for (int i = 0; i < mSound.length; i++)
			{
				mSound[i] = null;
				mSoundData[i] = null;
			}
		}*/
		nSoundCount = 0;
		nCurrentSound = -1;
		Utils.callGc();
	}



	public int loadSound(int name,int ms, int vol)
	{
		int ret = -1;

		if (nSoundCount >= MAX_SOUNDS)
		{
			Utils.Log("max sounds reached!");
			return -1;
		}

		if (ms > 5000)
		{
			try
			{
				InputStream in = Utils.openStream(name);
				nSoundType[nSoundCount] = SNDTYPE_MIDI;
				//nSoundPlayTime[nSoundCount] = (short)ms;
				mPlayer[nSoundCount] = Manager.createPlayer(in, "audio/midi");
				mPlayer[nSoundCount].realize();
				mPlayer[nSoundCount].prefetch();
				//mPlayer[nSoundCount].setLoopCount(1);
				//mPlayer[nSoundCount].addPlayerListener(this);
				System.gc();
				try { Thread.sleep(20); } catch (InterruptedException e) { }
				
				//all ok, give back snd index and increment snd count
				//Utils.Log("sound loaded " + name);
				ret = nSoundCount++;
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		else
		{
			try
			{
				
				//InputStream in = Utils.openStream((name - FileIds.MID_BASE) + FileIds.WAV_BASE);
				InputStream in = Utils.openStream(name); // TODO !!!!!!!!!!!
				
				nSoundType[nSoundCount] = SNDTYPE_WAV;
				//nSoundPlayTime[nSoundCount] = (short)ms;
				mPlayer[nSoundCount] = Manager.createPlayer(in, "audio/x-wav");
				//mPlayer[nSoundCount].realize();
				mPlayer[nSoundCount].prefetch();
				//mPlayer[nSoundCount].setLoopCount(1);
				//mPlayer[nSoundCount].addPlayerListener(this);
				System.gc();
				try { Thread.sleep(20); } catch (InterruptedException e) { }
				

				/*
				// recalculate ott id name from midi id nr from parameters
				int id = (name - FileIds.MID_BASE) + FileIds.WAV_BASE;
				// load OTA here
				//Utils.Log("load sound");
				nSoundType[nSoundCount] = SNDTYPE_OTA;
				mSoundData[nSoundCount] = Utils.loadByteArray(id);
				mSound[nSoundCount] = new Sound(mSoundData[nSoundCount], Sound.FORMAT_WAV);
				mSoundVolume[nSoundCount] = vol;
*/
				nSoundPlayTime[nSoundCount] = (short)ms;
				//all ok, give back snd index and increment snd count
				ret = nSoundCount++;
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}

		}

		return ret;
	}



	public void playSound(int sndidx, int loop)
	{
		if ((sndidx < 0) || (sndidx >= nSoundCount))
			return;

		if (!bIsEnabled)
			return;

		if (isPlaying())
			return;
		// stop sound if another one is playing
		//stopSound();

		if (nSoundType[sndidx] == SNDTYPE_MIDI)
		{
			try
			{
				if (nCurrentSound != sndidx)
					mPlayer[sndidx].setLoopCount(loop);
				//mPlayer[sndidx].prefetch();
				mPlayer[sndidx].start();
				bIsPlaying = true;
				nCurrentSound = sndidx;
				nCurrentLoop = loop;
				nStartPlayTime = System.currentTimeMillis();
				nPlayTime = nSoundPlayTime[sndidx];
				Utils.Log("PlaysMIDI: "+sndidx);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		else
		if (nSoundType[sndidx] == SNDTYPE_WAV)
		{
			try
			{
				if (nCurrentSound != sndidx)
					mPlayer[sndidx].setLoopCount(loop);
				mPlayer[sndidx].prefetch();
				mPlayer[sndidx].start();
				bIsPlaying = true;
				nCurrentSound = sndidx;
				nCurrentLoop = loop;
				nStartPlayTime = System.currentTimeMillis();
				nPlayTime = nSoundPlayTime[sndidx];
				Utils.Log("PlaysWAV: "+sndidx);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		else
		if (nSoundType[sndidx] == SNDTYPE_OTA)
		{
			/*
			if (loop == -1)
				loop = 0;

			// play OTA here
			if (nCurrentSound != sndidx)
				mSound[sndidx].setGain (mSoundVolume[sndidx]);
			
			mSound[sndidx].play(loop);
			bIsPlaying = true;
			nCurrentSound = sndidx;
			nCurrentLoop = loop;
			nStartPlayTime = System.currentTimeMillis();
			nPlayTime = nSoundPlayTime[sndidx];// + 250;
			*/
			Utils.Log("PlaysOTA: "+sndidx);
		}

	}



	public void stopSound()
	{
		if ((nCurrentSound < 0) || (nCurrentSound >= nSoundCount))
			return;

		if (!bIsEnabled)
			return;

		if ((nSoundType[nCurrentSound] == SNDTYPE_MIDI) || (nSoundType[nCurrentSound] == SNDTYPE_WAV))
		{
			try
			{
				if (mPlayer[nCurrentSound] != null)
				{
					mPlayer[nCurrentSound].stop();
					mPlayer[nCurrentSound].setMediaTime(0);
				}
			}
			catch (Exception e)
			{
			}
			//mPlayer[nCurrentSound] = null;
			Utils.Log("StopMIDI: "+nCurrentSound);
			nCurrentSound = -1;
			bIsPlaying = false;
			
		}
		else
		if (nSoundType[nCurrentSound] == SNDTYPE_OTA)
		{
			/*
			// stop OTA here
			if (mSound[nCurrentSound] != null)
			{
				mSound[nCurrentSound].stop();
			}*/
			Utils.Log("StopOTA: "+nCurrentSound);
			nCurrentSound = -1;
			bIsPlaying = false;
			
		}
	}



	public void enableSound()
	{
		bIsEnabled = true;
	}



	public void disableSound()
	{
		bIsEnabled = false;
		stopSound();
	}



	public boolean isPlaying()
	{
		if (nCurrentSound == -1)
		{
			bIsPlaying = false;
			return false;
		}
 
		if (nCurrentLoop != -1)
		{
			
			//Utils.Log("playtime: "+(System.currentTimeMillis() - nStartPlayTime)+ " / "+nPlayTime);
			if ((System.currentTimeMillis() - nStartPlayTime) > nPlayTime)
			{
				//stopSound();
				bIsPlaying = false;
			}
		}

		return bIsPlaying;
	}



	public static boolean isEnabled()
	{
		return bIsEnabled;
	}



	




}

⌨️ 快捷键说明

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