src\ MainActivity.java
package com.example.soundpoolandvirator; import java.util.HashMap; import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle; import android.os.Vibrator; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { Button bRingStart; Button bRingPause; //震动 Button bVStart; Button bVStop; Button bVForever; private Vibrator vibrator;//振动器 private SoundPool sp; private HashMap<Integer, Integer> spMap; /** * 震动模式(强度) * {100,400,100,400} 停止 开启 停止 开启 */ public static final long[] PATTERN_1 = {100,400,100,400}; public static final long[] PATTERN_2 = {100,700,100,700}; public static final long[] PATTERN_3 = {300,900,300,900}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initWidget(); initSoundPool(); // 点击监听 bRingStart.setOnClickListener(this); bRingPause.setOnClickListener(this); bVStart.setOnClickListener(this); bVStop.setOnClickListener(this); bVForever.setOnClickListener(this); } private void initWidget() { bRingStart = (Button) findViewById(R.id.bstart); bRingPause = (Button) findViewById(R.id.bpause); bVStart = (Button) findViewById(R.id.but_on_vibrate); bVStop = (Button) findViewById(R.id.but_off_vibrate); bVForever = (Button) findViewById(R.id.but_forever_vibrate); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { int viewID = v.getId(); switch (viewID) { //播放音效 case R.id.bstart: playSounds(1, 1); break; //暂停声音 case R.id.bpause: sp.pause(spMap.get(1)); break; case R.id.but_on_vibrate: openVibrate(PATTERN_3, -1); break; case R.id.but_off_vibrate: stopVibrate(); break; case R.id.but_forever_vibrate: openVibrate(PATTERN_2, 2); break; default: break; } } /** * 初始化SoundPool */ @SuppressLint("UseSparseArrays") private void initSoundPool() { sp = new SoundPool(2, AudioManager.STREAM_MUSIC, 5); spMap = new HashMap<Integer, Integer>(); spMap.put(1, sp.load(this, R.raw.water, 1)); } /** * 播放音频 * * @param sound * 加载的音频(spMap中的key) * @param number * 循环次数 (0 = no loop, -1 = loop forever) */ public void playSounds(int sound, int number) { AudioManager am = (AudioManager) this .getSystemService(MainActivity.AUDIO_SERVICE); float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC); float audioCurrentVolumn = am .getStreamVolume(AudioManager.STREAM_MUSIC); float volumnRatio = audioCurrentVolumn / audioMaxVolumn; sp.play(spMap.get(sound), volumnRatio / 2, volumnRatio / 2, 1, number, 1); } /** * 打开 手机震动 * 震动大小可以通过改变pattern来设定,如果开启时间太短,震动效果可能感觉不到 * @param pattern 模式 {100,400,100,400}; // 停止 开启 停止 开启 * @param repeat 重复次数 */ public void openVibrate(long[] pattern , int repeat){ vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(pattern, repeat); } /** * 取消震动 */ public void stopVibrate(){ vibrator.cancel(); } }
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/bstart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="24dp" android:layout_marginTop="14dp" android:text=" 播放" /> <Button android:id="@+id/but_on_vibrate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/bstart" android:layout_below="@+id/bstart" android:layout_marginTop="28dp" android:text="打开震动" /> <Button android:id="@+id/bpause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/bstart" android:layout_alignBottom="@+id/bstart" android:layout_alignParentRight="true" android:layout_marginRight="54dp" android:text="暂停" /> <Button android:id="@+id/but_off_vibrate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/but_on_vibrate" android:layout_alignLeft="@+id/bpause" android:text="关闭震动" /> <Button android:id="@+id/but_forever_vibrate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/but_on_vibrate" android:layout_below="@+id/but_on_vibrate" android:layout_marginTop="36dp" android:text="永久震动" /> </RelativeLayout>
res\rew 点击打开链接(提取码:24ad)