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,Stream,service,System,audio,playback)