/** * 录音 * @param incomingNumber */ public void startRecorder(String incomingNumber){ //1.创建一个录音机的实例 recorder = new MediaRecorder(); //2.设置路径的数据源(VOICE_CALL模拟器和国外手机不支持) recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //3.设置输出文件的格式 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); //4.设置保存的文件名称 recorder.setOutputFile("/sdcard/"+incomingNumber+".3gp"); //5.设置音频的编码格式 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //6.准备开始录音 try { recorder.prepare(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } recorder.start(); } /** * 播放音乐 * @param incomingNumber */ public void play(View view){ try { player = new MediaPlayer(); player.reset(); player.setDataSource("http://tsmusic24.tc.qq.com/4833285.mp3"); //player.prepare();//同步的准备,如果没有准备好,就一直等待,直到准备好(用异步准备) player.prepareAsync();//异步准备。如果没有准备好,会报错 pb.setVisibility(View.VISIBLE); //当准备好的时候调用 player.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { // TODO Auto-generated method stub player.start(); pb.setVisibility(View.INVISIBLE); } }); //player.release();//如果调用了release,就是释放资源,下次需要用的时候需要重新new MediaPlayer } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
本文出自 “曾颐楠的播客” 博客,请务必保留此出处http://zengyinan.blog.51cto.com/9524976/1721477