2012-09-20 52 views
0

我正在使用諾基亞S40系列手機進行音頻錄製。我能夠錄製該信息,但我無法播放錄製的音頻信息。用於Java ME中的.amr文件的語音或音頻播放器

任何人都可以幫助我如何編寫錄音.amr音頻文件的語音播放器? 有沒有人遇到過這個問題?

回答

0

這裏是錄製和播放聲音的我工作的例子,

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) { 
        Thread t = new Thread() { 
         public void run() { 
           try { 
            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()); 
           } 
         } 
        }; 
        t.start(); 

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