标签:android style blog http io color ar os 使用
使用SeekBar调节声音
SeekBar控件其实就是一个高级点的进度条,就像我们在听歌,看电影用的播放器上的进度条一样,是可以拖动的,可以改变进度的一个进度条控件!
SeekBar常用属性:
android:max[integer]//设置拖动条的最大值
android:progress[integer]//设置当前的进度值
android:secondaryProgress[integer]//设置第二进度,通常用做显示视频等的缓冲效果
android:thumb[drawable]//设置滑块的图样
android:progressDrawable[drawable]//设置进度条的图样
1.XML代码:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="SeekBar声音控制" /> 11 12 <SeekBar 13 android:id="@+id/seekbar" 14 android:layout_width="620px" 15 android:layout_height="wrap_content" 16 android:max="100" 17 android:progress="20" 18 android:progressDrawable="@drawable/soundbar" 19 android:thumb="@drawable/soundbutton" /> 20 21 <TextView 22 android:id="@+id/txtProgress" 23 android:layout_width="fill_parent" 24 android:layout_height="wrap_content" /> 25 26 27 <Button 28 android:id="@+id/btnStart" 29 android:layout_width="match_parent" 30 android:layout_height="wrap_content" 31 android:text="播放" > 32 </Button> 33 34 </LinearLayout>
2.java代码:
1 package com.example.seekbar; 2 3 import java.io.File; 4 import java.io.IOException; 5 import android.app.Activity; 6 import android.content.Context; 7 import android.media.AudioManager; 8 import android.media.MediaPlayer; 9 import android.os.Bundle; 10 import android.os.Environment; 11 import android.util.Log; 12 import android.view.KeyEvent; 13 import android.view.View; 14 import android.view.View.OnClickListener; 15 import android.widget.Button; 16 import android.widget.SeekBar; 17 import android.widget.SeekBar.OnSeekBarChangeListener; 18 import android.widget.TextView; 19 20 public class MainActivity extends Activity implements OnClickListener, 21 OnSeekBarChangeListener { 22 // 定义一个SeekBar和一个TextView 23 private SeekBar seekBar; 24 private TextView textView; 25 private Button btn; 26 private MediaPlayer mMediaPlayer; 27 28 @Override 29 public void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 33 /** 创建MediaPlayer对象 **/ 34 mMediaPlayer = new MediaPlayer(); 35 initView(); 36 } 37 38 // 初始化 39 private void initView() { 40 textView = (TextView) findViewById(R.id.txtProgress); 41 seekBar = (SeekBar) findViewById(R.id.seekbar); 42 btn = (Button) findViewById(R.id.btnStart); 43 seekBar.setMax(100); 44 seekBar.setOnSeekBarChangeListener(this); 45 btn.setOnClickListener(this); 46 } 47 48 private void play() throws IOException { 49 // 获取文件路径 ,放在SD卡下 50 File audioFile = new File(Environment.getExternalStorageDirectory(), 51 "welcome.wav"); 52 Log.i("PATH", audioFile.getAbsolutePath()); 53 mMediaPlayer.reset(); 54 mMediaPlayer.setDataSource(audioFile.getAbsolutePath()); 55 mMediaPlayer.prepare(); 56 mMediaPlayer.start(); 57 mMediaPlayer.setVolume(1f, 2f); 58 } 59 60 @Override 61 public boolean onKeyDown(int keyCode, KeyEvent event) { 62 if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN 63 || keyCode == KeyEvent.KEYCODE_1) { 64 AudioManager audioMa = (AudioManager) MainActivity.this 65 .getSystemService(Context.AUDIO_SERVICE); 66 audioMa.adjustStreamVolume(AudioManager.STREAM_MUSIC, 67 AudioManager.ADJUST_LOWER, 68 AudioManager.FX_FOCUS_NAVIGATION_UP); 69 } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP 70 || keyCode == KeyEvent.KEYCODE_2) { 71 AudioManager audioMa = (AudioManager) MainActivity.this 72 .getSystemService(Context.AUDIO_SERVICE); 73 audioMa.adjustStreamVolume(AudioManager.STREAM_MUSIC, 74 AudioManager.ADJUST_RAISE, 75 AudioManager.FX_FOCUS_NAVIGATION_UP); 76 } 77 return true; 78 } 79 80 @Override 81 public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { 82 int i = seekBar.getProgress(); 83 textView.setText("" + i); 84 85 } 86 87 @Override 88 public void onStartTrackingTouch(SeekBar arg0) { 89 // TODO Auto-generated method stub 90 91 } 92 93 @Override 94 public void onStopTrackingTouch(SeekBar arg0) { 95 int i = seekBar.getProgress(); 96 mMediaPlayer.setVolume(i / 100f, i / 100f); 97 98 } 99 100 @Override 101 public void onClick(View v) { 102 if (v.getId() == R.id.btnStart) { 103 try { 104 mMediaPlayer.prepare(); 105 } catch (Exception e) { 106 e.printStackTrace(); 107 } 108 try { 109 // 播放音效 110 if (!mMediaPlayer.isPlaying()) { 111 play(); 112 } else { 113 mMediaPlayer.stop(); 114 } 115 } catch (Exception e) { 116 e.printStackTrace(); 117 } 118 } 119 } 120 }
Android学习之------SeekBar(控制wav音频的声音)
标签:android style blog http io color ar os 使用
原文地址:http://www.cnblogs.com/summers/p/4081569.html