我正在尝试使用代号创建一个应用程序,该应用程序旨在记录语音并将文件发送到服务器,我刚刚创建了应用程序的录音部分,通过向我的表单添加一个按钮,当我点击它应该开始录制的按钮。它看起来工作得很好,但我想知道它何时以及如何停止,它似乎不停地继续下去。
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Voice Capture");
Button capture = new Button("Start Capture");
capture.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Capture.captureAudio(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String path = (String) evt.getSource();
if(path != null)
{
Dialog.show("Success", "You have successfulyy captured an audio file, The path ofthe file is " + path, "OK", null);
}
else
{
Dialog.show("ERROR", "You canceled the voice recording ", "OK",null);
}
}
});
}
});
hi.addComponent(capture);
hi.show();
}
答案 0 :(得分:0)
当用户取消录制窗口或按下录制器中的停止按钮时,它应该停止。
您还可以在没有UI的情况下进行录制,并通过使用媒体API进行更多控制:
Media media = MediaManager.createMediaRecorder(filePath, MediaManager.getAvailableRecordingMimeTypes()[0]);
media.play();
注意play()
是媒体API的一部分,实际上意味着开始录制。
答案 1 :(得分:0)
谢谢Shai,我想要的是我自己的录音机类型,我可以让它按照自己的方式工作。而且我认为我能够做到这一点,想到将它贴在这里应该以防万一它会帮助别人。
Media media;
String p;
String filename;
private void doRecord()
{
// initites devices microphone and do the recording until it is stopped.
p = getTempFileName();
try{
media = MediaManager.createMediaRecorder(p,MediaManager.getMediaRecorderingMimeType());
media.play();
}catch (IOException ex) {
ex.printStackTrace();
}
}
private String getTempFileName() {
String[] roots = FileSystemStorage.getInstance().getRoots();
// iOS doesn't have an SD card
String root = roots[0];
for (int i = 0; i < roots.length; i++) {
if (FileSystemStorage.getInstance().getRootType(roots[i]) == FileSystemStorage.ROOT_TYPE_SDCARD) {
root = roots[i];
break;
}
}
filename = "audioSample" + System.currentTimeMillis();
return root + FileSystemStorage.getInstance().getFileSystemSeparator() + filename;
}
public void stopRecord(boolean timeElapsed)
{
answertiming.cancel();
media.pause();
media.cleanup();
if(timeElapsed)
Dialog.show("SUCCESS","Your time is up system has saved your answer, The file path for the recording is " + p, "OK",null);
else
Dialog.show("SUCCESS","You have successfully answered the question System has saved your answer, The file path for the recording is " + p, "OK",null);
}
UITimer answertiming;
Label timeLabel;
public void startRecord()
{
i = 30;
timing.cancel();
Form record = new Form();
record.setLayout(new BorderLayout());
timeLabel = new Label("00:30");
TextArea questionLabel = new TextArea("Question: " + question_text);
questionLabel.setEditable(false);
Button doneButton = new Button("DONE");
doneButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
stopRecord(false);
}
});
Container c = new Container();
c.setLayout(new BorderLayout());
c.addComponent(BorderLayout.WEST,questionLabel);
c.addComponent(BorderLayout.EAST, timeLabel);
record.addComponent(BorderLayout.NORTH,c);
record.addComponent(BorderLayout.SOUTH,doneButton);
answertiming = new UITimer(new Runnable() {
public void run() {
i--;
timeLabel.setText("00:" + i);
if(i == 0)
{
stopRecord(true);
}
}
});
answertiming.schedule(1000, true, record);
record.show();
doRecord();
}
这是一个有问题的面试应用程序,用户答案被记录下来,然后发送到服务器(我删除了发送到服务器的部分)。如果用户停止应用程序录制本身它将停止并保存文件但如果不在30秒后它自行停止。
注意:要收听您可能需要编辑文件名的文件,并在其末尾添加.amr。