android声音服务启动,Android:声音池和服务

我已经创建了实现媒体播放器类的服务来播放背景音乐.但是现在我想转换成声音池以便可以播放多个声音.

可以帮助任何可以为我提供源代码的一些链接?

提前致谢.

解决方法:

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 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();

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-service

来源: https://codeday.me/bug/20190928/1825665.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值