阅读《Android 从入门到精通》(18)——拖动条

拖动条(SeekBar)

android.widget.ProgressBar;
android.widget.AbsSeekBar;
android.widget.SeekBar;

SeekBar 类方法


SeekBar 示例

完整工程:http://download.csdn.net/detail/sweetloveft/9419276

下面演示了 SeekBar 调节音量的功能,因为 SeekBar 继承于 ProgressBar,因此它也有 SetMax 等方法。

1.MainActivity.java

package com.sweetlover.activity;

import com.sweetlover.audiorecord.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;

public class MainActivity extends Activity {
	Button recordButton, stopButton, exitButton;
	SeekBar skbVolume;
	boolean isRecording = false;
	static final int frequency = 44100;
	@SuppressWarnings("deprecation")
	static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
	static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
	int recBufSize, playBufSize;
	AudioRecord audioRecord;
	AudioTrack audioTrack;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 用getMinBufferSize()方法得到采集数据所需要的最小缓冲区的大小
		recBufSize = AudioRecord.getMinBufferSize(frequency,
				channelConfiguration, audioEncoding);
		playBufSize = AudioTrack.getMinBufferSize(frequency,
				channelConfiguration, audioEncoding);
		// 实例化AudioRecord(声音来源,采样率,声道设置,采样声音编码,缓存大小)
		audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,
				channelConfiguration, audioEncoding, recBufSize);
		audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency,
				channelConfiguration, audioEncoding, playBufSize,
				AudioTrack.MODE_STREAM);
		recordButton = (Button) this.findViewById(R.id.recordbutton);
		stopButton = (Button) this.findViewById(R.id.stopbutton);
		exitButton = (Button) this.findViewById(R.id.exitbutton);
		recordButton.setOnClickListener(new ClickEvent());
		stopButton.setOnClickListener(new ClickEvent());
		exitButton.setOnClickListener(new ClickEvent());
		skbVolume = (SeekBar) this.findViewById(R.id.seekBarVolume);
		skbVolume.setMax(100);
		skbVolume.setProgress(70);
		// 设置声音大小
		audioTrack.setStereoVolume(0.7f, 0.7f);
		skbVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

					public void onStopTrackingTouch(SeekBar seekBar) {
						float vol = (float) (seekBar.getProgress())
								/ (float) (seekBar.getMax());
						audioTrack.setStereoVolume(vol, vol);

					}

					@Override
					public void onStartTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onProgressChanged(SeekBar seekBar,
							int progress, boolean fromUser) {
						// TODO Auto-generated method stub

					}
				});
	}

	protected void onDestroy() {
		super.onDestroy();
		// 杀死当前进程
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	class ClickEvent implements View.OnClickListener {
		public void onClick(View v) {
			if (v == recordButton) {
				isRecording = true;
				new RecordPlayThread().start();
			} else if (v == stopButton) {
				isRecording = false;
			} else if (v == exitButton) {
				isRecording = false;
				MainActivity.this.finish();
			}
		}
	}

	class RecordPlayThread extends Thread {
		@SuppressLint("ShowToast")
		public void run() {
			try {
				// byte 文件来存储声音
				byte[] buffer = new byte[recBufSize];
				// 开始采集声音
				audioRecord.startRecording();
				// 播放声音
				audioTrack.play();
				while (isRecording) {
					// 从MIC存储到缓存区
					int bufferReadResult = audioRecord.read(buffer, 0,
							recBufSize);
					byte[] tmpBuf = new byte[bufferReadResult];
					System.arraycopy(buffer, 0, tmpBuf, 0, bufferReadResult);
					// 播放缓存区的数据
					audioTrack.write(tmpBuf, 0, tmpBuf.length);

				}
				audioTrack.stop();
				audioRecord.stop();
			} catch (Throwable t) {
				Toast.makeText(MainActivity.this, t.getMessage(), 1000);
			}
		}
	};
}

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewOper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/operator"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/recordbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/action" />

    <Button
        android:id="@+id/stopbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/stop" />

    <Button
        android:id="@+id/exitbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/exit" />

    <TextView
        android:id="@+id/textViewAdjust"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/adjust"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <SeekBar
        android:id="@+id/seekBarVolume"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

3.string.xml

<resources>

    <string name="app_name">SeekBarDemo</string>
    <string name="operator">录制操作</string>
    <string name="action">开始边录边放</string>
    <string name="stop">停止</string>
    <string name="exit">退出</string>
    <string name="adjust">音量调节</string>

</resources>

4.AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sweetlover.audiorecord"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.sweetlover.activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值