AudioRecorder功能
AudioRecorder這個物件可以設定錄音的品質(頻率、取樣數、聲道)
但是錄出來的資料是原始音頻檔(PCM)
後續還要加Header將其轉換成可播放的格式,例如WAV
下面是使用AudioRecorder做錄音的範例
在範例中,AudioRecorder需要放在另一個線程(Thread)執行
// 按鈕操作
public void onClick(View v) {
// Press start record
if (v.getId() == R.id.recStart) {
isRecording = true;
thread = new Thread(new Runnable() {
public void run() {
record();
}
});
thread.start();
}
// Press stop record else if (v.getId() == R.id.recStop) {
isRecording = false;
// 可以用"thread.join()"讓thread進入blocked狀態,目前直接在結束的地方寫"thread.stop();"
// try {
// thread.join();
// } catch (InterruptedExpection e) { }
}
}
private void record() {
pcmFile = new File(myDataPath.getAbsolutePath() + "/record.pcm");
// Delete any previous recording.
if (pcmFile.exists())
pcmFile.delete();
// Create the new file.
try {
pcmFile.createNewFile();
} catch (IOException e) {
throw new IllegalStateException("Failed to create " + pcmFile.toString());
}
// Start record pcm data try {
// Create a DataOuputStream to write the audio data into the saved file. OutputStream os = new FileOutputStream(pcmFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
// Create a new AudioRecord object to record the audio.
int bufferSize =
AudioRecord.getMinBufferSize(
sampleRateInHz,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
AudioRecord audioRecord =
new AudioRecord(
MediaRecorder.AudioSource.MIC,
sampleRateInHz,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSize);
byte[] buffer = new byte[bufferSize];
audioRecord.startRecording();
while (isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
for (int i = 0; i < bufferReadResult; i++)
dos.write(buffer[i]);
}
// filler, data size need can %8 = 0 if (dos.size()%8 != 0) {
int filler = 0;
filler = 8 - (dos.size()%8);
for (int i = 0 ; i < filler ; i++) {
dos.writeByte(0);
}
}
// stop record and close the file. audioRecord.stop();
dos.close();
} catch (Throwable t) { }
// stop thread, this method may be not great, we can use the "thread.join( )" method
thread.stop();
}
2011年5月27日 星期五
2011年4月20日 星期三
FingerPaint功能
FingerPaint功能
下面為一繪圖的範例
public class Project1 extends Activity {
private Bitmap mBitmap;
private MyView mView;
public void onCreate (Bundle bundle) {
super.onCreate(bundle);
// remove the title, and display "full screen"
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// get the windows resolution, for bitmap use
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// create a new bitmap for paint
mBitmap = Bitmap.createBitmap(dm.widthPixels, dm.heightPixels, Bitmap.Config.ARGB_8888);
// use "MyView" class, this is define by author
mView = new MyView(this);
setContentView(mView);
}
private class MyView extends View {
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;
private float new_x, new_y;
private float old_x, old_y;
public MyView(Context context) {
super(context);
// create and initial object
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPaint = new Paint();
// make paint more smooth
mPaint.setAntiAlias(true);
mPaint.setDither(true);
// (1.FILL 2.FILL_AND_STROKE 3.STROKE)
mPaint.setStyle(Paint.Style.STROKE);
// (1.BEVEL 2.MITER 3.ROUND) default is MITER
mPaint.setStrokeJoin(Paint.Join.ROUND);
// (1.BUTT 2.ROUND 3.SQUARE) default is BUIT
mPaint.setStrokeCap(Paint.Cap.ROUND);
// if set "0", will be a line
mPaint.setStrokeWidth(12);
mPaint.setColor(Color.GREEN);
}
protected void onDraw(Canvas canvas) {
// set background color
canvas.drawColor(Color.WHITE);
// draw the specified bitmap
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
// the path will be filled or framed based on the Style in the paint
canvas.drawPath(mPath, mPaint);
}
public boolean onTouchEvent(MotionEvent event) {
new_x = event.getX();
new_y = event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// save the previous path, then reset the path
mPath.reset();
// start position
mPath.moveTo(new_x, new_y);
old_x = new_x;
old_y = new_y;
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
float dx = Math.abs(new_x - old_x);
float dy = Math.abs(new_y - old_y);
if (dx >= 4 || dy >= 4) {
// set path from (old_x, old_y) to ((new_x + old_x)/2, (new_y + old_y)/2)
mPath.quadTo(old_x, old_y, (new_x + old_x)/2, (new_y + old_y)/2);
old_x = new_x;
old_y = new_y;
}
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
// unknown, this line like no used
mPath.lineTo(old_x, old_y);
// paint the path when "ACTION_UP"
mCanvas.drawPath(mPath, mPaint);
// save the paint
mPath.reset();
}
// update the paint, it will recall the onDraw function
invalidate();
return true;
}
}
}
下面為一繪圖的範例
public class Project1 extends Activity {
private Bitmap mBitmap;
private MyView mView;
public void onCreate (Bundle bundle) {
super.onCreate(bundle);
// remove the title, and display "full screen"
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// get the windows resolution, for bitmap use
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// create a new bitmap for paint
mBitmap = Bitmap.createBitmap(dm.widthPixels, dm.heightPixels, Bitmap.Config.ARGB_8888);
// use "MyView" class, this is define by author
mView = new MyView(this);
setContentView(mView);
}
private class MyView extends View {
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;
private float new_x, new_y;
private float old_x, old_y;
public MyView(Context context) {
super(context);
// create and initial object
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPaint = new Paint();
// make paint more smooth
mPaint.setAntiAlias(true);
mPaint.setDither(true);
// (1.FILL 2.FILL_AND_STROKE 3.STROKE)
mPaint.setStyle(Paint.Style.STROKE);
// (1.BEVEL 2.MITER 3.ROUND) default is MITER
mPaint.setStrokeJoin(Paint.Join.ROUND);
// (1.BUTT 2.ROUND 3.SQUARE) default is BUIT
mPaint.setStrokeCap(Paint.Cap.ROUND);
// if set "0", will be a line
mPaint.setStrokeWidth(12);
mPaint.setColor(Color.GREEN);
}
protected void onDraw(Canvas canvas) {
// set background color
canvas.drawColor(Color.WHITE);
// draw the specified bitmap
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
// the path will be filled or framed based on the Style in the paint
canvas.drawPath(mPath, mPaint);
}
public boolean onTouchEvent(MotionEvent event) {
new_x = event.getX();
new_y = event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// save the previous path, then reset the path
mPath.reset();
// start position
mPath.moveTo(new_x, new_y);
old_x = new_x;
old_y = new_y;
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
float dx = Math.abs(new_x - old_x);
float dy = Math.abs(new_y - old_y);
if (dx >= 4 || dy >= 4) {
// set path from (old_x, old_y) to ((new_x + old_x)/2, (new_y + old_y)/2)
mPath.quadTo(old_x, old_y, (new_x + old_x)/2, (new_y + old_y)/2);
old_x = new_x;
old_y = new_y;
}
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
// unknown, this line like no used
mPath.lineTo(old_x, old_y);
// paint the path when "ACTION_UP"
mCanvas.drawPath(mPath, mPaint);
// save the paint
mPath.reset();
}
// update the paint, it will recall the onDraw function
invalidate();
return true;
}
}
}
2011年4月18日 星期一
WiFi功能
WiFi功能
這邊簡單介紹WIFI開關的功能
public class wifi extends Activity implements OnCheckedChangeListener{
private WifiManager wifiManager;
private WifiInfo wifiInfo;
private CheckBox chkOpenCloseWifiBox;
private List<WifiConfiguration> wifiConfigurations;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
// 獲得WifiManager對象
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// 獲得連接訊息
wifiInfo = wifiManager.getConnectionInfo();
chkOpenCloseWifiBox = (CheckBox) findViewById(R.id.chkOpenCloseWifi);
TextView tvWifiConfigurations = (TextView) findViewById(R.id.tvWifiConfigurations);
TextView tvWifiInfo = (TextView) findViewById(R.id.tvWifiInfo);
chkOpenCloseWifiBox.setOnCheckedChangeListener(this);
// 根據目前WiFi狀態設置輸出訊息
if (wifiManager.isWifiEnabled()) {
chkOpenCloseWifiBox.setText("Wifi opened");
chkOpenCloseWifiBox.setChecked(true);
}
else {
chkOpenCloseWifiBox.setText("Wifi closed");
chkOpenCloseWifiBox.setChecked(false);
}
// 輸出WIFI訊息
StringBuffer sb = new StringBuffer();
sb.append("Wifi Info\n");
sb.append("MAC Address:" + wifiInfo.getMacAddress() + "\n");
sb.append("SSID:" + wifiInfo.getSSID() + "\n");
sb.append("IP Address(int):" + wifiInfo.getIpAddress() + "\n");
sb.append("IP Address(Hex):" + Integer.toHexString(wifiInfo.getIpAddress()) + "\n");
sb.append("IP Address:" + ipIntToString(wifiInfo.getIpAddress()) + "\n");
sb.append("Network ID:" + wifiInfo.getNetworkId() + "\n");
sb.append("Network Speed:" + Integer.toString(wifiInfo.getLinkSpeed()) + "\n");
sb.append("Signal Strength:" + Integer.toString(wifiInfo.getRssi()) + "\n");
tvWifiInfo.setText(sb.toString());
// 得到配置好的網路
wifiConfigurations = wifiManager.getConfiguredNetworks();
tvWifiConfigurations.setText("Had connected wifi\n");
for (WifiConfiguration wifiConfiguration : wifiConfigurations) {
tvWifiConfigurations.setText(tvWifiConfigurations.getText() + wifiConfiguration.SSID + "\n");
}
}
// 將int型態的IP轉換成文字型態
private String ipIntToString(int ip) {
try {
byte[] bytes = new byte[4];
bytes[0] = (byte) (0xff & ip);
bytes[1] = (byte) ((0xff00 & ip) >> 8);
bytes[2] = (byte) ((0xff0000 & ip) >> 16);
bytes[3] = (byte) ((0xff000000 & ip) >> 24);
return Inet4Address.getByAddress(bytes).getHostAddress();
}
catch (Exception e) {
return "";
}
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 當選中複選框時打開WiFi
if (isChecked) {
wifiManager.setWifiEnabled(true);
chkOpenCloseWifiBox.setText("Wifi opened");
}
// 當取消複選框時關閉WiFi
else {
wifiManager.setWifiEnabled(false);
chkOpenCloseWifiBox.setText("Wifi closed");
}
}
}
這邊簡單介紹WIFI開關的功能
public class wifi extends Activity implements OnCheckedChangeListener{
private WifiManager wifiManager;
private WifiInfo wifiInfo;
private CheckBox chkOpenCloseWifiBox;
private List<WifiConfiguration> wifiConfigurations;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
// 獲得WifiManager對象
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// 獲得連接訊息
wifiInfo = wifiManager.getConnectionInfo();
chkOpenCloseWifiBox = (CheckBox) findViewById(R.id.chkOpenCloseWifi);
TextView tvWifiConfigurations = (TextView) findViewById(R.id.tvWifiConfigurations);
TextView tvWifiInfo = (TextView) findViewById(R.id.tvWifiInfo);
chkOpenCloseWifiBox.setOnCheckedChangeListener(this);
// 根據目前WiFi狀態設置輸出訊息
if (wifiManager.isWifiEnabled()) {
chkOpenCloseWifiBox.setText("Wifi opened");
chkOpenCloseWifiBox.setChecked(true);
}
else {
chkOpenCloseWifiBox.setText("Wifi closed");
chkOpenCloseWifiBox.setChecked(false);
}
// 輸出WIFI訊息
StringBuffer sb = new StringBuffer();
sb.append("Wifi Info\n");
sb.append("MAC Address:" + wifiInfo.getMacAddress() + "\n");
sb.append("SSID:" + wifiInfo.getSSID() + "\n");
sb.append("IP Address(int):" + wifiInfo.getIpAddress() + "\n");
sb.append("IP Address(Hex):" + Integer.toHexString(wifiInfo.getIpAddress()) + "\n");
sb.append("IP Address:" + ipIntToString(wifiInfo.getIpAddress()) + "\n");
sb.append("Network ID:" + wifiInfo.getNetworkId() + "\n");
sb.append("Network Speed:" + Integer.toString(wifiInfo.getLinkSpeed()) + "\n");
sb.append("Signal Strength:" + Integer.toString(wifiInfo.getRssi()) + "\n");
tvWifiInfo.setText(sb.toString());
// 得到配置好的網路
wifiConfigurations = wifiManager.getConfiguredNetworks();
tvWifiConfigurations.setText("Had connected wifi\n");
for (WifiConfiguration wifiConfiguration : wifiConfigurations) {
tvWifiConfigurations.setText(tvWifiConfigurations.getText() + wifiConfiguration.SSID + "\n");
}
}
// 將int型態的IP轉換成文字型態
private String ipIntToString(int ip) {
try {
byte[] bytes = new byte[4];
bytes[0] = (byte) (0xff & ip);
bytes[1] = (byte) ((0xff00 & ip) >> 8);
bytes[2] = (byte) ((0xff0000 & ip) >> 16);
bytes[3] = (byte) ((0xff000000 & ip) >> 24);
return Inet4Address.getByAddress(bytes).getHostAddress();
}
catch (Exception e) {
return "";
}
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 當選中複選框時打開WiFi
if (isChecked) {
wifiManager.setWifiEnabled(true);
chkOpenCloseWifiBox.setText("Wifi opened");
}
// 當取消複選框時關閉WiFi
else {
wifiManager.setWifiEnabled(false);
chkOpenCloseWifiBox.setText("Wifi closed");
}
}
}
2011年4月8日 星期五
SD card read/write功能
SD card read/write功能
功能:讀寫SD card
AndroidManifest.xml
加入權限"android.permission.WRITE_EXTERNAL_STORAGE"
Xxx.java
// Write
private writeToSdCard() {
File vSDCard= null;
try {
// 獲得外部外接裝置位置
vSDCard= Environment.getExternalStorageDirectory();
// vSDCard.getParent() - "/" ; vSDCard.getName() - "sdcard"
FileWriter vFile = new FileWriter(vSDCard.getParent() + vSDCard.getName() + "/Log.txt", true);
// 使用這種方式有可能會得到錯誤的路徑,所以改用下面的方式
// vSDCard.getAbsolutePath() - 擷取目前位置路徑
FileWriter vFile = new FileWriter(vSDCard.getAbsolutePath() + "/Log.txt", true);
// write the data to file
vFile.append("," + status + "," + level + "," + "\n");
vFile.close();
}
catch (Exception e) {
}
}
// Read
private void readFromSdCard() {
File vSDCard= null;
try {
// 獲得外部外接裝置位置
vSDCard= Environment.getExternalStorageDirectory();
// read data form sd card file
FileReader vFile = new FileReader( vSDCard.getAbsolutePath() + "/sd_test.txt" );
StringBuffer sb = new StringBuffer();
int c;
// if value = "-1", it mean the end of the file
while ((c = vFile.read()) != -1) {
sb.append((char) c);
}
vFile.close();
}
catch (Exception e) {
}
}
功能:讀寫SD card
AndroidManifest.xml
加入權限"android.permission.WRITE_EXTERNAL_STORAGE"
Xxx.java
// Write
private writeToSdCard() {
File vSDCard= null;
try {
// 獲得外部外接裝置位置
vSDCard= Environment.getExternalStorageDirectory();
// vSDCard.getParent() - "/" ; vSDCard.getName() - "sdcard"
// 使用這種方式有可能會得到錯誤的路徑,所以改用下面的方式
// vSDCard.getAbsolutePath() - 擷取目前位置路徑
FileWriter vFile = new FileWriter(vSDCard.getAbsolutePath() + "/Log.txt", true);
// write the data to file
vFile.append("," + status + "," + level + "," + "\n");
vFile.close();
}
catch (Exception e) {
}
}
// Read
private void readFromSdCard() {
File vSDCard= null;
try {
// 獲得外部外接裝置位置
vSDCard= Environment.getExternalStorageDirectory();
// read data form sd card file
FileReader vFile = new FileReader( vSDCard.getAbsolutePath() + "/sd_test.txt" );
StringBuffer sb = new StringBuffer();
int c;
// if value = "-1", it mean the end of the file
while ((c = vFile.read()) != -1) {
sb.append((char) c);
}
vFile.close();
}
catch (Exception e) {
}
}
設定Brightness功能
設定Brightness功能
在提及設定Brightness功能之前要先介紹一下目前Android設定Brightness的方式。
目前Android設定Brightness的方式大致上分為二種:
1. 調整目前AP的亮度,離開AP會返回系統設定亮度。
2. 調整系統亮度,但是設定值要在亮度有被修改時才會更新。
例如:進入設定亮度頁面,再離開,或修改AP亮度。
若要在AP裡設定系統亮度,則需要使用到上述二個部分。
相關程式碼如下:
AndroidManifest.xml
加入權限"android.permission.WRITE_SETTINGS"
Xxx.java
// 可以利用SeekBar物件當作設定值的輸入
// 修改系統亮度設定
// 先將亮度設定模式調整為手動
Settings.System.putInt(
getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
// 修改系統亮度設定 (setting rage : 0~255)
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS,value);
// 修改目前視窗亮度 (setting rage : 0.0~1.0)
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = value/255f;
getWindow().setAttributes(lp);
*當設定值0時,會造成Android系統將整個螢幕關閉的問題。
在提及設定Brightness功能之前要先介紹一下目前Android設定Brightness的方式。
目前Android設定Brightness的方式大致上分為二種:
1. 調整目前AP的亮度,離開AP會返回系統設定亮度。
2. 調整系統亮度,但是設定值要在亮度有被修改時才會更新。
例如:進入設定亮度頁面,再離開,或修改AP亮度。
若要在AP裡設定系統亮度,則需要使用到上述二個部分。
相關程式碼如下:
AndroidManifest.xml
加入權限"android.permission.WRITE_SETTINGS"
Xxx.java
// 可以利用SeekBar物件當作設定值的輸入
// 修改系統亮度設定
// 先將亮度設定模式調整為手動
Settings.System.putInt(
getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
// 修改系統亮度設定 (setting rage : 0~255)
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS,value);
// 修改目前視窗亮度 (setting rage : 0.0~1.0)
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = value/255f;
getWindow().setAttributes(lp);
*當設定值0時,會造成Android系統將整個螢幕關閉的問題。
Wake up lock功能
Wake up lock功能
功能:程式執行時,鎖定在Wake up狀態
AndroidManifest.xml
Add "android.permission.WAKE_LOCK" user permission
private PowerManager.WakeLock wl;
// 利用PowerManager取得service,設定wake up狀態
public void onCreate(Bundle savedInstanceState) {
.............................
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag");
wl.acquire();
}
// 離開時,釋放控制權
protected void onDestroy() {
super.onDestroy();
wl.release();
}
功能:程式執行時,鎖定在Wake up狀態
AndroidManifest.xml
Add "android.permission.WAKE_LOCK" user permission
private PowerManager.WakeLock wl;
// 利用PowerManager取得service,設定wake up狀態
public void onCreate(Bundle savedInstanceState) {
.............................
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag");
wl.acquire();
}
// 離開時,釋放控制權
protected void onDestroy() {
super.onDestroy();
wl.release();
}
訂閱:
文章 (Atom)