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

Posted SweetLoverFT

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阅读《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>

以上是关于阅读《Android 从入门到精通》(18)——拖动条的主要内容,如果未能解决你的问题,请参考以下文章

阅读《Android 从入门到精通》(31)——Intent

阅读《Android 从入门到精通》(20)——图片视图

阅读《Android 从入门到精通》(30)——字体

阅读《Android 从入门到精通》(16)——表状时钟

阅读《Android 从入门到精通》(29)——四大布局

阅读《Android 从入门到精通》(29)——四大布局