Java Tutorial/J2ME/Audio Capture

Материал из Java эксперт
Версия от 05:00, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Capture Playback Audio

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.RecordControl;
import javax.microedition.midlet.MIDlet;
public class CapturePlaybackAudioMIDlet extends MIDlet implements CommandListener {
  private Display display = null;
  private Alert alert = new Alert("Message");
  private Command exitCommand = new Command("Exit", Command.EXIT, 1);
  private Player capturePlayer = null;
  private Player playbackPlayer = null;
  private RecordControl recordControl = null;
  private ByteArrayOutputStream bos = new ByteArrayOutputStream();
  private ByteArrayInputStream bis = null;
  public CapturePlaybackAudioMIDlet() {
    display = Display.getDisplay(this);
    alert.setTimeout(Alert.FOREVER);
    alert.setString("Capturing for 10 seconds.");
    alert.addCommand(exitCommand);
    alert.setCommandListener(this);
    try {
      capturePlayer = Manager.createPlayer("capture://audio");
      if (capturePlayer != null) {
        capturePlayer.realize();
        recordControl = (RecordControl) capturePlayer
            .getControl("javax.microedition.media.control.RecordControl");
        display.setCurrent(alert);
        if (recordControl == null)
          throw new Exception("No RecordControl available");
        bos = new ByteArrayOutputStream(1024);
        recordControl.setRecordStream(bos);
      } else {
        throw new Exception("Capture Audio Player is not available");
      }
    } catch (Exception e) {
      error(e);
    }
  }
  public void startApp() {
    try {
      capturePlayer.start();
      recordControl.startRecord();
      Thread.sleep(10000);
      recordControl.stopRecord();
      recordControl.rumit();
      capturePlayer.stop();
      alert.setString("Well done!");
      bos.flush();
      bis = new ByteArrayInputStream(bos.toByteArray());
      playbackPlayer = Manager.createPlayer(bis, recordControl.getContentType());
      playbackPlayer.start();
      alert.setString("Playing back ... ");
    } catch (Exception e) {
      error(e);
    } finally {
      try {
        if (bos != null)
          bos.close();
        if (bis != null)
          bis.close();
      } catch (Exception ex) {
        error(ex);
      }
    }
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command cmd, Displayable disp) {
    if (cmd == exitCommand) {
      notifyDestroyed();
    }
  }
  private void error(Exception e) {
    alert.setString(e.getMessage());
    alert.setTitle("Error");
    display.setCurrent(alert);
    e.printStackTrace();
  }
}





Save Captured Audio

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.RecordControl;
import javax.microedition.midlet.MIDlet;
public class SaveCapturedAudioMIDlet extends MIDlet implements CommandListener {
  private Display display = null;
  private Alert alert = new Alert("Message");
  private Command exitCommand = new Command("Exit", Command.EXIT, 1);
  private Player capturePlayer = null;
  private Player playbackPlayer = null;
  private RecordControl rControl = (RecordControl) capturePlayer.getControl("javax.microedition.media.control.RecordControl");
  public SaveCapturedAudioMIDlet() {
    display = Display.getDisplay(this);
    alert.setTimeout(Alert.FOREVER);
    alert.setString("Capturing for 10 seconds.");
    alert.addCommand(exitCommand);
    alert.setCommandListener(this);
    try {
      capturePlayer = Manager.createPlayer("capture://audio");
      if (capturePlayer != null) {
        capturePlayer.realize();
        display.setCurrent(alert);
        if (rControl == null)
          throw new Exception("No RecordControl available");
        rControl.setRecordLocation("file:///test.wav");
      } else {
        throw new Exception("Capture Audio Player is not available");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public void startApp() {
    try {
      capturePlayer.start();
      rControl.startRecord();
      Thread.sleep(10000);
      rControl.stopRecord();
      rControl.rumit();
      capturePlayer.stop();
      capturePlayer.close();
      playbackPlayer = Manager.createPlayer("file:///test.wav");
      playbackPlayer.start();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command cmd, Displayable disp) {
    if (cmd == exitCommand) {
      destroyApp(true);
      notifyDestroyed();
    }
  }
}