1.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="wyf.ytl.MainActivity" >    <TextView        android:id="@+id/textView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="没有播放任何声音" />    <Button         android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="使用MediaPlayer播放声音"        />        <Button         android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="暂停MediaPlayer声音"        />        <Button         android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="使用SoundPool播放声音"        />        <Button         android:id="@+id/button4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="暂停SoundPool声音"        />    </LinearLayout>

2.

public class MainActivity extends ActionBarActivity implements android.view.View.OnClickListener{Button button1;Button button2;Button button3;Button button4;TextView textView;MediaPlayer mMediaPlayer;SoundPool soundPool;HashMap<Integer, Integer> soundPoolMap;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initSounds();textView = (TextView) findViewById(R.id.textView);button1 = (Button) findViewById(R.id.button1);button2 = (Button) findViewById(R.id.button2);button3 = (Button) findViewById(R.id.button3);button4 = (Button) findViewById(R.id.button4);button1.setOnClickListener(this);button2.setOnClickListener(this);button3.setOnClickListener(this);button4.setOnClickListener(this);}public void initSounds(){//初始化mediaplayermMediaPlayer = MediaPlayer.create(this, R.raw.backsound);soundPool = new SoundPool(4,AudioManager.STREAM_MUSIC, 100);soundPoolMap = new HashMap<Integer, Integer>();soundPoolMap.put(1, soundPool.load(this, R.raw.dingdong, 1));}//用SoundPool播放声音的方法public void playSound(int sound,int loop) {AudioManager mgr = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);float volume = streamVolumeCurrent/streamVolumeMax;soundPool.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f);}@Overridepublic void onClick(View v) {int key = v.getId();switch (key) {case R.id.button1:textView.setText("use MediaPlayer to play sound");if(!mMediaPlayer.isPlaying()){mMediaPlayer.start();}break;case R.id.button2:textView.setText("suspend MediaPlayer's sound ");if(mMediaPlayer.isPlaying()){mMediaPlayer.pause();}break;case R.id.button3:textView.setText("use SoundPool to play sound");this.playSound(1, 0);break;case R.id.button4:textView.setText("suspend the SoundPool's sound");soundPool.pause(1);break;default:break;}}}

更多相关文章

  1. Android自动播放图片功能实现
  2. android播放音乐文件代码
  3. android 播放音频和视频
  4. Android(安卓)触屏播放音效与释放
  5. android音乐播放器源码java类
  6. Android应用实例之----基于Service与ContentProvider的音乐播放
  7. android 自定义view支持gif格式播放
  8. Android利用VideoView实现VideoPlayer
  9. 使用android MediaPlayer播放音频文件时,有时会出现prepareasync

随机推荐

  1. Android 获取设备宽高分辨率
  2. MVP+RxJava+Dagger打造的Android(安卓)Al
  3. Android 计时器 分:秒:毫秒 http://downloa
  4. Android(安卓)UI Operation in Thread
  5. Android - HTTP util class SyncGET & Sy
  6. Android拒绝来电的实现--ITelephony类的
  7. Android 中的看门狗—Watchdog
  8. android studio 报错 Lint found fatal e
  9. 45 Android activity 向 fragment 传值
  10. Android 导出数据库到Excel表格功能