
我已经创建了实现媒体播放器类的服务来播放背景音乐.但是现在我想转换成声音池以便可以播放多个声音.
可以帮助任何可以为我提供源代码的一些链接?
提前致谢.
解决方法:
我刚用背景音实现了服务,我已经检查了http://android-er.blogspot.com/2010/11/play-audio-resources-using-soundpool.html和http://developer.android.com/guide/topics/fundamentals/bound-services.html的代码
public class LocalService extends Service { // Binder given to clIEnts private final IBinder mBinder = new LocalBinder(); // Random number generator private final Random mGenerator = new Random(); private SoundPool soundPool; private HashMap<Integer, Integer> soundsMap; int SOUND1=1; int SOUND2=2; @OverrIDe public voID onCreate() { // Todo auto-generated method stub super.onCreate(); soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100); soundsMap = new HashMap<Integer, Integer>(); soundsMap.put(SOUND1, soundPool.load(this, R.raw.baby_laugh, 1)); soundsMap.put(SOUND2, soundPool.load(this, R.raw.touchdown, 1)); } public voID playSound(int sound, float fSpeed) { AudioManager mgr = (AudioManager)getSystemService(Context.AUdio_SERVICE); float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC); float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC); float volume = streamVolumeCurrent / streamVolumeMax; soundPool.play(soundsMap.get(sound), volume, volume, 1, 0, fSpeed); } /** * Class used for the clIEnt Binder. Because we kNow this service always * runs in the same process as its clIEnts, we don't need to deal with IPC. */ public class LocalBinder extends Binder { LocalService getService() { // Return this instance of LocalService so clIEnts can call public methods return LocalService.this; } } @OverrIDe public IBinder onBind(Intent intent) { return mBinder; } /** method for clIEnts */ public int getRandomNumber() { return mGenerator.nextInt(100); } public voID soundplay(int index){ playSound(index, 1.0f); Log.d("SOUND1","hi1"); } }你从Activity调用
public class Test extends Activity implements OnClickListener { LocalService mService; boolean mBound = false; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); button btn=(button)findVIEwByID(R.ID.a_button); btn.setonClickListener(this); } @OverrIDe protected voID onStart() { super.onStart(); // Bind to LocalService Intent intent = new Intent(this, LocalService.class); bindService(intent, mConnection, Context.BIND_auto_CREATE); } @OverrIDe protected voID onStop() { super.onStop(); // Unbind from the service if (mBound) { unbindService(mConnection); mBound = false; } } /** Called when a button is clicked (the button in the layout file attaches to * this method with the androID:onClick attribute) */ public voID onbuttonClick(VIEw v) { if (mBound) { // Call a method from the LocalService. // However, if this call were something that might hang, then this request should // occur in a separate thread to avoID slowing down the activity performance. int num = mService.getRandomNumber(); Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show(); } } /** defines callbacks for service binding, passed to bindService() */ private ServiceConnection mConnection = new ServiceConnection() { public voID onServiceConnected(Componentname classname, IBinder service) { // We've bound to LocalService, cast the IBinder and get LocalService instance LocalBinder binder = (LocalBinder) service; mService = binder.getService(); mBound = true; } public voID onServicedisconnected(Componentname arg0) { mBound = false; } }; public voID onClick(VIEw arg0) { if (mBound) { // Call a method from the LocalService. // However, if this call were something that might hang, then this request should // occur in a separate thread to avoID slowing down the activity performance. mService.soundplay(1); int num = mService.getRandomNumber(); Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show(); } }}感谢所有帮助我的人.特别是谁写博客.
总结以上是内存溢出为你收集整理的Android:声音池和服务全部内容,希望文章能够帮你解决Android:声音池和服务所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)