C# 使用MediaPlayer播放WAV文件

C# 使用MediaPlayer播放WAV文件,c#,audio,xamarin.android,C#,Audio,Xamarin.android,我正在尝试使用MonoDroid播放WAV文件,但我对这里找到的以下代码感到困惑: 似乎我的项目中没有资源/原始文件夹,我不知道如何创建一个 所以基本上我想知道如何使用MediaPlayer播放MonoDroid的音频文件。试试下面的代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content;

我正在尝试使用MonoDroid播放WAV文件,但我对这里找到的以下代码感到困惑:

似乎我的项目中没有资源/原始文件夹,我不知道如何创建一个

所以基本上我想知道如何使用MediaPlayer播放MonoDroid的音频文件。

试试下面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Media;

namespace Example_WorkingWithAudio
{
    //
    // Shows how to use the MediaPlayer class to play audio.
    class PlayAudio : INotificationReceiver
    {
        MediaPlayer player = null;
        static string filePath = "/data/data/Example_WorkingWithAudio.Example_WorkingWithAudio/files/testAudio.mp4";

        public void strtPlayer ()
        {
            try {
                if (player == null) {
                    player = new MediaPlayer ();
                } else {
                    player.Reset ();
                }

                // This method works better than setting the file path in SetDataSource. Don't know why.
                Java.IO.File file = new Java.IO.File (filePath);
                Java.IO.FileInputStream fis = new Java.IO.FileInputStream (file);
                player.SetDataSource (fis.FD);

                //player.SetDataSource(filePath);
                player.Prepare ();
                player.Start ();
            } catch (Exception ex) {
                Console.Out.WriteLine (ex.StackTrace);
            }
        }

        public void StopPlayer ()
        {
            if ((player != null)) {
                if (player.IsPlaying) {
                    player.Stop ();
                }
                player.Release ();
                player = null;
            }
        }

        public void Start ()
        {
            strtPlayer ();
        }

        public void Stop ()
        {
            this.StopPlayer ();
        }

    }

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Media;

namespace Example_WorkingWithAudio
{
    //
    // Shows how to use the MediaPlayer class to play audio.
    class PlayAudio : INotificationReceiver
    {
        MediaPlayer player = null;
        static string filePath = "/data/data/Example_WorkingWithAudio.Example_WorkingWithAudio/files/testAudio.mp4";

        public void strtPlayer ()
        {
            try {
                if (player == null) {
                    player = new MediaPlayer ();
                } else {
                    player.Reset ();
                }

                // This method works better than setting the file path in SetDataSource. Don't know why.
                Java.IO.File file = new Java.IO.File (filePath);
                Java.IO.FileInputStream fis = new Java.IO.FileInputStream (file);
                player.SetDataSource (fis.FD);

                //player.SetDataSource(filePath);
                player.Prepare ();
                player.Start ();
            } catch (Exception ex) {
                Console.Out.WriteLine (ex.StackTrace);
            }
        }

        public void StopPlayer ()
        {
            if ((player != null)) {
                if (player.IsPlaying) {
                    player.Stop ();
                }
                player.Release ();
                player = null;
            }
        }

        public void Start ()
        {
            strtPlayer ();
        }

        public void Stop ()
        {
            this.StopPlayer ();
        }

    }

}