Xamarin-Plugins/SimpleAudioPlayer - Error in Code

Riffy 276 Reputation points
2022-01-05T16:54:10.357+00:00

Hi

I want to use a mediaplayer in my Xamarin/C# to mainly play Audio files (mp3/wav).

I have installed Xamarin-Plugins/SimpleAudioPlayer into my first Android project.

The following is the code for playing an audio file:

 ISimpleAudioPlayer AudioPlayer;

        private void PlayAudio(object sender, EventArgs e)
        {

            AudioPlayer.Load("Test.mp3");
            AudioPlayer.Play;
        }

AudioPlayer.Play; is throwing an Compiler Error CS0201 Only assignment, call, increment, decrement, and new object expressions can be used as a statement

PlayAudio is called by a Clicked action from a toolbar item in my XAML file

Also Test3.mp3 is added to my resources file and Build Action is Embedded Source. Is AudioPlayer.Load("Test.mp3"); declared correctly?

Can anyone suggest what I am doing wrong and how I can overcome issue.

Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,300 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,364 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,311 Reputation points Microsoft Vendor
    2022-01-06T02:49:23.1+00:00

    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Castorix31 82,031 Reputation points
    2022-01-05T18:07:05.537+00:00

    Where do you create an instance of AudioPlayer ?

    (with CreateSimpleAudioPlayer, from samples like Play Audio in Xamarin Forms using Xam.Plugin.SimpleAudioPlayer