I create an application for android via Monogame (C#). In order to reduce my .apk size I decied to don't use Content Pipeline for audio. I found that I can use raw mp3 with Song.FromUri() method. Using next code
Song mySong;
//...
mySong = Song.FromUri("track01", new System.Uri("audio.mp3", System.UriKind.Relative));
//...
MediaPlayer.Play(mySong);
I got and exception "Unhandled Exception: Java.IO.FileNotFoundException: track01" on
MediaPlayer.Play(mySong);
, but I have file "audio.mp3" in main folder.
Solution Screenshot
Changing property "Build Action" to "None"/"Content"/"AndroidAsset" and "Copy to output directory" to "Copy always" didn't bring anything new.
I searched the solution, but it looks like nobody has the same problem or they solve it easily. The only similar problem I meet here, but their solution didn't help.
Do you have any ideas that can help me? Thanks.
I'm not sure about Song.FromUri Method, but this is how I'm using sound files.
var player = new MediaPlayer();
var fd = global::Android.App.Application.Context.Assets.OpenFd("sounds/Wrong.mp3");
player.Prepared += (s, e) =>
{
player.Start();
};
player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
player.Prepare();
I have Kept my Wrong.mp3 in Assets/sounds Folder
Is it not a possibility to put the audio file into the resources?
If you open your project properties, there should be a resources tab. Within this tab, you should be able to add different values (strings, images, etc) including audio files. If you've added this here, it should be reachable through the resource manager.
Let me know if this works out!
Related
I am trying to play a sound file in my WPF application. Currently I have the following call:
private void PlaySound(string uriPath)
{
Uri uri = new Uri(#"pack://application:,,,/Media/movepoint.wav");
var player = new MediaPlayer();
player.Open(uri);
player.Play();
}
Now if I specify Media/movepoint.wav as build action Content and load it as a relative or absolute file path it works fine, so I suspect this has something to do with the Pack URI, but I cannot for the life of me figure out what.
The objective is to store the file as a resource so that its not available in the output directory. I can provide either the WAV copy or the MP3 copy.
I tried this with an image file, which works the same as a sound file as far as the uri is concerned because it's just another resource. I used the code below which essentially matches what you have.
new Uri(#"pack://application:,,,/Resources/logo.png")
Make sure that your 'Media' folder is not nested in any other folder. If it is, you need to include that folder as well.
Using .NET Framework 4.0, VS2012.
This link gives a pretty good description of the whole "pack" scheme of things.
EDIT
More research on this topic seems to indicate that what you want to do might not be possible with audio or video files. The excerpt below is taken from the remarks section of this MSDN page.
Although you can declare an instance of this class in Extensible
Application Markup Language (XAML), you cannot load and play its media
without using code. To play media in XAML only, use a MediaElement.
Also, if you declare an instance in XAML, the only practical use is to
fill property element syntax for the Player property.
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.
MediaPlayer can be used in two different modes, depending on what is
driving the player: independent mode or clock mode. In independent
mode, the MediaPlayer is analogous to an image and the media opened
through the Open method drives playback. In Clock mode, the
MediaPlayer can be thought of as a target for an animation, and thus
it will have corresponding Timeline and Clock entries in the Timing
tree which controls playback. For more information on media modes, see
the Multimedia Overview.
MediaPlayer is different from a MediaElement in that it is not a
control that can be added directly to the user interface (UI) of an
application. To display media loaded using MediaPlayer, a VideoDrawing
or DrawingContext must be used.
The following seems to work in .NET Framework 4.5:
var sri = Application.GetResourceStream(new Uri("pack://application:,,,/MyAssemblyName;component/Resources/CameraShutter.wav"));
if ((sri != null))
{
using (s == sri.Stream)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(s);
player.Load();
player.Play();
}
}
CameraShutter.wav is embedded as Resource in my project (and resides inside Resources subfolder, as indicated in the pack URI).
You can also load a Stream into the SoundPlayer if the .wav file is an Embedded Resource. Note that in this example the resources are in a folder called Resources that is in the root of the project, that is why it is written {0}.Resources.{1}.
//the wav filename
string file = "emergency_alarm_002.wav";
//get the current assembly
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
//load the embedded resource as a stream
var stream = assembly.GetManifestResourceStream(string.Format("{0}.Resources.{1}", assembly.GetName().Name, file));
//load the stream into the player
var player = new System.Media.SoundPlayer(stream);
//play the sound
player.Play();
This also seems to work and is maybe simpler to remember than that long line of pack or assembly stuff..
I opened the Resources.resx and dragged a sound file in there called aa_pickup.wav Then set the properties of it to Copy to Output Dir = Copy Always.
System.IO.Stream str = Properties.Resources.aa_pickup;
System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str);
snd.Play();
Now.. if I could only work out how to change the volume.
Hello Stack Overflow community,
I have been working on making a custom installer/launcher for the game I am making, it is currently in working in a non-portable context, however if I want to put it inside of a game for distribution, it must work inside of a portable context (i.e. it should not access the drive for any of it's own needs, only the other software's needs)/
At the current moment it loads a song from the drive to play, and as well loads the prerequisites for the game if the launcher has never been opened before or did not finish successfully. The files are all set to "EmbeddedResource" inside of SharpDevelop, and they are part of the final compiled script.
However, in the code's current context, the script still has to access the drive to do all of those functions, even when they are embedded into the final program.
The current code I have so far is below, "programpath" refers to the directory of which the file is being executed from, "MainText" is the main output window, which is better seen in this Stack Overflow question, and label1 is a debug line, that is only used to show the path of the current running command (will be removed when all the things are embedded).
public MainForm()
{
//Open Window
InitializeComponent();
CenterToScreen();
//Start song
System.Media.SoundPlayer player = new System.Media.SoundPlayer(programpath+"\\Resonance.wav");
player.PlayLooping();
this.Shown+=(s,e)=>{
if(File.Exists(programpath+"\\FirstLaunch.lic")&&File.ReadAllText(programpath+"\\FirstLaunch.lic").IndexOf("Installation Successful")>=0){
BackColor=System.Drawing.Color.OrangeRed;
MainText.BackColor=System.Drawing.Color.OrangeRed;
MainText.ForeColor=System.Drawing.Color.Aquamarine;
MainText.Text="Starting game..";
Process game = new Process();
//placeholder executable, will be finished game executable
game.StartInfo.FileName="D:\\UT2004\\System\\UT2004.exe";
game.StartInfo.ErrorDialog=true;
game.Start();
//stop playing music,
player.Stop();
//when game stops, close the launcher
game.WaitForExit();
Application.Exit();
}else{
//Start prerequisite installation
newLaunch();
}
};
}
//if the launcher has not been open before OR the last installation was not successful
void newLaunch(){
//Creates and makes a stream to the file
StreamWriter writer = new StreamWriter(programpath+"\\FirstLaunch.lic");
//Changing color scheme
BackColor=System.Drawing.Color.Chartreuse;
MainText.BackColor=System.Drawing.Color.Chartreuse;
MainText.ForeColor=System.Drawing.Color.Black;
MainText.Text="Configuring Prerequisites....\nInstalling DirectX";
//write to file about stage
writer.Write("Installing DirectX");
//start DirectX installer
Process prerequisite = new Process();
prerequisite.StartInfo.FileName=programpath+"\\dxwebsetup.exe";
prerequisite.StartInfo.ErrorDialog=true;
prerequisite.Start();
//debug line
label1.Text=programpath+"\\dxwebsetup.exe";
//wait for installer to finish and close
prerequisite.WaitForExit();
//remove refernce to DirectX installer
prerequisite.Close();
//write to file about stage
writer.WriteLine("...true");
//Changing color scheme
BackColor=System.Drawing.Color.DarkMagenta;
MainText.BackColor=System.Drawing.Color.DarkMagenta;
MainText.ForeColor=System.Drawing.Color.Yellow;
MainText.Text="Configuring Prerequisites....\nInstalling Microsoft VC++2015 Update RC3";
//write to file about stage
writer.Write("Installing VCRedist");
//start VC Redistributable installer
prerequisite.StartInfo.FileName=programpath+"\\vc_redist.x86.exe";
prerequisite.StartInfo.ErrorDialog=true;
prerequisite.Start();
//debug line
label1.Text=programpath+"\\vc_redist.x86.exe";
//wait for installer to finish and close
prerequisite.WaitForExit();
//remove reference to VC Redistributable installer
prerequisite.Close();
//write to file about stage
writer.WriteLine("...true");
writer.WriteLine("Installation Successful");
writer.Close();
//re-open launcher from open context
label1.Text=programpath+"\\ColorLoop.exe";
Process.Start(programpath+"\\ColorLoop.exe");
Application.Exit();
}
}
}
How do I get the program to play the music, and load the pre-requisites from itself and not from separate drive files inside the code. It is all already embedded, just not being used.
I finaly figured it out, so I am answering my own question in the hopes that people who are struggling in the future can figure it out.
Using the link provided by #Jimi for Accessing Resources in SharpDevelop, and scrolling down to the "Embedding Files directly" section, shows the context at which to access Embedded Resource. The thing to note here is that the return type of GetManifestStream(string) is a System.IO.Stream object.
This is important as we need SoundPlayer to accept a System.IO.Stream object some how. This can be done two ways, by using an overloaded constructor or Property of the SoundPlayer class. As defined by the MSDN page for the SoundPlayer Class.
However, when I tested this, I could not get the sound to play. Furthering my research, I found that Assembly.GetExecutingAssembly() has a accessible method named "GetManifestResourceNames()" that returns a string array of all the resources and their corrective names to access them. I was able to see the return by creating a Windows Form Label called "ExecutingAssem", and using the instructions from DotNetPerls to create a static method named "ConvertStringArrayToStringJoin()", but changing its seperator from "." to "|" to better read the assets.
With that in place the final program shows the list of embedded resources with the line:
ExecutingAssem.Text=ConvertStringArrayToStringJoin(Assembly.GetExecutingAssembly().GetManifestResourceNames());
And the final program appearing as:
Program showing all the resources in the corner
The interesting thing here is that the song resource (Resonance) is not actually a part of the Namespace, or MainForm...but rather its own name as the resource title.
With that missing information found, I needed to change the program from reading the drive to reading the manifest stream. Using the overloaded constructor that utilizes a System.IO.Stream object as a parameter instead of a string to a file location, I changed where the player object is initiated to use that constructor.
SoundPlayer player =new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("Resonance"));
Finally the final application played the song with the ability to move the .exe file elsewhere and still be able to play the same song without "Resonance.wav" needing to be in the same directory.
private void button1_Click(object sender, EventArgs e)
{
SoundPlayer player = new SoundPlayer();
//Add TestSound.wav file to the built-in resource file Project>Properties>Resources.resx
player.Stream = Properties.Resources.TestSound;
//Add TestSound.wav file to a new resource file Resource1.resx
//player.Stream = Resource1.TestSound;
player.Play();
}
I have this chunk of code:
private void button4_MouseEnter(object sender, MouseEventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"Resources/navigation.wav");
player.Play();
}
And I get FileNotFoundException, but navigation.wav is in Project/Resources. Plese help!!!
This looks for the file from your Bin\Debug Folder
You have couple of options:
Right click the file and pick Properties. Select for BuildAction = Content.
You will find the file under Bin\Debug\Resource\Sound.wav
Right click the file and pick Properties. Select for BuildAction = Embedded Resource.
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "NamespaceName.FolderName.Sound.wav";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
var wave = new WaveFileReader(stream);
Console.WriteLine(wave.TotalTime);
}
The path is determined relatively from the executable, so in this case probably Bin\Debug.
Try to add the resource in your application as Content (it copies the file to Bin\Debug). That should work.
That path is relative to the directory in which the application is currently running. If you hit F5 in Visual Studio this is most probably bin/Debug, so the file should be there.
Consider embedding this resource, or setting Copy to output directory property to "copy always".
To start with, you need a backslash: #"Resources\navigation.wav"
If this doesn't help, then most likely you are running your application from a different directory than you think. Are you running in debug mode from VS? Is your file in Project\bin\Debug\Resources then?
How can I play a sound from the
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal
folder?
I have tried:
string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "Sounds/Twing.mp3");
CCSimpleAudioEngine.SharedEngine.PlayEffect(path);
But it doesn't work
you get an exception ?
i assume that the specified sound is loaded via the content pipleline,
so you would need to add that to your content project.
So if you see ContentPipeline, ContentLoader or something in the stack trace, this may be the correct answer.
I dit not find a way to directly pass the audio.
With images that is possible.
I'm new to Windows Phone development and I'm trying to do a simple training app.
I want my app to load some audio files that I've put into a folder inside the project.
Here's a short snippet:
private void LoadSound(String SoundFilePath, out SoundEffect Sound)
{
// For error checking, assume we'll fail to load the file.
Sound = null;
try
{
// Holds informations about a file stream.
StreamResourceInfo SoundFileInfo = Application.GetResourceStream(new Uri(SoundFilePath, UriKind.Relative));
My folder structure is something like
- Project
- Kits
- TestKit_128
- Bass_126.wav
and calling
Button.setSound("Kits\\TestKit_128\\bass_126.wav");
throws a System.NullReferenceException because the path is not found when the URI is created. (At least I think so!)
What should I do?
Is there any way to load files from a folder in the project or to copy them into the IsolatedStorage when I run the app for the first time?
Thanks
EDIT:
I've just opened the XAP file with WinRar and there's no "Kits" folder so I guess that my problem is how to make it add the folder to the XAP file.
I think you have to add the folder and the file to your project (i assume you are using Visual Studio). Try rightclick on your solution and add an existing object (in this case your wav file).
Did you set the build action of the wav file to "Content"? Right click on the WAV file --> properties --> build action == content.
Try this:
Uri uri = new Uri("Kits/TestKit_128/bass_126.wav", UriKind.Relative);//Above the Kits folder only the project itself.
StreamResourceInfo sr = Application.GetResourceStream(uri);
try
{
using (Stream stream = sr.Stream)
{
//working here with your data image or wav or whatever you want from URI
}
}
catch (Exception)
{
}
This is for resource inside project and working good for me. Also prevoius note about "Content" is right, it wouldn't work without it. I think the problem is double-slashes in address.