android播放器音量控制-使用震动示例代码-进度条控制音量
在写程序的时候我们会使用到声音通知下面代码就是实现该行为:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
// mediaPlayer.setOnCompletionListener(beepListener);
AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(),
file.getLength());
file.close();
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
} catch (IOException e) {
mediaPlayer = null;
}
mediaPlayer.start();
下面代码是使用系统的震动:
/提示
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
long[] pattern = {800, 50, 400, 30}; // OFF/ON/OFF/ON...
vibrator.vibrate(pattern, 2);//-1不重复,非-1为从pattern的指定下标开始重复
关于系统的音量的问题有以下参数:
136 /** The audio stream for system sounds */
137 public static final int STREAM_SYSTEM = AudioSystem.STREAM_SYSTEM;
138 /** The audio stream for the phone ring */
139 public static final int STREAM_RING = AudioSystem.STREAM_RING;
140 /** The audio stream for music playback */
141 public static final int STREAM_MUSIC = AudioSystem.STREAM_MUSIC;
如果想使用进度框来调节的话有下面代码:
MediaPlayer mp = MediaPlayer.create(context, R.raw.recv);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mp.setVolume(maxVolume * mLastProgress / 10 , maxVolume * mLastProgress / 10 );
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume * mLastProgress / 10 , 0 );
更多相关文章
- android怎么将已经安装在内存上的应用程序移动到SD卡上
- android 动态注册 广播
- android中webView中对HTML的一些操作【更新代码】
- 常用的Android(安卓)Widget组件学习①--Button and TextView
- android典型代码系列(十一)------判断某个应用程序是 不是三方的
- IntelliJ IDEA破解版 2017.3.4 Ultimate下载安装和JetbrainsCrac
- Android(安卓)调用系统短信 - 群发
- Android设置Activity全屏和无标题
- 转 20个Android很有用的代码片段