2
A
回答
9
只是我已经实现与背景音服务,我从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");
} }
检查你的代码从活动中使用
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();
}
}
}
谢谢所有谁帮我打电话。特别是谁写博客。
相关问题
- 1. 活动与声音池服务?
- 2. 声音池权限
- 3. Android和声音
- 4. 回声和噪音在声音webRTC android
- 5. 安卓中的声音池
- 6. 服务和电池消耗
- 7. TTS和播放声音 - Android
- 8. Android声音:seekTo和setPlaybackRate
- 9. Android录音,上传和播放声音
- 10. 按顺序播放声音池中的声音
- 11. iOS和Android上的超声波声音
- 12. 声音计android
- 13. 从服务人员播放声音
- 14. 与服务共享声音资源
- 15. Android:播放来自Kivy后台服务的声音
- 16. 如何在Android中使用服务播放声音
- 17. 开始通过Android的声音的服务?
- 18. 使用GCM服务的Android设备没有通知声音
- 19. 如何将声音从Android设备传输到Java服务器?
- 20. 声音效果的AS3对象池
- 21. 使用声音池的Android多点触摸
- 22. 服务检查电池百分比Android
- 23. Android声音扬声器
- 24. Android简单的方法来记录声音和流到服务器?
- 25. Android的声音流
- 26. 访问Android声音
- 27. Android单击声音
- 28. android:默认声音
- 29. Android声音验证
- 30. Android声音问题