Hello,
Welcome to our Microsoft Q&A platform!
You need to read the mp3 file from the assets folder. you can write a dependenceservice to read Stream. Then you can use Plugin.SimpleAudioPlayer
to play it
Firstly, create an interface in the share folder.
public interface IReadFile
{
Stream ReadAudioStream(string FileName);
}
Then achieve this interface in the android platform. Note, please copy your mp3 file to android Assets folder, build action
property is AndroidAsset
.
[assembly: Dependency(typeof(ReadFileServoce))]
namespace App4.Droid
{
class ReadFileServoce : IReadFile
{
public Stream ReadAudioStream(string FileName)
{
AssetManager assets = Android.App.Application.Context.Assets;
Stream stream = assets.Open(FileName);
return stream;
}
}
}
Open you can play it the Forms background code. Note: Please use android device to test it. I use android emulator, do not have any audio(it is my emulator's issue).
ISimpleAudioPlayer player;
public AboutPage()
{
InitializeComponent();
BindingContext = ViewModelLocator.MainViewModel;
var stream = DependencyService.Get<IReadFile>().ReadAudioStream("jay.mp3");
player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();
player.Load(stream);
}
private async void Button_Clicked(object sender, EventArgs e)
{
player.Play();
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.