I wanted to make looped background music for my game. I'm working in Microsoft Visual Studio 2010. In C# I wrote this:
InitializeComponent();
System.Media.SoundPlayer sp = new System.Media.SoundPlayer(#"MCStratV1.Properties.Resources.loop1.mp3");
//sp.PlayLooping();
sp.Play();
And I don't know how to do a link to resources. I'm new at C# so I don't know much about it.
Also I wrote // because I'm not sure it is correct.
There are two problems here.
First, if you look at the MSDN page for SoundPlayer you'll find that it says "Controls playback of a sound from a .wav file." So as a start you'll have to convert your mp3 to wav format.
Second, the constructor that takes a string wants the path to the wav file on disk, not the path of a resource. You'll either have to give the path to the file, or stream the file and pass the stream to another form of the constructor.
Related
I am trying to convert a .wav to an .mp3 file.
My code usually works but now I have gotten a .wav file that was exported with Imovie and I get the error 'NoDriver calling acmFormatSuggest'.
The internet suggest my machine is missing a codec.
But if I run the file, my machine is able to play the file with standard windows media player, so I guess it is on my machine.
When I put the file into https://www.metadata2go.com/ I can see that the problematic file has the codec pcm_s24le with a sample rate of 48000.
When I create a file with the same codec pcm_s24le but with a sample rate of 44100 the code does not trigger this error. So the problem seems to lie in the combination codec - sample rate.
Naudio's github page refers to this page with guides to fix the error.
it says under option 5 (i'm only playing back a wav file in my example)
What you need to do is search the web for an ACM codec that can decode
the format you are using. Unfortunately this is not always a simple
process,
It does unfortunately not provide any help on how or where to find these codecs. And, as I'm able to playback the file and the same codec with a different sample rate works fine, I'm also not sure this is the actual problem.
My code runs on a Windows 11 pro v21H2.
The following basic code (this is not my conversion code) throws the error
var outputDevice = new WaveOutEvent();
var audioFile = new AudioFileReader(path);
outputDevice.Init(audioFile);
outputDevice.Play();
Opening up the wav file and saving it with a different sample rate seems to solve the problem, but that is manual labor I would rather do without, since there are potentially a lot of files to convert in my case.
Any ideas would be appreciated.
It looks like Naudio has components to do the resampling of .WAV files, as noted on this page: How to Resample Audio with NAudio.
I am learning C#, and have created an application with winforms that can play video files from my local hard drive. Here is my code for playing a file that is stored in the same directory as my exe file.
My question is - the actual file is still accessible to anyone that can access my exe folder, and therefore, can play the video file just by clicking on it. Is it possible to embed the video file in such a way that the actual video file remain inaccessible to someone I give my application to use? I mean he/she should only be able to use the exe file to access the application and then play the video using the application.
I have read this post here to get some idea: How to store a video file in exe file in c# and play it
However, this answer is also getting the physical file url, converting to bytes from resource and then using the url to play. Therefore, if I had to give my application to someone else I would still have to copy the physical file to a folder, and again it remains accessible for an open-by-click option.
I am a complete newbie on this, so advance thanks for helping me out.
{
string exepath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
string vdopath = System.IO.Path.Combine(exepath, "MyFolder\\SecondFolder\\VideoFile.mkv");
axWindowsMediaPlayer1.stretchToFit = true;
axWindowsMediaPlayer1.URL = vdopath;
}```
It's possible to add any file to a project and from the Properties set the Build Action to "Embedded Resource", then to read the file this SO answer - https://stackoverflow.com/a/3314213/4000335
I'm using this solution to try to play an mp3 file in C# which I have inside a class library using this code: http://www.geekpedia.com/code111_Play-MP3-Files-Using-Csharp.html
I'm creating an instance, and calling the play method after opening a file and it doesn't play the mp3 file. Is this method deprecated nowadays as it dates back to 2008 or should this be working still?
Also worth pointing out, this is for a console application on Windows.
The code that is linked to in the question has to be run from an STA thread, otherwise it will not work.
i have the MediaElement and i want to play a video,
as i know the only way of doing so is to set this item Source
mediaElement1.Source = new Uri(fileName);
but now i have the resources file which i wanna play but i cant do so because it doesnt have a path.
so to make a long story short, i'm looking for a way to play video in the madiaElement from the resources file(without writing its byte first).
From first line in the "Remarks" section of the MediaElement docs: "When distributing media with your application, you cannot use a media file as a project resource. In your project file, you must instead set the media type to Content and set CopyToOutputDirectory to PreserveNewest or Always."
So you can't play video from a resource. You can play a file attached to a project as content.
Some alternate ways described here..
media-element-wont-play-a-video
Accessing the resources in the published application
Play embedded video resource as stream
How to play a video from WinForms resources in a axWindowsMediaPlayer?
I'm using the System.Media.SoundPlayer for playing sounds in C# desktop application passing the sound location.
Everything works fine. But how can I access the total file length (in milliseconds) and the player position?
Theses properties are accessible?
The System.Media.SoundPlayer class is not suitable for providing this information.
http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx
The SoundPlayer class provides a simple interface for loading and playing a .wav file. The SoundPlayer class supports loading a .wav file from a file path, a URL, a Stream that contains a .wav file, or an embedded resource that contains a .wav file.
You may want to look at some other libraries for audio playback, suggested here:
C# Audio Library