我试图使用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的音频文件。
答案 0 :(得分: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 ();
}
}
}