是否可以检查“声音池”的状态。我想在它启动和停止时执行一些功能。喜欢; mediaplayer的功能“isplaying()”。声音池有这种功能......
答案 0 :(得分:0)
不幸的是,SoundPool API似乎没有提供该功能;它可以让你开始和停止声音,但没有方法来检查它的状态。在我的一个应用程序中,我基本上通过维护我自己的布尔值来攻击它,我在启动声音时设置为true,然后在我停止它或经过足够的时间时设置为false。
//your code to start sound here
long soundStartTime = System.currentTimeMillis();
soundPlaying=true;
//other code here
if(System.currentTimeMillis()-soundStartTime > SOUND_LENGTH_MILLIS){
soundPlaying=false;
//also set false at beginning of level and whenever you stop it manually
答案 1 :(得分:-1)
参考此代码..
SoundPoolManager Class
package com.demosoft.music;
import android.media.SoundPool;
import android.media.JetPlayer;
class SoundPoolEvent
{
public SoundPoolEvent(int eventType,int eventSound)
{
this.eventType = eventType;
this.eventSound = eventSound;
}
public int eventType;
public int eventSound;
public static final int SOUND_PLAY=0;
public static final int SOUND_STOP=1;
public static final int SOUND_MUSIC_PLAY=2;
public static final int SOUND_MUSIC_PAUSE=3;
public static final int SOUND_MUSIC_STOP=4;
public static final int SOUND_MUSIC_RESUME=5;
}
class SoundStatus
{
public SoundStatus()
{
}
public static final int STATUS_LOOPING_NOT_STARTED=0;
public static final int STATUS_LOOPING_PAUSED=1;
public static final int STATUS_LOOPING_PLAYING=2;
}
public class SoundPoolManager implements Sound
{
SoundPoolManager(android.content.Context context)
{
this.context = context;
soundEvents = new java.util.LinkedList();
sounds = new java.util.HashMap();
handles = new java.util.HashMap();
streamIds = new java.util.HashMap();
isRunning = false;
finished = false;
this.musicPlayer =JetPlayer.getJetPlayer();
this.musicPlayer.loadJetFile(context.getResources().openRawResourceFd(R.raw.notify));
byte segmentId = 0;
this.musicPlayer.queueJetSegment(0, -1, -1, 0, 0, segmentId++);
}
public void addSound(int resid, boolean isLooping)
{
sounds.put(resid, new Boolean(isLooping));
}
public void startSound()
{
this.soundPool = new android.media.SoundPool(this.sounds.size(),android.media.AudioManager.STREAM_MUSIC,100);
java.util.Iterator iterator = sounds.keySet().iterator();
while(iterator.hasNext())
{
int soundid = iterator.next().intValue();
int soundhandle = this.soundPool.load(this.context, soundid, 1);
handles.put(new Integer(soundid), new Integer(soundhandle));
}
}
public void stopSound()
{
try
{
java.util.Iterator iterator = sounds.keySet().iterator();
while(iterator.hasNext())
{
int soundid = iterator.next().intValue();
this.soundPool.pause( this.handles.get(soundid).intValue());
this.soundPool.stop(this.handles.get(soundid).intValue());
}
}
catch(Exception e)
{
}
finally
{
try
{
this.musicPlayer.pause();
}
catch(Exception e1)
{
}
try
{
this.musicPlayer.release();
}
catch(Exception e2)
{
}
try
{
this.soundPool.release();
}
catch(Exception e3)
{
}
}
}
public int currentPlayer;
private boolean isRunning;
private java.util.HashMap sounds;
private java.util.HashMap handles;
private java.util.HashMap streamIds;
private android.content.Context context;
private java.util.LinkedList soundEvents;
private java.util.HashMap mediaPlayers;
public void stopSound(int resid)
{
}
public void playSound(int resid)
{
if(soundEvents!=null)
{
try
{
android.media.AudioManager mgr = (android.media.AudioManager) context.getSystemService(android.content.Context.AUDIO_SERVICE);
int streamVolume = mgr.getStreamVolume(android.media.AudioManager.STREAM_MUSIC);
int streamID = soundPool.play(handles.get( resid).intValue(), streamVolume, streamVolume, 1, 0, 1.0f);
int maxvolume = mgr.getStreamMaxVolume(android.media.AudioManager.STREAM_MUSIC);
mgr.setStreamVolume(android.media.AudioManager.STREAM_MUSIC, maxvolume, 0);
this.streamIds.put(resid, streamID);
}
catch(Exception e)
{
}
}
}
public void startMusic(int resid)
{
this.musicPlayer.play();
}
public void stopMusic(int resid)
{
this.musicPlayer.pause();
}
public void pauseMusic(int resid)
{
this.musicPlayer.pause();
}
public void resumeMusic(int resid)
{
this.musicPlayer.play();
}
SoundPool soundPool;
JetPlayer musicPlayer;
boolean finished = false;
}
And add the interface of Sound..... to the above package
package com.demosoft.music;
public interface Sound {
public void addSound(int resid, boolean isLooping);
public void startSound();
public void stopSound();
public void stopSound(int resid);
public void playSound(int resid);
public void startMusic(int resid);
public void stopMusic(int resid);
public void pauseMusic(int resid);
public void resumeMusic(int resid);
}
And u can use in difffernt ways ....
1. creating the soundpoolmanager instance class ......
2. by using Sound interface instance,....
SoundPoolManager m = new SoundPoolManager(context);
m.addSound(R.raw.demo, false);
m.addSound(R.raw.soft,true);
m.startSound();
m.playSound(R.raw.demo);
m.playMusic(R.raw.soft);
or
private Sound soundManager;
soundManager.playSound(R.raw.demo);
public synchronized void stopMusic()
{
soundManager.stopSound();
//message.sendToTarget();
this.soundManager.stopSound();
}