 |
|
C# อ่านค่า string จาก List เพื่อนำมาเล่นกับ mp3 ไม่ได้ครับ |
|
 |
|
|
 |
 |
|
ผมแก้ไขฟังก์ชั่นแปลงเลขเป็นตัวหนังสือจาก โค้ดตัวอย่างนี้ https://www.thaicreate.com/community/c-asp-net-convert-money-number-to-thai-text.html เพื่อนำมาอ่านเสียงตัวเลขจาก mp3 ครับ ซึ่งผมสามารถเล่นเพลงจาก mp3 ได้แต่ไม่สามารถเล่นไฟล์เสียงจากชื่อใน List ได้
โค้ดแปลงเลขเป็นตัวหนังสือแก้แล้วเป็นแบบนี้ครับ
Code
public List<String> NumberToWordTH(string txt)
{
List<string> ttsTH = new List<string>();
string queueTxt, n, queueTH = "";
double amount;
try { amount = Convert.ToDouble(txt); }
catch { amount = 0; }
queueTxt = amount.ToString("####.00");
//string[] num = { "ศูนย์", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า", "สิบ" };
string[] num = { "blank", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" };
//string[] rank = { "", "สิบ", "ร้อย", "พัน", "หมื่น", "แสน", "ล้าน" };
string[] rank = { "", "ten", "hundred", "thousand", "thousand" };
string[] temp = queueTxt.Split('.');
string intVal = temp[0];
if (Convert.ToDouble(queueTxt) == 0)
{
// queueTH = "ศูนย์";
ttsTH.Add("blank");
}
else
{
for (int i = 0; i < intVal.Length; i++)
{
n = intVal.Substring(i, 1);
if (n != "0")
{
if ((i == (intVal.Length - 1)) && (n == "1"))
{
//queueTH += "เอ็ด";
ttsTH.Add("1sp");
}
else if ((i == (intVal.Length - 2)) && (n == "2"))
{
//queueTH += "ยี่";
ttsTH.Add("2sp");
}
else if ((i == (intVal.Length - 2)) && (n == "1"))
queueTH += "";
else
//queueTH += num[Convert.ToInt32(n)];
ttsTH.Add(num[Convert.ToInt32(n)]);
//queueTH += rank[(intVal.Length - i) - 1];
ttsTH.Add(rank[(intVal.Length - i) - 1]);
}
}
}
return ttsTH;
}
ส่วนโค้ดปุ่มกดในการเรียกอ่านไฟล์เสียงแบบนี้ครับ
Code
private void button1_Click(object sender, EventArgs e)
{
List<String> voiceTH = new List<string>();
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
voiceTH = NumberToWordTH("551");
foreach (string i in voiceTH)
{
wplayer.URL = "voicesTH\\" + i + ".mp3";
wplayer.controls.play();
Console.WriteLine(i);
}
}
ผลการ Run ตรง output แสดงผลได้แบบนี้ครับ
five
thousand
five
ten
1sp
ซึ่งเอาต์พุตจะเล่นเฉพาะไฟล์เสียงจาก List บรรทัดสุดท้ายได้ แต่ที่ผมต้องการคือเล่นไฟล์เสียงหมดทุกบรรทัดครับตามที่วนลูป ไม่ทราบว่าจะต้องแก้ไขยังไงครับ
Tag : .NET, C#
|
ประวัติการแก้ไข 2014-12-18 12:21:12 2014-12-18 17:35:01
|
 |
 |
 |
 |
Date :
2014-12-18 11:55:46 |
By :
mmc01 |
View :
2035 |
Reply :
8 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ลองเช็คดูละครับว่ามีไฟล์ไม๊
Code (C#)
Console.WriteLine(File.Exists("voicesTH\\" + i + ".mp3") ? "voicesTH\\" + i + ".mp3 File exists." : "voicesTH\\" + i + ".mp3" File does not exist.");
|
 |
 |
 |
 |
Date :
2014-12-18 23:01:14 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เช็คแล้วมีครบครับ ถ้าเล่นทีละไฟล์โดยไม่ใช้ for จะเล่นได้หมด แต่ถ้าวนลูปด้วย for ให้เล่นทุกไฟล์มันจะเล่นเฉพาะไฟล์สุดท้ายครับ
|
 |
 |
 |
 |
Date :
2014-12-19 01:06:28 |
By :
mmc01 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
งั้นก็แสดงว่ามันเล่นทุกไฟล์แหละคับ
แต่ไม่ได้เล่นในแบบ list มันก็เลยเล่นไฟล์ล่าสุด
วิธีแบบบ้านๆของผมก็คง
ต้องหาทางให้มันเล่นแบบ list โดยอาจจะใช้เวลาของแต่ละไฟล์เป็นตัวกำหนด
ประมาณว่าหน่วงเวลาตาม เวลาเล่นของแต่ละไฟล์(ว่าไปนั่น)
|
 |
 |
 |
 |
Date :
2014-12-19 08:50:21 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวนี้ก็น่าสนครับ
สร้างเป็น playlist เหมือน winamp
Code (C#)
/*
// Windows Media Player 10 List Format
<?wpl version="1.0"?>
<smil>
<head>
<meta name="Generator" content="Microsoft Windows Media Player -- 10.0.0.4036"/>
<author/>
<title>telugu_all</title>
</head>
<body>
<seq>
<media src="Filename1"/>
...........
</seq>
</body>
</smil>
*/
namespace CreateWMList
{
public partial class frmCreateWMList : Form
{
#region Global Variables
public const string Separator = ",";
public static ArrayList searchAudio = new ArrayList();
public static ArrayList searchVideo = new ArrayList();
//public const int MAX_AUDIOTYPES = 20;
//public const int MAX_VIDEOTYPES = 24;
//string[] audioSearch = new string[MAX_AUDIOTYPES] { ".wma", ".wax", ".cda", ".wav", ".mp3", ".m3u", ".mid", ".midi", ".rmi", ".aif", ".aifc", ".aiff", ".au", ".snd", ".ra", ".rm", ".ram", ".rpm", ".rmm", ".rnx" };
//string[] videoSearch = new string[MAX_VIDEOTYPES] { ".asf", ".asx", ".wpl", ".wm", ".wmx", ".wmd", ".wmz", ".wmv", ".wvx", ".avi", ".mpeg", ".mpg", ".mpe", ".m1v", ".mp2", ".mpv2", ".mp2v", ".mpa", ".dvr-ms", ".rm", ".ram", ".rpm", ".rmm", ".rnx" };
public static StreamWriter sw;
#endregion
public frmCreateWMList()
{
InitializeComponent();
}
private void frmCreateWMList_Load(object sender, EventArgs e)
{
// Set default values
cmbType.SelectedIndex = 0;
chkIncSubDir.Checked = true;
LoadFormats();
/*
* Set Default formats
int iIndex;
for (iIndex = 0; iIndex < MAX_AUDIOTYPES; iIndex++)
searchAudio.Add(audioSearch[iIndex]);
for (iIndex = 0; iIndex < MAX_VIDEOTYPES; iIndex++)
searchVideo.Add(videoSearch[iIndex]);
txtDir.Text = "e:\\songs\\tamil\\jeans";
*/
txtFilename.Text = "PlayList1.wpl";
}
#region Load Formats from formats.ini
#region Parser
private string Parse(string line, ref int index)
{
if (index == -1)
{
return "";
}
//Bypass the very first separator and start the string at the start of the data
//if (index == 0)
//{
// index = line.IndexOf(Separator, 0);
// index += Separator.Length;
//}
//Get the next separator position
int tempNo = line.IndexOf(Separator, index);
string sTemp = string.Empty;
//Get the data in between the separators
if (tempNo == -1)
{
sTemp = line.Substring(index);
index = tempNo;
}
else
{
sTemp = line.Substring(index, tempNo - index);
//Set the index to the start of the next set of data
index = tempNo + Separator.Length;
}
// Remove Double quotes in a string.
sTemp = sTemp.Trim('"', ' ');
return sTemp.Trim();
}
#endregion
private void LoadFormats()
{
string sFormatIni = "formats.ini";
string sTemp;
int index;
FileStream fs = new FileStream(sFormatIni, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
using (fs)
{
string fileLine = string.Empty;
try
{
while ((fileLine = sr.ReadLine()) != null)
{
if (fileLine == string.Empty)
continue;
if (fileLine[0] == '#') // Process only uncommented lines
continue;
index = 0;
sTemp = Parse(fileLine, ref index);
switch (sTemp.ToUpper())
{
case "AF":
sTemp = Parse(fileLine, ref index);
while (string.IsNullOrEmpty(sTemp) == false)
{
searchAudio.Add(sTemp);
sTemp = Parse(fileLine, ref index);
}
break;
case "VF":
sTemp = Parse(fileLine, ref index);
while (string.IsNullOrEmpty(sTemp) == false)
{
searchVideo.Add(sTemp);
sTemp = Parse(fileLine, ref index);
}
break;
default:
break;
} // End switch
} // End while
} // End Try
catch (Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
}
finally
{
// Close File handles
sr.Close();
fs.Close();
}
} // End Using
}
#endregion
#region CreatePlayList
private void CreatePlayList()
{
// Open a file to write
string sFileName = txtFilename.Text;
FileStream fs = File.Create(sFileName);
sw = new StreamWriter(fs);
try
{
sw.WriteLine("<?wpl version=\"1.0\"?>"); // File Header
sw.WriteLine("<smil>"); // Start of File Tag
sw.WriteLine("\t<head>"); // Playlist File Header Information Start Tag
sw.WriteLine("\t\t<meta name=\"Generator\" content=\"Microsoft Windows Media Player -- 10.0.0.4036\"/>");
sw.WriteLine("\t\t<author>" + txtAuthor.Text + "</author>");
sw.WriteLine("\t\t<title>" + txtTitle.Text + "</title>");
sw.WriteLine("\t</head>"); // Playlist File Header Information End Tag
sw.WriteLine("\t<body>"); // Start of body Tag
sw.WriteLine("\t\t<seq>"); // Start of filelist Tag
// Get Directory's File list and Add files
DirectoryListing(txtDir.Text);
sw.WriteLine("\t\t</seq>"); // End of filelist Tag
sw.WriteLine("\t</body>"); // End of body Tag
sw.WriteLine("</smil>"); // End of File Tag
sFileName = sFileName + " Successfully created.";
MessageBox.Show(sFileName, "Create Playlis");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Create Playlist: Error");
sFileName = sFileName + " Unsuccessful.";
MessageBox.Show(sFileName, "Create Playlis");
}
finally
{
sw.Close();
fs.Close();
}
}
#endregion
#region Get File List
private int DirectoryListing(string sPath)
{
int iFileCount = 0;
if (string.IsNullOrEmpty(sPath) == true)
{
MessageBox.Show("Directory not specified. Please select Valid directory.");
return iFileCount;
}
// if (Directory.Exists(sPath) == false)
// {
// MessageBox.Show("Directory not exist. Please select Valid directory.");
// return iFileCount;
// }
ArrayList searchList = new ArrayList();
switch (cmbType.SelectedIndex)
{
case 0: // Audio files
searchList = searchAudio;
break;
case 1: // Video files
searchList = searchVideo;
break;
case 2: // All Audio Video Files
searchList = searchAudio;
searchList.AddRange(searchVideo);
break;
case 3: // All files
//searchList = null;
break;
default:
//searchList = null;
break;
}
if(File.Exists(sPath))
{
// This path is a file
iFileCount = ProcessFile(sPath, searchList);
}
else if(Directory.Exists(sPath))
{
// This path is a directory
iFileCount = ProcessDirectory(sPath, searchList);
}
else
{
MessageBox.Show(sPath + " is not a valid file or directory.");
}
return iFileCount;
}
// Insert logic for processing found files here.
public static int ProcessFile(string fileName, ArrayList searchList)
{
string fileLine;
string sFileExt;
int iFileCount = 0;
if (string.IsNullOrEmpty(fileName) == true)
return iFileCount;
if (searchList.Count != 0) // If it's not All files
{
sFileExt = fileName.Substring(fileName.IndexOf('.'));
if (searchList.IndexOf(sFileExt) == -1)
return iFileCount;
}
fileLine = "\t\t\t<media src=\"";
fileLine = fileLine + fileName + "\"/>";
sw.WriteLine(fileLine);
return ( ++iFileCount);
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public static int ProcessDirectory(string targetDirectory, ArrayList searchList)
{
int iFileCount = 0;
if (string.IsNullOrEmpty(targetDirectory) == true)
return iFileCount;
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(targetDirectory);
if (fileEntries.Length > 0)
{
foreach (string fileInfo in fileEntries)
iFileCount += ProcessFile(fileInfo, searchList);
}
// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
if (subdirectoryEntries.Length > 0)
{
foreach (string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory, searchList);
}
return iFileCount;
}
#endregion
#region Open Directory
private void btnOpenDir_Click(object sender, EventArgs e)
{
FolderBrowserDialog FolderBrowserDialog1 = new FolderBrowserDialog();
FolderBrowserDialog1.ShowNewFolderButton = false; // Hide 'Make New Folder' button
if (string.IsNullOrEmpty(txtDir.Text) == false) // If something entered dir field
{
FolderBrowserDialog1.SelectedPath = txtDir.Text;
}
FolderBrowserDialog1.Description = "Select Folder to Create Playlist for...";
// Show the FolderBrowserDialog.
if (FolderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtDir.Text = FolderBrowserDialog1.SelectedPath;
}
FolderBrowserDialog1.Dispose();
}
#endregion
#region Get Filename
private void btnSaveAs_Click(object sender, EventArgs e)
{
SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
SaveFileDialog1.Title = "Save Windows Playlist As...";
SaveFileDialog1.Filter = "wpl files (*.wpl)|*.wpl|All files (*.*)|*.*";
SaveFileDialog1.FilterIndex = 1;
SaveFileDialog1.RestoreDirectory = true;
SaveFileDialog1.DefaultExt = "wpl";
if (string.IsNullOrEmpty(txtFilename.Text) == false)
SaveFileDialog1.FileName = txtFilename.Text;
else
SaveFileDialog1.FileName = "Playlist1.wpl";
//SaveFileDialog1.InitialDirectory = "C:\\Documents and Settings\\All Users\\My Documents\\My Music\\My Playlists";
//SaveFileDialog1.FileName = "Playlist1.wpl";
SaveFileDialog1.CreatePrompt = false; // Don't prompt the user for permission to Create a file
// if the user specifies a file that does not exist
SaveFileDialog1.OverwritePrompt = true; // prompt the user for permission to Overwtire a file
if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(SaveFileDialog1.FileName) == false)
{
txtFilename.Text = SaveFileDialog1.FileName;
}
}
SaveFileDialog1.Dispose();
}
#endregion
#region Button Controls
private void btnCreate_Click(object sender, EventArgs e)
{
// Get Directory Name
if (Directory.Exists(txtDir.Text) == false) // if directory doesn't exist
{
MessageBox.Show(txtDir.Text + " does not exist.");
return;
}
// Get Filename
string sFilename = txtFilename.Text;
if (string.IsNullOrEmpty(sFilename) == true)
{
MessageBox.Show("Filename field can not be empty.");
return;
}
if (sFilename.IndexOf('.') == -1) // If no extension specified
txtFilename.Text = sFilename + ".wpl"; // Add default player list extension
// Get Author Name
if (string.IsNullOrEmpty(txtAuthor.Text) == true)
{
txtAuthor.Text = "Suneel Piduguralla";
}
// Get Title Name
if (string.IsNullOrEmpty(txtTitle.Text) == true)
{
txtTitle.Text = "My Playlist";
}
// Create List
CreatePlayList();
}
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
#endregion
#region About form
private void btnAbout_Click(object sender, EventArgs e)
{
Form frmAbout1 = new frmAbout();
frmAbout1.ShowDialog();
}
#endregion
}
}
|
 |
 |
 |
 |
Date :
2014-12-19 09:03:30 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับ สร้างเป็น playlist ขอบคุณมากครับ
|
 |
 |
 |
 |
Date :
2014-12-19 18:04:17 |
By :
mmc01 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|