Search code examples
androidaudioxamarin.forms

the system cannot find the file specified error when i add sound file to xamarin forms project


enter image description here

I add this bip.wav file to Assets folder and i got this the system cannot find the file specified error.

enter image description here

I delete the sound file then clean solution and rebuild solution. After all that processes my app works. So, i can not add a sound file to project.

This is my code to play sound.

var player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current; player.Load("bip.wav"); player.Play();

Solution

  • First of all, if you added the file in to the Asset folder of the Android part, you need to set the Build Action for this file to AndroidAsset. And then use the AssetManager to read the file. And AssetManager can be only used the android project. So you can use the Xamarin.Forms DependencyService to call this api for the android.

    For more details, you can check the official document about Using Android Assets.

    In addition, you can also put the file in the share project setting Build Action to Embedded Resource. For more details, you can refer to the accpeted answer in this case about Play .wav audio in Xamarin.