0

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.

Casey
  • 81
  • 4
  • Refer to https://stackoverflow.com/questions/18459122/play-sound-on-button-click-android – Lucas Zhang Mar 26 '19 at 02:22
  • Thank you for responding, that post has a lot of good info, but still sends me down the path of player = MediaPlayer.Create(global::Android.App.Application.Context, **Resource.Raw.someFileName**); VS will not allow this, giving the message: "'Resource.Raw' does not contain a definition for 'someFileName'". As a temporary get by, I amusing an if statement to determine which sound is needed and statically assigning the resource id to a variable, which is passed into the create statement. I DETEST static code, but it it works until I find a dynamic option or my flaw. – Casey Mar 26 '19 at 14:59

0 Answers0