CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2SE / 基础类

求java声音播放(对单词或句子进行阅读)代码例子

楼主wangyanghu(钳工级程序员)2003-06-04 08:50:29 在 Java / J2SE / 基础类 提问

如IBM詞典,对句子可以完成阅读 问题点数:100、回复次数:1Top

1 楼wyhgg(石头)回复于 2003-06-04 08:52:44 得分 100

//Talker.java  
  import   java.io.*;  
  import   java.util.*;  
  import   java.net.*;  
  import   javax.sound.sampled.*;  
   
  public   class   Talker  
  {  
        private   SourceDataLine   line=null;  
        public   static   void   main(String   args[])  
        {  
              Talker   player=new   Talker();  
              if(args.length>0)  
              {  
                    for(int   i=0;i<args.length;i++)  
                    {  
                          player.sayPhoneWord(args[i]);  
                    }  
              }  
              else  
              {  
                    player.sayPhoneSentence("ii   l|o|ve   y|ou   l|i   l|i   p|i|ng");  
              }  
              System.exit(0);  
        }  
        public   void   sayPhoneSentence(String   sentence)  
        {  
              StringTokenizer   st=new   StringTokenizer(sentence,"   ",false);  
              while(st.hasMoreTokens())  
              {  
                    String   word=st.nextToken();  
                    sayPhoneWord(word);  
              }  
        }  
        public   void   sayPhoneWord(String   word)  
        {  
              byte[]   previousSound=null;  
              StringTokenizer   st=new   StringTokenizer(word,"|",false);  
              while   (st.hasMoreTokens())  
              {  
                    String   thisPhoneFile=st.nextToken();  
                    thisPhoneFile="/allophones/"+thisPhoneFile+".au";  
                    byte[]   thisSound=getSound(thisPhoneFile);  
                    if(previousSound!=null)  
                    {  
                          int   mergeCount=0;  
                          if   (previousSound.length>=500   &&   thisSound.length>=500)  
                          {  
                                mergeCount=500;  
                          }  
                          for(int   i=0;   i<mergeCount;i++)  
                          {  
                                previousSound[previousSound.length-mergeCount+i]=(byte)((previousSound[previousSound.length-mergeCount+i]+thisSound[i])/2);  
                          }  
                          playSound(previousSound);  
                          byte[]   newSound=new   byte[thisSound.length-mergeCount];  
                          for   (int   ii=0;   ii<newSound.length;   ii++)  
                          {  
                                newSound[ii]=thisSound[ii+mergeCount];  
                          }  
                          previousSound=newSound;    
                    }  
                    else  
                    {  
                          previousSound=thisSound;  
                    }  
              }  
              playSound(previousSound);  
              drain();  
        }  
        private   void   drain()  
        {  
              if   (line!=null)   line.drain();  
              try   {Thread.sleep(100);}catch(Exception   e){}  
        }  
        private   void   playSound(byte[]   data)  
        {  
              if   (data.length>0)   line.write(data,   0,   data.length);  
        }  
        private   byte[]   getSound(String   fileName)  
        {  
              try  
              {  
                    URL   url=Talker.class.getResource(fileName);  
                    AudioInputStream   stream   =   AudioSystem.getAudioInputStream(url);  
                    AudioFormat   format   =   stream.getFormat();  
                    if   ((format.getEncoding()   ==   AudioFormat.Encoding.ULAW)   ||   (format.getEncoding()   ==   AudioFormat.Encoding.ALAW))    
                    {  
                          AudioFormat   tmpFormat   =   new   AudioFormat(AudioFormat.Encoding.PCM_SIGNED,format.getSampleRate(),format.getSampleSizeInBits()*2,format.getChannels(),format.getFrameSize()*2,format.getFrameRate(),true);  
                          stream   =   AudioSystem.getAudioInputStream(tmpFormat,   stream);  
                          format   =   tmpFormat;  
                    }  
                    DataLine.Info   info   =   new   DataLine.Info(Clip.class,format,((int)   stream.getFrameLength()   *   format.getFrameSize()));  
                    if(line==null)  
                    {  
                          DataLine.Info   outInfo   =   new   DataLine.Info(SourceDataLine.class,format);  
                          if   (!AudioSystem.isLineSupported(outInfo))  
                          {  
                                System.out.println("Line   matching   "   +   outInfo   +   "   not   supported.");  
                                throw   new   Exception("Line   matching   "   +   outInfo   +   "   not   supported.");  
                          }  
                          line   =   (SourceDataLine)   AudioSystem.getLine(outInfo);  
                          line.open(format,   50000);  
                          line.start();  
                    }  
                    int   frameSizeInBytes   =   format.getFrameSize();  
                    int   bufferLengthInFrames   =   line.getBufferSize()   /   8;  
                    int   bufferLengthInBytes   =   bufferLengthInFrames   *   frameSizeInBytes;  
                    byte[]   data=new   byte[bufferLengthInBytes];  
                    int   numBytesRead   =   0;  
                    if   ((numBytesRead   =   stream.read(data))   !=   -1)  
                    {  
                          int   numBytesRemaining   =   numBytesRead;  
                    }  
                    byte   maxByte=0;  
                    byte[]   newData=new   byte[numBytesRead];  
                    for   (int   i=0;   i<numBytesRead;i++)  
                    {  
                          newData[i]=data[i];  
                          if   (newData[i]>maxByte)  
                          {  
                                maxByte=newData[i];  
                          }  
                    }  
                    return   newData;  
              }  
              catch   (Exception   e)  
              {  
              return   new   byte[0];  
              }  
        }  
  }  
  Top

相关问题

  • 输出一个句子中最长的单词
  • 谁有用PB编写的单词学习系统的源代码啊
  • 谁有用PB编写的单词学习系统的源代码啊
  • 谁有用PB编写的单词学习系统的源代码啊
  • java源代码??(48分)
  • 求优秀的JAVA代码
  • java代码运行问题
  • 跪求core java volume1,6th代码
  • [200分]求一JAVA 代码
  • 能够做单词、句子MP3的超爽软件推荐,还有免费的呢!

关键词

  • 阅读
  • 句子
  • talker
  • args
  • import

得分解答快速导航

  • 帖主:wangyanghu
  • wyhgg

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo