Android游戏开发学习笔记(二):音频的播放(2)
来源:未知 责任编辑:责任编辑 发表时间:2014-01-25 11:34 点击:次
import android.widget.TextView;
public class MainActivity extends Activity {
Button btn1, btn2, btn3, btn4;
TextView tv;
MediaPlayer media;
SoundPool sound;
HashMap<Integer, Integer> soundMap;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initSounds();
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn1.setOnClickListener(btn_listener);
btn2.setOnClickListener(btn_listener);
btn3.setOnClickListener(btn_listener);
btn4.setOnClickListener(btn_listener);
}
private void initSounds() {
// TODO Auto-generated method stub
media = MediaPlayer.create(this, R.raw.shadow);
sound = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundMap = new HashMap<Integer, Integer>();
soundMap.put(1, sound.load(this, R.raw.shake, 1));
}
private OnClickListener btn_listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
tv.setText("使用MediaPlayer播放声音");
if (!media.isPlaying())
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>