2010-11-13 53 views
2

是否可以检查“声音库”的状态。我想在启动和停止时执行一些功能。 like; mediaplayer的函数“isplaying()”。健全池已这种类型的功能...Android声音库功能

回答

-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(); 
} 
+0

我没有发现这个答案解决了这个问题。他希望(也是我)知道如何查询流的状态。 – Yuyo 2011-05-17 22:07:04

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