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.
Related
I program C# Android app using Xamarin. I wrote this code:
protected MediaPlayer player;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout1);
this.Window.AddFlags(WindowManagerFlags.Fullscreen);
player = new MediaPlayer();
player.Reset();
var fileDescriptor = Assets.OpenFd("MySound.mp3");
player.SetDataSource(fileDescriptor.FileDescriptor);
player.Prepare();
player.Start();
}
MySound.mp3 file is direct in the Assets folder. When I run app there is an error:
Java.IO.IOException
Message=Prepare failed.: status=0x1
in the line with player.Prepare();
What is wrong? Why it's no working?
This seems to be a common exception with MediaPlayer.Prepare() on Android devices. A quick search turned up this:
https://social.msdn.microsoft.com/Forums/en-US/15f9c371-1b8d-4927-9555-f40e2829c377/mediaplayer-prepare-failed-stauts0x1?forum=xamarinandroid
Quote by Anonymous:
Hi, I wasted a lot of time, trying to find a solution to this issue on
my side too. So I post here, just in case it is helping some people.
My situation : I wanted to load an audio file from my assets (so not
registered in my resources). I am using a similar code as Ike Nwaogu,
except that I am using an AssetFileDescriptor to open my file (in my
activity class code, so I have access to "Assets") :
string path = "Audio/myfile.ogg";
Android.Content.Res.AssetFileDescriptor afd = Assets.OpenFd(path);
MediaPlayer soundPlayer = new MediaPlayer();
if (afd != null)
{
soundPlayer.Reset();
soundPlayer.SetDataSource(afd.FileDescriptor);
soundPlayer.Prepare();
soundPlayer.Enabled = true;
afd.Close();
}
I was failing on the Prepare(). I tried to add the access to external
storage permissions (but it did make sense since it
was loaded from my assets directly, I tried just in case).
Just by chance, by seeing other people samples on the forums, I added
the afd.StartOffset, afd.DeclaredLength to the parameters:
soundPlayer.SetDataSource(afd.FileDescriptor, afd.StartOffset,
afd.DeclaredLength);
and it worked ... I don't know if it just luck and if is going to fail
again later or if there is a bug in API ...
According to various sources, using getStartOffset and getLength when setting the DataSource should fix this:
player.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
Alternatively, here are a few more ideas:
Android MediaPlayer throwing "Prepare failed.: status=0x1" on 2.1, works on 2.2
https://forum.processing.org/two/discussion/6722/andriod-mediaplayer-prepare-failed-status-0x1
I am using Uno Platform to make an app which access a specific folder inside the user's Documents library from a game (BeamNG.drive). I want the app to read all the mod files inside this folder to be able to edit and display them to users on startup. I installed the Nito.Mvvm.Async Package to help me bind everything to the UI.
Here's part of the method that loads the mod files from the Documents folder:
public static async Task<List<Mod>> GetModList()
{
StorageFolder documents = await KnownFolders.DocumentsLibrary.GetFolderAsync("BeamNG.drive");
IReadOnlyList<StorageFile> fileList = await documents.GetFilesAsync();
List<Mod> foundModsList = new();
foreach (StorageFile file in fileList)
{
//...
}
return foundModsList;
}
Here's the code on MainPage.xaml.cs inside the Shared project in my solution, based on code from this answer
public NotifyTask<ObservableCollection<Mod>> ModsData { get; }
public MainPage()
{
InitializeComponent();
ModsData = NotifyTask.Create(InitModData());
}
private static async Task<ObservableCollection<Mod>> InitModData()
{
return new(await ModManager.GetModList());
}
The GetModList() method is called, but the GetFolderAsync("BeamNG.drive") method never returns, and the app keeps running normally (not UI freezes or anything). If I add a breakpoint in that line, Visual Studio stops there normally. But if I press "Step Over", instead of continuing on that method, VS jumps to this line...
return new(await ModManager.GetModList());
...then this one:
ModsData = NotifyTask.Create(InitModData());
Using ConfigureAwait(false) in any of the calls using await doesn't help anything. I'm really not sure what is going on and I suspect that Nito.Mvvm.Async might have something to do with it (considering its last update was in 2017) but I'm really not sure.
From your question it seems this problem occurs under .NET 5 - meaning targeting WebAssembly or Skia targets of Uno Platform. Under Uno, the KnownFolders type is not yet supported, so accessing DocumentLibrary is not possible. If you want to have this supported, please file an issue on Uno Platform GitHub.
In case of UWP, to access the Documents library, you need to declare a special capability in app manifest (see Docs). However, it is a restricted capability and it is quite likely that if you utilize it, the app will not pass Microsoft Store certification. Instead, it is recommended to use FolderPicker instead and let the user decide on the location when files are stored, or to use ApplicationData.Current.LocalFolder to store the data for the app privately.
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);
I'm basically asking the same thing that was asked here, however, that question was asked 8 years ago and the answer is no longer applicable to UWP.
I have a audio stream with http://someurl.com/stream that streams in audio/ogg format. I would like to be able to play that from an UWP app.
I see the NAudio library recommended a lot (after all, it's used in the above example), however it's very larger and has fairly lackluster documentation and very few up-to-date examples (they used to have a streaming example, but from what I'm able to download off Codeplex, it was replaced with a regular local-file player example). I'm not experience enough to make sense of the little documentation and example code they do have.
I'm honestly not even sure where to begin. I've never handled a stream like this (or any stream). Maybe the NAudio library isn't the way to go?
Code would be appreciated, but even pointers to sources where I could read up on playing such stream would be very helpful as my google-fu has failed me.
Thank you.
EDIT:
private void PlayMedia() {
System.Uri manifestUri = new Uri("http://amssamples.streaming.mediaservices.windows.net/49b57c87-f5f3-48b3-ba22-c55cfdffa9cb/Sintel.ism/manifest(format=m3u8-aapl)");
var mediaPlayer = new Windows.Media.Playback.MediaPlayer();
~~~~~~~~~~~~ -> "'Media Player' does not contain a constructor that takes 0 arguments."
mediaPlayer.Source = MediaSource.CreateFromUri(manifestUri);
mediaPlayer.Play();
}
but I can't get the MediaPlayer class to work. It says for example the x.Play() doesn't exist.
You have not posted your code segment. So I could not locate the problem of Visual Studio alerting "doesn't exist" accurately. If you want to use "MediaPlayer" class please add Windows.Media.Core and Windows.Media.Playback namespace at first. And you could reference the following code implementing a basic mediaplayer.
using Windows.Media.Core;
using Windows.Media.Playback;
......
private void PlayMedia()
{
System.Uri manifestUri = new Uri("http://amssamples.streaming.mediaservices.windows.net/49b57c87-f5f3-48b3-ba22-c55cfdffa9cb/Sintel.ism/manifest(format=m3u8-aapl)");
var mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromUri(manifestUri);
mediaPlayer.Play();
}
The error message of Media Player does not contain a constructor that takes 0 arguments is means that there is no constructor with no arguments in the MediaPlayer class. Please try use the full name of constructor with namespace.
var mediaPlayer = new Windows.Media.Playback.MediaPlayer();
I have a method that gets called when a button is pressed,
private List<Page> _pages = new List<Page>();
public void LoadKern(int requestedKern)
{
TextAsset pages = Resources.Load("kern" + requestedKern) as TextAsset;
JSONArray jsonPages = JSON.Parse(pages.text)["pages"].AsArray;
foreach (JSONNode page in jsonPages)
{
_pages.Add(new Page(page["image"], page["text"]));
}
ImageSpriteRenderer.sprite = Resources.Load<Sprite>(_pages[currentPage].image);
TextSpriteRenderer.sprite = Resources.Load<Sprite>(_pages[currentPage].text);
}
The code works perfect when running it in the simulator but whenever I deploy it to an android device or use the Untiy Remote 4 it no longer updates the sprites.
Whenever I remove this line and set the resource manually, it does update when the button is pressed.
_pages.Add(new Page(page["image"], page["text"]));
It seems very odd that it does work on a desktop but not on Android, is there something I am missing?
I think it difficult to put it in the comment, so I write it as an answer here.
The simplest approach to verify this a problem due to the Resource.Load<>, is to add below code:
TextAsset pages = Resources.Load("kern" + requestedKern) as TextAsset;
Debug.Load(pages + "are Loading"); // to see if it is really loaded successfully
Connect your device and open the adb log, filter the message with "are loading".
If you see pages are null, then it is clear that Resource.Load<> is the culprit.
If it is Resource.Load<> problem, you can consider using StreamingAssets:
Any files placed in a folder called StreamingAssets in a Unity project will be copied verbatim to a particular folder on the target machine. You can retrieve the folder using the Application.streamingAssetsPath property. It’s always best to use Application.streamingAssetsPath to get the location of the StreamingAssets folder, it will always point to the correct location on the platform where the application is running.
On Android, you should use:
path = "jar:file://" + Application.dataPath + "!/assets/";
I suspect that your loading the text assets is carried out at runtime, when you package the app, the text assets are not read, and it might be excluded from the project, as Unity considers this "not used". When you run the app on Android, it is natural that it fails.
Using StreamingAssets approach, you force Unity to copy the text assets "verbatim" which assures it is accessible at runtime!