积极答复者
api播放MP3的问题。高手求救啊!

问题
-
foreach很快,所以只能听到最后一条的声音。
我要想全部播怎么办?难道要让线程休眠一会?但是也不行啊,用户体验太差。也不知道要休眠多少时间,因为每个MP3的长度不一样。
请问api里有没有检查文件是否播完的方法?
#region 点击试听 播放MP3
private void ie语音_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
if (1 == e.Button.Index)
{
string mp3 = "";
DevExpress.XtraEditors.MemoExEdit mee = sender as DevExpress.XtraEditors.MemoExEdit;
foreach (string str in mee.Lines)
{
mp3 = @"E:\原始语音\test\" + str;
MP3API.Sound.Play(mp3);
}}
}
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Security.Cryptography;namespace MP3API
{
public class APIClass
{
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
string path,
string shortPath,
int shortPathLength
);[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);public static string IniReadValue(string Section, string Key, string path)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
return temp.ToString();
}
}
public class Sound
{
public static void Play(string fname)
{
string TemStr = "";
string shortName = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
shortName = shortName.PadLeft(260, Convert.ToChar(" "));
APIClass.mciSendString("close all", TemStr, TemStr.Length, 0);
if (!File.Exists(fname))
{
MessageBox.Show("无法找到语音文件[" + fname + "],请设置语音文件位置!", "无法找到语音文件", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
APIClass.GetShortPathName(fname, shortName, shortName.Length);
APIClass.mciSendString("play " + shortName, TemStr, TemStr.Length, 0);
}//关闭
public static void Stop()
{
APIClass.mciSendString("close media", "", 0, 0);
}
//暂停
public static void Pause()
{
APIClass.mciSendString("pause media", "", 0, 0);
}
}
}
答案
-
简单的说 把文件一口气放进去是不可以的 因为后面的覆盖了前面的
至少要弄一个list 文件 把所有文件保存到一个临时的文本文件 中间用回车分割 这样的文件保存为 .m3u 播放这个文本文件 似乎就可以顺序播放了
工作突然有点忙 嘿嘿- 已标记为答案 韦恩卑鄙 waywaModerator 2009年5月4日 4:33
全部回复
-
-
foreach很快,所以只能听到最后一条的声音。
我要想全部播怎么办?难道要让线程休眠一会?但是也不行啊,用户体验太差。也不知道要休眠多少时间,因为每个MP3的长度不一样。
请问api里有没有检查文件是否播完的方法?或者用个timer控件,但是没有思路。请大家帮帮忙。
#region 点击试听 播放MP3
private void ie语音_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
if (1 == e.Button.Index)
{
string mp3 = "";
DevExpress.XtraEditors.MemoExEdit mee = sender as DevExpress.XtraEditors.MemoExEdit;
foreach (string str in mee.Lines)
{
mp3 = @"E:\原始语音\test\" + str;
MP3API.Sound.Play(mp3);
}}
}
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Security.Cryptography;namespace MP3API
{
public class APIClass
{
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
string path,
string shortPath,
int shortPathLength
);[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);public static string IniReadValue(string Section, string Key, string path)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
return temp.ToString();
}
}
public class Sound
{
public static void Play(string fname)
{
string TemStr = "";
string shortName = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
shortName = shortName.PadLeft(260, Convert.ToChar(" "));
APIClass.mciSendString("close all", TemStr, TemStr.Length, 0);
if (!File.Exists(fname))
{
MessageBox.Show("无法找到语音文件[" + fname + "],请设置语音文件位置!", "无法找到语音文件", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
APIClass.GetShortPathName(fname, shortName, shortName.Length);
APIClass.mciSendString("play " + shortName, TemStr, TemStr.Length, 0);
}//关闭
public static void Stop()
{
APIClass.mciSendString("close media", "", 0, 0);
}
//暂停
public static void Pause()
{
APIClass.mciSendString("pause media", "", 0, 0);
}
}
}- 已合并 韦恩卑鄙 waywaModerator 2009年4月29日 10:01 内容完全一致
-
简单的说 把文件一口气放进去是不可以的 因为后面的覆盖了前面的
至少要弄一个list 文件 把所有文件保存到一个临时的文本文件 中间用回车分割 这样的文件保存为 .m3u 播放这个文本文件 似乎就可以顺序播放了
工作突然有点忙 嘿嘿- 已标记为答案 韦恩卑鄙 waywaModerator 2009年5月4日 4:33