Looping mp3 using Naudio - c#

I play mp3 using NAudio but I don't know how to loop the music during opening program?
IWavePlayer waveOutDevice = new WaveOut();
AudioFileReader musicBackground = new AudioFileReader(#"....mp3");
waveOutDevice.Init(musicBackground);
waveOutDevice.Play();

Related

Playing from wav stream

var result = service.Synthesize(
text: text,
accept: "audio/wav",
voice: "en-US_AllisonVoice"
//voice: "en-US_HenryV3Voice"
);
using (FileStream fs = File.Create(#"C:\Users\nkk01\Desktop\voice.wav"))
{
result.Result.WriteTo(fs);
fs.Close();
result.Result.Close();
}
var waveStream = new WaveFileReader(#"C:\Users\nkk01\Desktop\voice.wav");
var waveOut = new WaveOutEvent();
waveOut.Init(waveStream);
Console.WriteLine("Playing");
waveOut.Play();
Console.WriteLine("Finished playing");
Hi this is my current code which is extremely inefficient as it has to save an audio stream to a file to play it. I would like to pass the audio stream directly to my laptop's speaker using the NAudio library. I still have not managed to find a solution. It will be of great help, thanks.
i'm not familiar with naudio but as far as i can see from their git hub repo, the constructor for WaveFileReader also accepts a stream instead of a filename ...
simply try writing everything you have to a memory stream instead of a file... don't forget to reposition the memory stream before you read it ('seek' to offset 0)

Play WAV file continuously

I have an ASP.NET + C # application that is not playing a WAV audio file.
My code causes no error, it starts playing but stops shortly after.
My code:
AudioFileReader wave = null
WaveOut outputSound = null
wave = new NAudio.Wave.AudioFileReader("C:/appwin/Audio.WAV");
OffsetSampleProvider OffsetSampleProvider = new OffsetSampleProvider(wave);
outputSound = new WaveOut();
outputSound.Init(OffsetSampleProvider);
outputSound.Play();

c# cscore how to instantly play loopback capture recorded bytes

Yo guys, its me again with my noob questions. so this time I've used cscore to record windows sounds then send the recorded bytes to another pc by sockets and let them play there.
I just could not figure out how to play the gotten bytes under DataAvailable callback...
I've tried to write the bytes gotten to a file and play that file that worked but sound is not playing correctly like there's some unexpected sounds being heard with it too.
so here's my code:
WasapiCapture capture = new WasapiLoopbackCapture();
capture.Initialize();
capture.DataAvailable += (s, e) =>
{
WaveWriter w = new WaveWriter("file.mp3", capture.WaveFormat);
w.Write(e.Data, e.Offset, e.ByteCount);
w.Dispose();
MemoryStream stream = new MemoryStream(File.ReadAllBytes("file.mp3"));
SoundPlayer player = new SoundPlayer(stream);
player.Play();
stream.Dispose();
};
capture.Start();
any help would be highly appreciated ;-;.
if you wanna hear how sound comes out by that way I would record you the result.
NOTE: if I just record sounds to a file and open later it just works perfectly but if I write and play instantly it unexpected sounds being heard.....
Use the SoundInSource as an adapater.
var capture = new WasapiCapture(...)
capture.Initialize(); //initialize always first!!!!
var soundInSource = new SoundInSource(capture)
{ FillWithZeros = true }; //set FillWithZeros to true, to prevent WasapiOut from stopping for the case WasapiCapture does not serve any data
var soundOut = new WasapiOut();
soundOut.Initialize(soundInSource);
soundOut.Play();

BufferedWaveProvider doesnt work in Unity

I am trying to implement NAudio into Unity. I managed to link the NAudio dll, but I am getting a strange error when I try to play music with NAudio BufferedWaveProvider.
If I to this:
WaveOut player;
BufferedWaveProvider buf;
AudioFileReader reader;
void Start () {
reader = new AudioFileReader(#"..\music.mp3"); // some music
player = new WaveOut();
player.Init(reader );
player.Play();
}
The music plays normal, without any problems.
But when I try use BufferedWaveProvider:
WaveOut player;
BufferedWaveProvider buf;
AudioFileReader reader;
void Start () {
reader = new AudioFileReader(#"..\music.mp3"); // some music
buf = new BufferedWaveProvider(reader.WaveFormat);
byte[] tmp = new byte[50000];
reader.Read(tmp, 0, tmp.Length); //read 50000 bytes
buf.AddSamples(tmp, 0, tmp.Length); //add bytes to buf
player = new WaveOut();
player.Init(buf); //init the WaveOut with buff
player.Play(); // play
}
It doesnt play! I debuged really a lot, and found out that the BufferedWaveProvider is using the samples (BufferedBytes are lowering), but I dont get any sound out of it!
I am using BufferedWaveProvider because of a more complex project, but its already a problem in such a simple example..
What am I missing?
Note: The same code WORKS in C# Windows Forms...
Try using WaveOutEvent instead of WaveOut, it worked at least for me in one of the projects.
As Mark pointed out:
it works because WaveOut uses Windows message callbacks by default, so if you have no gui thread (e.g. you are in a console app), then it can't be used and WaveOutEvent should be preferred

How to convert any audio format to mp3 using NAudio

public void AudioConvert()
{
FileStream fs = new FileStream(InputFileName, FileMode.Open, FileAccess.Read);
NAudio.Wave.WaveFormat format = new NAudio.Wave.WaveFormat();
NAudio.Wave.WaveStream rawStream = new RawSourceWaveStream(fs, format);
NAudio.Wave.WaveStream wsDATA = WaveFormatConversionStream.CreatePcmStream(rawStream);
WaveStream wsstream = wst.CanConvertPcmToMp3(2, 44100);
.....
}
// Here is the class
public class WaveFormatConversionStreamTests
{
public WaveStream CanConvertPcmToMp3(int channels,int sampleRate)
{
WaveStream ws = CanCreateConversionStream(
new WaveFormat(sampleRate, 16, channels),
new Mp3WaveFormat(sampleRate, channels, 0, 128000/8));
return ws;
}
}
Here, i am trying to convert any audio format to mp3 but my code is throwing exception like "ACMNotPossible" at ConvertPCMToMp3 function call. I am using NAudio 1.6 version dll. Right now i am working on windows 7. Please tell me where i went wrong in this code.
WaveFormatConversionStream is a wrapper around the Windows ACM APIs, so you can only use it to make MP3s if you have an ACM MP3 encoder installed. Windows does not ship with one of these. The easiest way to make MP3s is simply to use LAME.exe. I explain how to do this in C# in this article.
Also, if you are using the alpha of NAudio 1.7 and are on Windows 8 then you might be able to use the MP3 encoder which seems to come with Windows 8 as a Media Foundation Transform. Use the MediaFoundationEncoder (the NAudio WPF demo shows how to do this).

Categories

Resources