JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區 » IO/Streams  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 關於音檔串流寫入檔案方法請教
ttnezpwinf





發文: 20
積分: 0
於 2018-08-23 11:19 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
各位好:
  小弟在音檔串流寫入檔案時遇到了一些問題想請教。
  在讀取檔案播放音檔聽起來都很正常,我嘗試將串流寫入新的檔案,新的音檔聽起來有很多雜音,
想請教音訊串流寫入檔案時是否有更好的方法。請各位指點迷津,謝謝。
以下是我的程式碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.lib.test;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
 
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
 
public class Audio_Library {
  public static String configFilePath = "";
 
  public Audio_Library(){
 
    String path = null;
    try {
      path = URLDecoder.decode(Thread.currentThread().getContextClassLoader().getResource("").toString(),"UTF-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    configFilePath = path.replace("/", "\\").replace("file:", "").replace("WEB-INF\\classes\\", "voice\\Chinese\\").substring(1);
  }
  public void doPlay(){
    String readFile = configFilePath + "M591000.wav" ;
    try{
      InputStream in = new FileInputStream(readFile);
      AudioStream as = new AudioStream(in);
      AudioPlayer.player.start(as);  //播放音樂
//      AudioPlayer.player.stop(as);  //關閉音樂播放
      doSaveFile(in);
    }catch(FileNotFoundException e){
      System.out.print("FileNotFoundException ");
    }catch(IOException e){
      System.out.print("IOException");
    }
  }
  
  public void doSaveFile(InputStream in){
    String saveFile = configFilePath + "test.wav";
      try {
        byte[] buffer = new byte[in.available()];
      in.read(buffer);
      File targetFile = new File(saveFile);
        OutputStream os = new FileOutputStream(targetFile);
        os.write(buffer);
        os.flush();
        os.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
 


reply to postreply to post
» JWorld@TW »  Java SE 討論區 » IO/Streams

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8