我尝试使用ogg数据从icecast挂载点编写本地OGG文件。我使用URL建立连接,获取InputStream并在通过vorbisspi支持OGG格式的AudioInpustStream中捕获它,我可以在数据线中播放这个,我可以在ogg文件上写入数据但我无法在vlc播放器上播放此文件,我尝试使用此代码,我在编译中没有错误。
Click here to data line example
CODE。
public class AppSave {
private URL url;
private URLConnection urlconection;
private InputStream inputStream;
private AudioInputStream aisin;
private File file;
public AppSave() {
url = null;
urlconection = null;
inputStream = null;
aisin = null;
}
public static void main(String[] args) throws LineUnavailableException, IOException {
final AppSave player = new AppSave();
player.save("http://192.168.0.21/virtual.ogg");//Mount point
}
public void save(String mountpoint) throws LineUnavailableException, IOException {
try {
url = new URL(mountpoint); //URL icecast mount point
} catch (MalformedURLException ex) {
System.err.println("URL Malformed " + mountpoint);
}
try {
urlconection = url.openConnection();//open connexion with URL
} catch (IOException ex) {
System.err.println("Connexion unrealized");
}
try {
inputStream = urlconection.getInputStream();//get raw data
} catch (IOException ex) {
System.err.println("InputStream without data");
}
try {
try {
aisin = getAudioInputStream(inputStream);//prepare data for playback
} catch (IOException ex) {
System.err.println("Unsupported format");
}
final AudioFormat outFormat = getOutFormat(aisin.getFormat());//obtain format
String pathfile = "/home/stream/myfile.ogg";//output file path
File file = createfile(pathfile);
savestream(getAudioInputStream(outFormat, aisin), file);
} catch (UnsupportedAudioFileException e) {
System.err.println("Error: "+e.getMessage());
}
}
private AudioFormat getOutFormat(AudioFormat inFormat) {//obtain format
final int ch = inFormat.getChannels();
final float rate = inFormat.getSampleRate();
return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
}
private File createfile(String pathfile) throws IOException {//create and return the output file
PrintWriter pw = null;
FileWriter fichero = new FileWriter(pathfile);
pw = new PrintWriter(fichero);
File file = new File(pathfile);
return file;
}
private void savestream(AudioInputStream in, File outfile) throws IOException {//save stream
FileOutputStream out = new FileOutputStream(outfile);
final byte[] buffer = new byte[4096];
for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) {
out.write(buffer, 0, n);
}
out.close();
}
}
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>SaveOGG</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SaveOGG</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>tritonus-share</artifactId>
<version>0.3.7-2</version>
</dependency>
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>mp3spi</artifactId>
<version>1.9.5-1</version>
</dependency>
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>vorbisspi</artifactId>
<version>1.0.3-1</version>
</dependency>
</dependencies>
</project>