在 J2me 中录制音频时出现异常

Posted

技术标签:

【中文标题】在 J2me 中录制音频时出现异常【英文标题】:Getting an exception when recording audio in J2me 【发布时间】:2012-09-20 09:17:07 【问题描述】:

在诺基亚 C1 中录制音频时出现异常。以下是我得到的以下异常:

Error2:
javax.microediting.media.MediaException: MUS

谁能帮我消除这个异常或者我哪里出错了?

下面,我提供了出现此异常的代码:

package video;

import javax.microedition.midlet.*;

import java.io.*;
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, end;
    private Player player;
    private byte[] recordedAudioArray = null;
    private RecordControl rc;
    private ByteArrayOutputStream output;

    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.SCREEN, 0);
        end = new Command("End", Command.SCREEN, 0);
        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();
                        rc = (RecordControl) player.getControl("RecordControl");
                        output = new ByteArrayOutputStream();
                        rc.setRecordStream(output);
                        rc.startRecord();
                        player.start();
                        removeCommand(record);
                        addCommand(end);


                     catch (Exception e) 
                        errormessage.setLabel("Error1");
                        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("Error2");
                errormessage.setText(e.toString());
            
         else if (comm == end) 
            try 
                rc.stopRecord();
                removeCommand(end);
                addCommand(play);
                                rc.commit();
                recordedAudioArray = output.toByteArray();

                player.close();

             catch (IOException ex) 
                ex.printStackTrace();
            
        
    

【问题讨论】:

【参考方案1】:

将您的代码的录制部分与我的一个项目的录制部分进行比较时,我能发现的唯一区别是我在调用 recordControl.startRecord() 之前调用 player.start()

player = Manager.createPlayer("capture://audio");
player.realize();
player.start();
recordControl = (RecordControl) player.getControl("RecordControl");
bos = new ByteArrayOutputStream();
recordControl.setRecordStream(bos);
recordControl.startRecord();

这适用于索尼爱立信手机。

不过我不播放音频,所以我不知道你的那部分代码是否导致了问题。

【讨论】:

以上是关于在 J2me 中录制音频时出现异常的主要内容,如果未能解决你的问题,请参考以下文章

在 HTC One 上以 16khz 单声道 PCM (WAV) 录制时出现断断续续的音频

音频录制和播放环回缓冲区问题,将数据写入音轨时出现问题

尝试将音频文件上传到服务器时出现Android问题

在 Android 2.2 中从麦克风录制时出现奇怪的失真

Camtasia录制时出现video codec open failed错误的解决方法

如何使用附加模式将音频录制到现有的音频文件中?