Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android開發教程 傳感器實例

Android開發教程 傳感器實例

編輯: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;

  }

  }

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved