編輯:關於android開發
Android開發教程,Android開發當中傳感器的使用也是常常遇到的,今天我們來看看IBM的安卓開發代碼是在怎麼寫的吧,主要代碼如下:
IBMAudio.java代碼:
/*
*
* IBMAudio.java
* IBM的Developerworks示例代碼
* Author: anonymity
*
*/
package com.msi.ibm.audio;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class IBMAudio extends Activity {
public MediaRecorder mrec = null;
private Button startRecording = null;
private Button stopRecording = null;
private static final String TAG = "SoundRecordingDemo";
File audiofile;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mrec = new MediaRecorder();
startRecording = (Button)findViewById(R.id.startrecording);
stopRecording = (Button)findViewById(R.id.stoprecording);
startRecording.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
try
{
startRecording.setEnabled(false);
stopRecording.setEnabled(true);
stopRecording.requestFocus();
startRecording();
}catch (Exception ee)
{
Log.e(TAG,"Caught io exception " + ee.getMessage());
}
}
});
stopRecording.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
startRecording.setEnabled(true);
stopRecording.setEnabled(false);
startRecording.requestFocus();
stopRecording();
processaudiofile();
}
});
stopRecording.setEnabled(false);
startRecording.setEnabled(true);
}
protected void processaudiofile() {
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
// this does not always seem to work cleanly....
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
}
protected void startRecording() throws IOException
{
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// mRecorder.setOutputFile("/sdcard/yousuck2.3gp");
if (audiofile == null) {
File sampleDir = Environment.getExternalStorageDirectory();
try {
audiofile = File.createTempFile("ibm", ".3gp", sampleDir);
}
catch (IOException e)
{
Log.e(TAG,"sdcard access error");
return;
}
}
mrec.setOutputFile(audiofile.getAbsolutePath());
mrec.prepare();
mrec.start();
}
protected void stopRecording() {
mrec.stop();
mrec.release();
}
}
R.java代碼:
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.msi.ibm.audio;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int startrecording=0x7f050000;
public static final int stoprecording=0x7f050001;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
Android Doze模式源碼分析,androiddoze 科技的仿生學無處不在,給予我們啟發。為了延長電池是使用壽命,google從蛇的冬眠中得到體會,那
Android之操作SQLite,androidsqlite1.SQLite簡介 SQLite是一款輕型的數據庫,是遵守ACID的關聯式數據庫管理系統,它的設計目標是嵌入
Android IPC機制(四):細說Binder連接池 一、 前言 在上一篇文章 Android IPC機制(三):淺談Binder的使用中,筆者淺談了Binder
ReactNative官方中文文檔0.21,reactnative0.21 整理了一份ReactNative0.21中文文檔,提供給需要的reactnative愛好者