1

我的目标是制作一个可以拨打电话号码然后记录保存情况的应用程序。

我已经测试了这段代码,它可以录制语音,然后在我的 5310 上正确播放。

import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

public class VoiceRecordMidlet extends MIDlet {
      private Display display;

      public void startApp() {
            display = Display.getDisplay(this);
            display.setCurrent(new VoiceRecordForm());
      }

      public void pauseApp() {
      }

      public void destroyApp(boolean unconditional) {
            notifyDestroyed();
      }
}

class VoiceRecordForm extends Form implements CommandListener {
      private StringItem message;
      private StringItem errormessage;
      private final Command record, play;
      private Player player;
      private byte[] recordedAudioArray = null;
      public VoiceRecordForm() {
            super("Recording Audio");
            message = new StringItem("", "Select Record to start recording.");
            this.append(message);
            errormessage = new StringItem("", "");
            this.append(errormessage);
            record = new Command("Record", Command.OK, 0);
            this.addCommand(record);
            play = new Command("Play", Command.BACK, 0);
            this.addCommand(play);
            this.setCommandListener(this);
      }


  public void commandAction(Command comm, Displayable disp) {
            if (comm == record) {

              System.getProperty("audio.encodings");
                System.getProperty("supports.audio.capture");
              System.getProperty("supports.recording");
                  t.start();

            }
            else if (comm == play) {
                  try {

                        ByteArrayInputStream recordedInputStream = new ByteArrayInputStream(recordedAudioArray);
                        Player p2 = Manager.createPlayer(recordedInputStream, "audio/amr");
                        p2.prefetch();
                        p2.start();
                  }
                  catch (Exception e) {
                        errormessage.setLabel("Error");
                        errormessage.setText(e.toString());
                  }
            }
             }

         Thread t = new Thread() {
                        public void run() {
                              try {

                                 System.getProperty("audio.encodings");


                                    player = Manager.createPlayer("capture://audio");
                                    player.realize();
                                    RecordControl rc = (RecordControl) player.getControl("RecordControl");
                                    ByteArrayOutputStream output = new ByteArrayOutputStream();
                                    rc.setRecordStream(output);
                                    rc.startRecord();
                                    player.start();
                                    message.setText("Recording...");
                                    Thread.sleep(5000);
                                    message.setText("Recording Done!");
                                    rc.commit();
                                    recordedAudioArray = output.toByteArray();
                                    player.close();
                              } catch (Exception e) {
                                    errormessage.setLabel("Error");
                                    errormessage.setText(e.toString());
                              }
             };



};
}

然后我对我的代码进行了一些更改以记录保护

import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

public class VoiceRecordMidlet extends MIDlet {
      private Display display;

Form f = new VoiceRecordForm();

      public void startApp() {

            display = Display.getDisplay(this);
            display.setCurrent(f);
      }

      public void pauseApp() {
      }

      public void destroyApp(boolean unconditional) {
            notifyDestroyed();
      }


class VoiceRecordForm extends Form implements CommandListener {
    private MIDlet m;
      private StringItem message;
      private StringItem errormessage;
      private final Command record, play;
      private Player player;
      private byte[] recordedAudioArray = null;
              private TextField phoneField;
           private String n;
      public VoiceRecordForm() {
            super("Recording Audio");
            message = new StringItem("", "Select Record to start recording.");
            this.append(message);

            errormessage = new StringItem("", "");
            this.append(errormessage);
            record = new Command("Record", Command.SCREEN, 0);
            this.addCommand(record);
            play = new Command("Play", Command.BACK, 0);
            this.addCommand(play);
            this.phoneField =new TextField("number","",20,phoneField.PHONENUMBER);
            append(phoneField);
            this.setCommandListener(this);
      }


  public void commandAction(Command comm, Displayable disp) {
            if (comm == record) {

              System.getProperty("audio.encodings");
                System.getProperty("supports.audio.capture");
              System.getProperty("supports.recording");
 t.start();
                   try
   {
       n=phoneField.getString().trim();

       platformRequest("tel:"+n);


   }
  catch(javax.microedition.io.ConnectionNotFoundException e)

  {
      e.printStackTrace();
  }

 }
  else if (comm == play) {

  try {



               ByteArrayInputStream recordedInputStream = new ByteArrayInputStream(recordedAudioArray);
                        Player p2 = Manager.createPlayer(recordedInputStream, "audio/amr");

                        p2.prefetch();
                        p2.start();

                  }
                  catch (Exception e) {
                        errormessage.setLabel("Error");
                        errormessage.setText(e.toString());
                  }
            }
             }

         Thread t = new Thread() {
                        public void run() {
                              try {

                                 System.getProperty("audio.encodings");


                                    player = Manager.createPlayer("capture://audio");
                                    player.realize();
                                    RecordControl rc = (RecordControl) player.getControl("RecordControl");
                                    ByteArrayOutputStream output = new ByteArrayOutputStream();
                                    rc.setRecordStream(output);
                                    rc.startRecord();
                                    player.start();
                                    message.setText("Recording...");
                                    Thread.sleep(5000);
                                    message.setText("Recording Done!");
                                    rc.commit();
                                    recordedAudioArray = output.toByteArray();
                                    player.close();
                              } catch (Exception e) {
                                    errormessage.setLabel("Error");
                                    errormessage.setText(e.toString());
                              }
             };



};
}}

但是当按下记录命令时出现以下错误

**Error:    javax.microedition.media.MediaException: RecordControl**

请问有什么帮助吗?

4

0 回答 0