0

我正在尝试使用 MonoDroid 播放 WAV 文件,但我对在这里找到的以下代码感到困惑:http: //developer.android.com/guide/topics/media/mediaplayer.html

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();

我的项目中似乎没有 Resources/Raw 文件夹,而且我不知道如何创建一个。

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

4

1 回答 1

0

试试下面的代码。

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 ();
        }

    }

}

资源

于 2013-05-31T11:07:08.807 回答