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中动态初始化布局参数以及ConstraintLayout使用中遇到的
  3. Android播放ogg声音文件
  4. Android 8 声音调整过程
  5. Android获取声音长度
  6. Android GPS学习笔记—LMS初始化
  7. 修改Android开机图片、动画、声音
  8. android kernel 初始化 2

随机推荐

  1. Android如何从服务器获取图片
  2. Flex AIR应用拍照功能(Android和IOS版本)
  3. Android 图形密码
  4. Android Studio 错误 com.android.dex.De
  5. Android 获取网络时间
  6. 复选框
  7. Android 图片倒影效果源码
  8. android嵌入式底层开发
  9. 使用Android studio分析内存泄露
  10. The android SDK requaires Android Deve