当前位置: 代码迷 >> 移动开发 >> 简略recorder
  详细解决方案

简略recorder

热度:1074   发布时间:2013-02-26 00:00:00.0
简单recorder

//activity

package com.example.recorder_audio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;

import com.example.recorder_audio.PlayRecorderService.PlayBinder;
import com.example.recorder_audio.RecorderAudioService.RecorderBinder;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener
{

??? private Button recordAudio;
??? private Button stop;
??? private Button play;
??? private File file;
??? private RecorderBinder recorderBinder;
??? private ServiceConnection conn=new ServiceConnection()
??? {
???????

??????? @Override
??????? public void onServiceDisconnected(ComponentName name)
??????? {
??????? }
???????
??????? @Override
??????? public void onServiceConnected(ComponentName name, IBinder service)
??????? {
?????????? recorderBinder =(RecorderBinder)service;
??????? }
??? };
??? private PlayBinder playBinder;
??? private ServiceConnection conn2=new ServiceConnection()
??? {
???????

??????? @Override
??????? public void onServiceDisconnected(ComponentName name)
??????? {
??????? }
???????
??????? @Override
??????? public void onServiceConnected(ComponentName name, IBinder service)
??????? {
??????????? playBinder=(PlayBinder)service;???????????
??????? }
??? };

??? @Override
??? protected void onCreate(Bundle savedInstanceState)
??? {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.activity_main);
??????? iniService();
?????? recordAudio =(Button) findViewById(R.id.button1);
?????? stop =(Button) findViewById(R.id.button2);
?????? play =(Button) findViewById(R.id.button3);
?????? recordAudio.setOnClickListener(this);
?????? stop.setOnClickListener(this);
?????? play.setOnClickListener(this);
??? }

??? private void iniService()
??? {
????? Intent intent=new Intent(this, RecorderAudioService.class);
????? bindService(intent, conn, BIND_AUTO_CREATE);?
????? Intent intent2=new Intent(this, PlayRecorderService.class);
????? bindService(intent2, conn2, BIND_AUTO_CREATE);?
??? }

??? @Override
??? public boolean onCreateOptionsMenu(Menu menu)
??? {
??????? // Inflate the menu; this adds items to the action bar if it is present.
??????? getMenuInflater().inflate(R.menu.activity_main, menu);
??????? return true;
??? }

??? @Override
??? public void onClick(View v)
??? {
?????? switch (v.getId())
??? {
??? case R.id.button1:
??????? recordAudio();
??????? break;
??? case R.id.button2:
??????? stop();
??????? break;
??? case R.id.button3:
??????? play();
??????? break;

??? default:
??????? break;
??? }???????
??? }

??? private void play()
??? {
?????? playBinder.play(file.getAbsolutePath());
??? }

??? private void recordAudio()
??? {
??????? file = new File("/sdcard/"? + "wuxifu.amr");
??????? try
??????? {
??????????? file.createNewFile();//自己要先建好一个文件
??????? }
??????? catch (IOException e)
??????? {
??????????? // TODO Auto-generated catch block
??????????? e.printStackTrace();
??????? }

??????????????? Toast.makeText(getApplicationContext(), "正在录音,录音文件在"+file.getAbsolutePath(), Toast.LENGTH_LONG) .show();
??????????????? recorderBinder.record(file.getAbsolutePath());
??????????????? recordAudio.setText("录音中……");?

???????????????

??? }

??? private void stop()
??? {
??????? recorderBinder.stop();
??????? recordAudio.setText("录音完毕!!!");
??? }
??? @Override
??? protected void onDestroy()
??? {
??????? super.onDestroy();
??????? //解绑服务
??????? unbindService(conn);
??????? unbindService(conn2);
??? }

}
//录音的service

package com.example.recorder_audio;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Binder;
import android.os.IBinder;

public class RecorderAudioService extends Service
{
??? private MediaRecorder mediaRecorder;
??? private RecorderBinder recorderBinder;
??? public RecorderAudioService()
??? {
??? }
??? class RecorderBinder extends? Binder
??? {

??????? public void record(String path)
??????? {
??????????? // 从麦克风源进行录音
??????????? mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
???????? // 设置输出格式
??????????? mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
??????????? // 设置编码格式 Call this after setOutputFormat() but before prepare().
??????????? mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
??????????? // 设置输出文件
??????????? mediaRecorder.setOutputFile(path);
??????????? // 准备录制
??????????? try
??????????? {
??????????????? mediaRecorder.prepare();
??????????????? // 开始录制
??????????????? mediaRecorder.start();
??????????? }
??????????? catch (IllegalStateException e)
??????????? {
??????????????? // TODO Auto-generated catch block
??????????????? e.printStackTrace();
??????????? }
??????????? catch (IOException e)
??????????? {
??????????????? // TODO Auto-generated catch block
??????????????? e.printStackTrace();
??????????? }
??????????
???????????
??????? }

??????? public void stop()
??????? {
???????? mediaRecorder.stop();???????????
??????? }
???????????
??? }
??? @Override
??? public void onCreate()
??? {
??????? super.onCreate();
??????? mediaRecorder = new MediaRecorder();
??????? recorderBinder = new RecorderBinder();
??? }
??? @Override
??? public IBinder onBind(Intent intent)
??? {
??????? return recorderBinder;
??? }
}
//播放 的service

package com.example.recorder_audio;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

public class PlayRecorderService extends Service
{
??? private MediaPlayer mediaPlayer;
??? private PlayBinder playBinder;
??? public PlayRecorderService()
??? {
??? }
??? class PlayBinder? extends? Binder
??? {

??????? public void play(String absolutePath)
??????? {
??????????? try
??????????? {
??????????????? mediaPlayer.reset(); //这句很重要,重置对象,回到初始化创建 的状态
??????????????? mediaPlayer.setDataSource(absolutePath);
??????????????? mediaPlayer.prepare();
??????????????? mediaPlayer.start();
??????????????? mediaPlayer.setOnCompletionListener(new OnCompletionListener()
??????????????? {
???????????????????
??????????????????? @Override
??????????????????? public void onCompletion(MediaPlayer mp)
??????????????????? {
?????????????????????? Toast.makeText(PlayRecorderService.this, "播放完毕", Toast.LENGTH_LONG).show();
??????????????????? }
??????????????? });
??????????? }
??????????? catch (IllegalArgumentException e)
??????????? {
??????????????? // TODO Auto-generated catch block
??????????????? e.printStackTrace();
??????????? }
??????????? catch (SecurityException e)
??????????? {
??????????????? // TODO Auto-generated catch block
??????????????? e.printStackTrace();
??????????? }
??????????? catch (IllegalStateException e)
??????????? {
??????????????? // TODO Auto-generated catch block
??????????????? e.printStackTrace();
??????????? }
??????????? catch (IOException e)
??????????? {
??????????????? // TODO Auto-generated catch block
??????????????? e.printStackTrace();
??????????? }
?????????
?????????????????????
??????? }
???????
??? }
??? @Override
??? public void onCreate()
??? {
??????? super.onCreate();
??????? mediaPlayer = new MediaPlayer();
??????? playBinder = new PlayBinder();
??? }
??? @Override
??? public IBinder onBind(Intent intent)
??? {
??????? return?? playBinder;
??? }
}