[英]Using MediaPlayer to play a WAV file
我正在嘗試使用MonoDroid播放WAV文件,但是我對在這里找到的以下代碼感到困惑: http : //developer.android.com/guide/topics/media/mediaplayer.html
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();
看來我的項目中沒有“資源/原始”文件夾,也不知道如何創建該文件夾。
因此,基本上我想知道如何使用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 ();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.