'm working on playing a audio file and planning to play two parallel playing of files so chose "SoundEffect" for playing audio file.
Microsoft.Xna.Framework.Audio.SoundEffect
SoundEffect back;
back = ContentManager.Load<SoundEffect>("/Sounds/Background/bkm.mp3")
'm not getting that Load object.
Getting this error-->
An object reference is required for the non-static field, method, or
property
'Microsoft.Xna.Framework.Content.ContentManager.Load(string)'
Thanks
The content loading must occur inside the main Game class' LoadContent() method. Put your second line in there and try to run it.
Also your path to the file contains errors. It must look either like this:
"\\Sounds\\Background\\bkm"
or like this:
#"\Sounds\Background\bkm".
Don't include file extension and use \ backslash in paths for content manager.
Also it's a great idea to run this check first if you stumble upon File not found exception:
if (System.IO.File.Exists("\\Sounds\\Background\\bkm.mp3"))
{
// if you can step into this, it means the file exists
}
An object reference is required for the non-static field, method, or property 'Microsoft.Xna.Framework.Content.ContentManager.Load(string)'
So, create an instance:
var contentManager = new ContentManager();
var back = contentManager.Load<SoundEffect>("/Sounds/Background/bkm.mp3");
try this maybe:
SoundEffect back;
var Cm= new ContentManager();
back = Cm.Load<SoundEffect>(#"/Sounds/Background/bkm.mp3")
Related
I'm using libvlc in a .netcore project to simply play .ogg and .wav files like this:
using Vlc.DotNet.Core;
...
namespace Test
{
public partial class MainWindow : Window
{
public MainWindow()
{
string vlcPath;
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\VideoLAN\VLC"))
vlcPath = key.GetValue("InstallDir").ToString();
vlcPlayer = new VlcMediaPlayer(new DirectoryInfo(vlcPath));
vlcPlayer.Play(songFilename);
...
I would think doing something like vlcPlayer.SetMedia(songFilename) before using the .Play() method, would set the vlcPlayer object members with info relevant to the clip, but it doesn't. Is there an uncomplicated way to get duration using this library?
First, I needed to use the .Parse() method after setting the player's media item like this:
FileInfo info = new FileInfo(songFilename);
vlcPlayer.SetMedia(info);
vlcPlayer.Parse();
But, it appears there is a bug in the latest Vlc.DotNet.Core (v3.1.0), such that the .Parse () method does not work as it should. For one, there is no class member to find out the state of the parsing operation, and it never seems to complete if I take a look at the Duration property, which never changes from -1.
Instead, I found LibVLCSharp that works great. Here's the code that worked for me that allowed me to work with the media item outside of a player object altogether:
var media = new Media(libvlc, new Uri(songFilename));
media.Parse();
while (media.ParsedStatus != MediaParsedStatus.Done) Thread.Sleep(10);
Afterwards, media.Duration is correct.
I'm working on a Cities: Skylines mod and I want to access the sharedassets.assets file(s) the game has in the Data folder programmatically to get a mesh/prefab.
I've found a tool called Unity Assets Bundle Extractor (UABE) and it is able to open up these files and extract the mesh.
Is there a way to extract a mesh from the sharedassets programmatically with C# code like UABE does?
I've looked in the Unity documentation but so far only have seen this page (not sure if relevant): https://docs.unity3d.com/ScriptReference/AssetBundle.LoadFromFile.html
I tried adapting the code from there but I haven't had any success so far, only have had not found error messages
var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.dataPath, "sharedassets11"));
Is there a way to achieve this? Thanks
Look at the API for AssetBundle.LoadFromFile.
There is a second method AssetBundle.LoadAsset (or alternatively also maybe AssetBundle.LoadAllAssets) you will need:
var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.dataPath, "sharedassets11"));
if (myLoadedAssetBundle == null)
{
Debug.Log("Failed to load AssetBundle!");
return;
}
var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("NameOfTheAccordingObject");
Instantiate(prefab);
myLoadedAssetBundle.Unload(false);
Using an android scanner device, running KitKat.
Using Xamarin in Visual Studio Enterprise 2017, 15.9.9.
I need to generate a "Success" or "Error" sound, based on the content of the scanned barcode.
I have two files: "Success.mp3" and "Error.wav", neither of which will play.
Since this should be a very simple process, I am trying to use Android's MediaPlayer class, rather than add some NuGet package.
I am using Dependency Injection to properly run the Android code, since Xamarin does not have any sort or media API.
I instantiate Android's MediaPlayer as variable "player", and it does successfully instantiate, but as soon as I attempt to do anything with it, it throws a Null Exception error and the value of "player" displays as Null.
I have been experimenting with different ways to do this and have copies of the sound files stored both in the Assets folder, and the Resources/Raw folder (see below).
Here is my method:
public void PlaySound(string soundType) {
var filename =
global::Android.App.Application.Context.Assets.OpenFd(soundType);
if (player == null) {
MediaPlayer player = new MediaPlayer();
}
//This is where the error happens
player.SetDataSource(filename);
player.Prepare();
player.Start();
}
I have also tried the last three lines as the following, with the same result:
player.Prepared += (s, e) => {
player.Start();
};
player.SetDataSource(filename.FileDescriptor, filename.StartOffset,
filename.Length);
player.Prepare();
I have also attempted to utilize what so many people demonstrate as the way to do this, but it does not work for me. This is where the file must be stored in Resources/Raw:
player = MediaPlayer.Create(global::Android.App.Application.Context,
Resource.Raw.someFileName);
Whatever value that you use for "someFileName", all Visual Studio gives you is "'Resource.Raw' does not contain a definition for 'someFileName'".
Resource.designer.CS does contain entries for both files:
public const int Error = 2131230720;
public const int Success = 2131230721;
Expected results: sound, or some meaningful error message that puts me on the right path.
I am still relatively new to Xamarin and am probably missing something that would be obvious to veteran eyes. I have tried so many other things, most of which are not mentioned here, grasping for some straw. This should be simple, but is proving otherwise. Thank you for any help that you can provide.
I'm attempting to import a model created in Keras/Tensorflow and use it for inference in a Unity project.
I have successfully imported the model and validated by printing names of input and output nodes in the graph. Though, when I try to get the output value from the runner, I get this exception.
TFException: Attempting to use uninitialized value action_W
[[Node: action_W/read = IdentityT=DT_FLOAT, _class=["loc:#action_W"], _device="/job:localhost/replica:0/task:0/cpu:0"]]
TensorFlow.TFStatus.CheckMaybeRaise (TensorFlow.TFStatus incomingStatus, System.Boolean last) (at <6ed6db22f8874deba74ffe3e566039be>:0)
TensorFlow.TFSession.Run (TensorFlow.TFOutput[] inputs, TensorFlow.TFTensor[] inputValues, TensorFlow.TFOutput[] outputs, TensorFlow.TFOperation[] targetOpers, TensorFlow.TFBuffer runMetadata, TensorFlow.TFBuffer runOptions, TensorFlow.TFStatus status) (at <6ed6db22f8874deba74ffe3e566039be>:0)
TensorFlow.TFSession+Runner.Run (TensorFlow.TFStatus status) (at <6ed6db22f8874deba74ffe3e566039be>:0)
RecordArbitraryData.ModelPredict (System.Single[,] input) (at Assets/Scripts/Spells/RecordArbitraryData.cs:230)
RecordArbitraryData.FixedUpdate () (at Assets/Scripts/Spells/RecordArbitraryData.cs:95)
Here are the two functions I use. InstantiateModel is called OnStart() in my unity script. And ModelPredict is called when the user passes an input to the script.
void InstantiateModel(){
string model_name = "simple_as_binary";
//Instantiate Graph
graphModel = Resources.Load (model_name) as TextAsset;
graph = new TFGraph ();
graph.Import (graphModel.bytes);
session = new TFSession (graph);
}
void ModelPredict(float[,] input){
using (graph) {
using (session) {
//Assign input tensors
var runner = session.GetRunner ();
runner.AddInput (graph [input_node_name] [0], input);
//Calculate and access output of graph
runner.Fetch (graph[output_node_name][0]);
Debug.Log ("Output node name: " + graph [output_node_name].Name);
float[,] recurrent_tensor = runner.Run () [0].GetValue () as float[,];
//var results = runner.Run();
//Debug.Log("Prediciton: " + results);
}
}
}
Any help appreciated - TensorflowSharp is very new to me.
I was able to figure out most of my problems. I'm currently at a point where my model is predicting in unity, but only predicting the first of four classes. My guess is it has something to do with the weights not getting initialized correctly from the checkpoint files? Edit: My values weren't being normalized before being passed to neural network.
Preface: Mozilla Firefox works best for displaying tensorboard; it took me a long time to realize that google chrome was causing my graph to be invisible (tensorboard is how I was able to figure out the nodes that needed to be used for input and output).
First Issue: I was renaming a .pb file into a .bytes file. This is incorrect because the model’s weights come from the checkpoint file, and are given to the nodes held in the .pb file. This was causing the uninitialized variables. These variables were used for training, which were removed after using the freeze_graph function.
Second Issue: I was using the file created called ‘checkpoint’, which was throwing an error. I then changed the name of the checkpoint to ‘test’ and used this in the freeze_graph function. When calling the checkpoints file, I was required to use ‘test.ckpt’. I assume this function knows to grab the three files automatically based on the .ckpt? ‘Test’ without ‘.ckpt’ did not work.
Third Issue: when using the freeze_graph function, I needed to export the .pb file in keras/tf with text=False. I tested True and False; True threw an error about “bad wiring”.
Fourth Issue: Tensorboard was very difficult to use without any organization. Using tf.name_scope helped a lot with not only visualization, but making sure I was using/referencing the correct nodes in TensorFlowSharp. In keras I found it helpful to separate the final Dense layer and Activation into their own scopes so I could find the correct output node. The rest of my network was put into a ‘body’ scope, and the sole input layer in ‘input’ scope. The name_scope function prepends ‘scopename/’ to the node name. I don't think it’s necessary, but it helped me.
Fifth Issue: The version of tensorflowsharp released as a unity package is not up to date. This caused an issue with a keras placeholder for ‘keras_training_phase’. In keras, you pass this along with an input like [0] + input. I tried to do the same by creating a new TFTensor(bool), but I was getting an error ‘inaccessible due to its protection level. This was an error with the implicit conversion between bool and TFTensor in my unity TensorFlowSharp version. To fix this I had to use a function found in this stackoverflow solution where the .bytes file is read in, the placeholder for keras_training_phase is found, and is swapped out for a bool constant set to false. This worked for me because my model was pretrained in python, so it may not be a great fix for someone that’s trying to train and test the model. A condition for removing this node with the freeze_graph function would really be useful.
Hope someone finds this useful!
the problem I am facing is that i cant seem to get the following code working...
SimpleStream stream = new SimpleStream("https://stream.twitter.com/1.1/statuses/sample.json");
stream.StreamStarted += (sender, args) => Console.WriteLine("Stream has started!");
// Starting the stream by specifying credentials thanks to the Token
//stream.AddTrack("usa");
stream.StartStream(token, x => richTextBox1.AppendText(x.Text));
The error that I get is "An object reference is required for the non-static field, method, or property"
richTextBox is part of my form cs file and I tried changing it to static in the Designer.cs file it compiles however nothing happens on AppendText method
The error indicates that you're trying to access an instance member of your class (presumably richTextBox1) from a static method\property. You need to change the method to be non-static (so it has access to the local instance members).
If this doesn't help, you will need post the details of your class\method.