写了一个用java播放音频的代码,运行时总是出现错误!求指点!
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了写了一个用java播放音频的代码,运行时总是出现错误!求指点!相关的知识,希望对你有一定的参考价值。
运行结果:
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.Audiosystem.getAudioInputStream(Unknown Source)
at com.music.AePlayWave.run(Demo.java:44)
代码:
package com.music;
import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class Demo
/**
* @param args
*/
public static void main(String[] args)
// TODO Auto-generated method stub
AePlayWave apw = new AePlayWave("e:\\111.wma");
apw.start();
//播放声音的类
class AePlayWave extends Thread
private String filename;
public AePlayWave(String wavfile)
filename = wavfile;
public void run()
File soundFile = new File(filename);
AudioInputStream audioInputStream = null;
try
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
catch (Exception e1)
e1.printStackTrace();
return;
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class,format);
try
auline = (SourceDataLine)AudioSystem.getLine(info);
auline.open(format);
catch (Exception e)
e.printStackTrace();
return;
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[1024];
try
while (nBytesRead !=-1)
nBytesRead = audioInputStream.read(abData,0,abData.length);
if (nBytesRead>=0)
auline.write(abData,0,nBytesRead);
catch (Exception e)
e.printStackTrace();
return;
finally
auline.drain();
auline.close();
jdk对音乐文件播放支持不太好,但是sun有专门开发一个音频类播放的框架,叫 jmf,我这就有用jmf写过两个播放器
参考技术B jmfFile file =new File("e:\\111.wma");
Player audioPlayer = Manager.createRealizedPlayer(file.toURL());
//播放
audioPlayer.start();
//停止
audioPlayer.stop();
audioPlayer.close(); 参考技术C 等待解决方案 参考技术D 因为java sound不支持wma
无法播放用 naudio 录制的音频流
【中文标题】无法播放用 naudio 录制的音频流【英文标题】:Can't play audio stream recorded with naudio 【发布时间】:2015-05-19 20:13:08 【问题描述】:我尝试将一些音频(在录制单击按钮后)录制到 MemoryStream 中,然后使用 NAudio 在 Visual Studio(WPF) 中播放。我生成了一个 RawSourceWaveStream,它在单击播放按钮时获取 myMemoryStream,但我认为这是在窃听代码。 它不播放...这是我的代码。 我哪里错了?
namespace RecAndPlay
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
public MainWindow()
InitializeComponent();
private WaveIn myWaveIn=null;
private MemoryStream myMemoryStream = null;
private WaveOut myWaveOut= null;
private RawSourceWaveStream myRaw = null;
private void myRecord(object sender, RoutedEventArgs e)
RecordButton.Content = "Stop Rec";
myWaveIn = new WaveIn();
myWaveIn.WaveFormat = new WaveFormat(44100, 1);
myWaveIn.DeviceNumber = 0;
myWaveIn.DataAvailable += myWaveIn_DataAvailable;
myMemoryStream = new MemoryStream();
myWaveIn.StartRecording();
void myWaveIn_DataAvailable(object sender, WaveInEventArgs e)
myMemoryStream.Write(e.Buffer, 0, e.BytesRecorded);
private void myPlay(object sender, RoutedEventArgs e)
myWaveOut = new WaveOut();
myRaw = new RawSourceWaveStream(myMemoryStream, myWaveIn.WaveFormat);
myWaveOut.Init(myRaw);
myWaveOut.Play();
private void mtStopRec(object sender, RoutedEventArgs e)
myWaveIn.StopRecording();
RecordButton.Content = "Record";
【问题讨论】:
是的,它到了 myWaveOut.Play();但在那之后我什么都不能在这里。当鼠标站在 myRaw 上时,我可以看到它充满了缓冲区,但没有任何反应。 【参考方案1】:在播放音频之前,您需要将 MemoryStream
上的 Position
设置为 0。另外,请记住,使用此解决方案时,您不应在仍在录制时尝试播放。
【讨论】:
以上是关于写了一个用java播放音频的代码,运行时总是出现错误!求指点!的主要内容,如果未能解决你的问题,请参考以下文章