录音机音频并保存到SD卡

时间:2014-04-13 15:57:37

标签: android mediarecorder

我正在尝试使用mediarecorder录制音频。但是我在该行中收到错误:

recorder.setOutputFile(archivo.getAbsolutePath());

公共类MainActivity扩展ActionBarActivity实现OnCompletionListener {

private TextView mensaje;
private MediaRecorder recorder ;
private MediaPlayer player;
private File archivo;
private Button boton1, boton2, boton3;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    boton1 = (Button) findViewById(R.id.button1);
    boton2 = (Button) findViewById(R.id.button2);
    boton3 = (Button) findViewById(R.id.button3);
    mensaje = (TextView) findViewById(R.id.textView1);  
}


public void grabar(View v) {
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
      File path = new File(Environment.getExternalStorageDirectory()
        .getPath());
              try {
        archivo = File.createTempFile("temporal", ".3gp", path);
     } catch (IOException e) {

     }
       recorder.setOutputFile(archivo.getAbsolutePath());
      try {
  recorder.prepare();

      } catch (IOException e) {

      }
                recorder.start();

          mensaje.setText("Grabando");
     boton1.setEnabled(false);
         boton2.setEnabled(true);
          }


public void detener(View v) {
    recorder.stop();
    recorder.release();
    player = new MediaPlayer();
    player.setOnCompletionListener(this);
    try {
        player.setDataSource(archivo.getAbsolutePath());
    } catch (IOException e) {
    }
    try {
        player.prepare();
    } catch (IOException e) {
    }
    boton1.setEnabled(true);
    boton2.setEnabled(false);
    boton3.setEnabled(true);
    mensaje.setText("Listo para reproducir");
}

public void reproducir(View v) {
    player.start();
    boton1.setEnabled(false);
    boton2.setEnabled(false);
    boton3.setEnabled(false);
    mensaje.setText("Reproduciendo");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

@Override
public void onCompletion(MediaPlayer mp) {


     boton1.setEnabled(true);
        boton2.setEnabled(true);
        boton3.setEnabled(true);
        mensaje.setText("Listo");



}

}

同样的帮助!感谢西班牙。

0 个答案:

没有答案