日期:2014-05-20  浏览次数:20849 次

j2me问题,有关如何录音的问题?
J2ME问题,有关如何录音的问题?    

第一个问题:

我想编一个java小程序,录一段正在播放的midi音乐。打算录5秒种时间.
我的代码如下:
try  
{
    Player   p   =   Manager.createPlayer(getClass().getResourceAsStream( "/air.mid "),   "audio/midi ");
  p.realize();
      //   Get   the   RecordControl,   set   the   record   stream,
        //   start   the   Player   and   record   for   5   seconds.
  p.prefetch();
        RecordControl   rc   =   (RecordControl)p.getControl( "RecordControl ");
        rc.setRecordLocation( "file:///audio2.wav ");
        rc.startRecord();
p.start();
Thread.currentThread().sleep(5000);
        rc.commit();
        p.close();
}  
catch   (IOException   ioe)  
{
}
catch   (MediaException   me)
{
}
catch   (InterruptedException   ie)
{
}
但是在运行过程中执行到rc.setRecordLocation( "file:///audio2.wav ")时,系统会报错,错误提示如下:
Uncaught   exception   java/lang/NullPointerException.


第二个问题:

但是如果我把程序改一下,如下:
  try  
  {
        Player   p   =   Manager.createPlayer( "capture://audio ");
        p.realize();
        //   Get   the   RecordControl,   set   the   record   stream,
        //   start   the   Player   and   record   for   5   seconds.
        RecordControl   rc   =   (RecordControl)p.getControl( "RecordControl ");
        rc.setRecordLocation( "file:///audio2.wav ");
        rc.startRecord();
        p.start();
        Thread.currentThread().sleep(5000);
        rc.commit();
        p.close();
  }  
  catch   (IOException   ioe)  
  {
  }  
  catch   (MediaException   me)  
  {
  }  
  catch   (InterruptedException   ie)
  {
  }
程序能够运行正常,但是打开audio2.wav文件,发现什么声音都没有,请教这是怎么回事?


------解决方案--------------------
我觉得这个可能是跟文件没有创建有关系 ,你试试使用Stream来做
------解决方案--------------------
录音处理错误
请参考MMAPI文档上的代码

Example
try {
// Create a Player that captures live audio.
Player p = Manager.createPlayer( "capture://audio ");
p.realize();
// Get the RecordControl, set the record stream,
// start the Player and record for 5 seconds.
RecordControl rc = (RecordControl)p.getControl( "RecordControl ");
ByteArrayOutputStream output = new ByteArrayOutputStream();
rc.setRecordStream(output);
rc.startRecord();
p.start();
Thread.currentThread().sleep(5000);
rc.commit();
p.close();
} catch (IOException ioe) {
} catch (MediaException me) {
} catch (InterruptedException ie) { }

------解决方案--------------------