My project is to build a simple game and for now I only made the Main Menu Form1 with 5 buttons. The buttons have a MouseClick and MouseEnter on them, and I have a background music track using a WMP method (using WMPLib and axWMPLib).
My problem is when I'm taking the bin/debug putting it on a rar file and giving it to my friends, they say they don't hear the sound. I made the project trough WMP version 11, so I asked them if their WMP version is 11 and they said yes. I have no idea why I hear the sounds on my computer and they don't.
I tried to give them the folders:
bin\Release
bin\Debug
x86\Release
x86\Debug
but they still said that they can't hear any sound from all of them.
EDIT
All my sounds are in a folder called "Sounds". I found some details and found out that you need to embeding those WMP sounds to "Resources".
So how can I do that, and how I call them when Form1 loads up. And no, this following code doesn't work:
BackGround.URL = Properties.Resources.Invincible;
It says I can't convert System.IO.UnmanagedMemoryStream to String.
Sincerely I don't recommend you to use WMP, so give a look to the SoundPlayer class.
However I think the problem is that you don't give the correct location to your files. So what you can do is to locate your files in your application folder, get its location and create the location of the music files.
So try:
string musicName = Application.StartupPath + "music.mp3";
Or if you have a Sounds folder in the application path use:
string musicName = Application.StartupPath + "\\Sounds\\music.mp3";
Else if you insert your music file in the application resources this:
BackGround.URL = Properties.Resources.Invincible;
didn't work because BackGround.URL is of type string while Properties.Resources.Invincible is the music file stream.
I don't know if using WMP you can set the stream from where it can play the file. Although the SoundPlayer class I linked previously contains a property from where you can set the input stream. You can do it in this way:
SoundPlayer mySoundPlayer = new SoundPlayer();
mySoundPlayer.Stream = Properties.Resources.Invincible;
mySoundPlayer.Load();
mySounPlayer.Play(); //plays the Properties.Resources.Invincible sound
Related
Sorry I'm completely new to Unity so I'm probably asking a stupid question. How would I play a video content from a different computer? I know that I would have to establish the remote access connection to access the specific video file located in another computer and then use the filepath of the stored video as the sourcepath to play the video from.
I guess I'm not really sure of which function to use in Unity exactly. I saw posts about movietexture function in unity, but is there any other way or can I just pass in the filepath parameter to movietexture and it will automatically start playing from the source path? Thanks
After reading your comment, it looks like all you have to do is share the folder that contains the videos file in Ubuntu. This is how you Share the folder.
Usually to access a network file, you use double backlash followed by the computer name then the file directory.
\\computername\filepath\videofilename.mp4
To use this in a code you have to escape the \ by adding another one in front of it. One \ becomes \\ and two \\ becomes \\\\.
The url to use in your code should look something like this:
"\\\\servername\\filepath\\videofilename.mp4"
This post describes is how to play video in Unity. You have to download Unity 5.6 before you can use that.
Get the script then replace:
videoPlayer.source = VideoSource.VideoClip;
with:
videoPlayer.source = VideoSource.Url;
videoPlayer.url = "\\\\servername\\filepath\\videofilename.mp4";
Situation
this is probably a really simple question with a really simple answer, but I can't find the answer anywhere, so I'm going to ask here, I've been working around this because it isn't a huge issue for me, but it will be when I submit my work.
Problem
I have a soundplayer, it works fine on my PC and does exactly what I want it to do on this PC.
SoundPlayer player = new SoundPlayer(#"C:\Users\Font\Desktop\GRADED_UNIT_SOLUTION_PLANNING_UPDATED\HillRacingGraded\HillRacingGraded\Resources\Audio\" + track + ".wav");
The problem is the path.
because of THIS path my program won't work on ANY other System apart from the one it's working on right now. It crashes soon after startup.
And.. as you can probably see from the path, it's going to eventually be graded, so my lecturer will need to use this program without having to switch around a directory.
How can I get the Soundplayer path to start at "HillRacingGraded\ ...\ ..."?
Rather than it starting at the C: drive.
If you're using System.Media.SoundPlayer, it supports reading of sound files from streams and I've seen it successfully used in the past from the UnmanagedResourceStream you get from an embedded resource. So one option would be to embed your sound file as a resource in your application and play it from there.
If embedding the file isn't an option, you can get paths relative to your executable folder using code similar to that shown below. Then you just have to provide your executable and the resource files in a subdirectory (but be careful with GetExecutingAssembly() if there's any chance your code is in a DLL and can be hosted by an arbitrary executable).
var assembly = Assembly.GetExecutingAssembly();
var folder = Path.GetDirectoryName(assembly.Location);
var soundFile = Path.Combine(folder, "MySoundFile.wav");
The special folder path helpers can also reduce the hard-coding but work best when your application will be installed via an installer and you can put files in the appropriate places automatically (which isn't likely to be the case for a school project).
For me (VS 2022, .net 6, C # 10) it worked:
Import the "file.wav" file into the main directory.
Change in: Properties (file.wav) - Copy to Output Directory to: Copy always.
Later it was enough to:
SoundPlayer player = new SoundPlayer("file.wav");
player.Load ();
player.Play ();
I use these following codes to play music while clicking the button.
private void button1_Click(object sender, EventArgs e)
{
SoundPlayer s = new SoundPlayer();
s.SoundLocation = #"f:\1.wav";
s.Play();
}
But these codes are only for my computer what should i do so that i can play this sound on the other computers like database. what should i do so that i can play this song in other computers?
Thanks in advance
You can import the file in the project's resources and load it from there using
Properties.Resources.<name_of_resource>
In order to import something to the resources do the following (assuming you use Visual Studio 2010 - it is similar to other versions I think):
In Visual Studio 2010 solution explorer, right click Properties -> Open -> Resources -> Add Resource -> Add Existing File
Note that when you will install your application later on other computers the resources will be installed as well.
In order to play the sound you need to do the following then:
SoundPlayer myPlayer = new SoundPlayer(yourNamespace.Properties.Resources.mySound);
myPlayer.Play();
You can do something like, get the file content as Stream from the database
You can do something like, get the file content as Stream from the database or remote system and pass the stream to the SoundPlayer instance.
You can store the sounds in a subfolder of the applications and access them via file's relative path (I believe, but I'm going to look it up precisely, it's Application.StartupPath in a winforms environment).
Or you could make them resources (see gkaran89's answer).
I would like to know, if there's a way to play a media-file in Windows Phone, while this file is still being downloaded?
At the current time, I have an application with feature, that allows to download and play media files, when they are fully downloaded.
(I've created this feature, using article)
So, in brief, first, the media-file is downloaded from url, then I'm using mediaElement to play it:
mediaElement.source = isolatedStorageFileStream;
After that, I can use this file via my player UI.
However, I prefer to make this feature better - allow to start playing file, while the download is still in progress. In the end, this should look like that: start downloading file, contemporaneously start decrypting this file, contemporaneously allow to play decrypted part of file. Let's forget about decryption so far(I've already written a method for a downloaded file, it should be slightly changed then), the main problem is how to allow two parallel tasks: downloading and playing.
The first question is: "Does MediaElement only allow to set "source" property to only solid(finally downloaded) files or it can use this Stream, during downloading, as well"?
Because, I've found some info, that:
"The MediaElement does not supporting streaming at the moment. It
loads the entire file first before it actually plays it."
, though I'm not sure, that this one refers to Windows Phone, it sounds rather anxiously.
If this cannot be done, what else can I use to work with media-files? I'm not interested in downloading video, let's limit only with mp3 audio files.
Right now, the download process is implemented in this way:
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri(link));
Then in OpenReadCompleted the download file stream is set as source of mediaFile.
mediaFile.SetSource(isolatedStorageFileStream);
After this I can use my media file.
I there a solution for my problem? I'm rather new at Windows Phone threading and streaming and don't know, how to implement such feature.
P.S. Of course, I tried to find a similar question, but failed. If you know one, give me a link, please.
P.P.S. Sorry for my English.
You can only stream it using BackgroundAudioPlayer.
you are trying to read a file that is in use. I don't think is possible.
However:
VideoRecorder works by using filesink and managed to capture, write and display at the same time. would suggest you have a look in that area
How to: Record Video in a Camera Application for Windows Phone
http://msdn.microsoft.com/en-us/library/hh394041(v=vs.92).aspx
FileSink
http://msdn.microsoft.com/en-us/library/system.windows.media.filesink(VS.95).aspx
I'm trying to get background music going for my XNA game, but the options in XNA all require that the audio files be converted to .wav and included as project resources. This doesn't work for me for two reasons. First, .wav files are enormous, and have no real benefit to the user. Second, the user would be unable to have a folder of mp3s to substitute, should they prefer their own music. What are my options for playing mp3s that are not actually part of the project?
After doing a lot of searching, I finally found this thread on the App Hub forums which gave me a solution.
You can use Song.FromUri to load a song, then use MediaPlayer.Play(song) to play said song.
Uri songPath = new Uri("Songs/song.mp3", UriKind.Relative);
Song song = Song.FromUri("song", songPath);
MediaPlayer.Play(song);
In your question you said (quite obviously) that you don't want to use MediaPlayer, but it seems as though the reason why is because you would first need to convert the song and it needs to be included in the project resources. With this, the song would only need to be in a "Songs" directory (which would be located in the same directory as the executable).
Using the SoundEffect class wouldn't work either, since you'd have to use TitleContainer.OpenStream, which liked to complain when I tried it.