c#调用api播放MP3的问题。。。高手求救 20
foreach很快,所以只能听到最后一条的声音。我要想全部播怎么办?难道要让线程休眠一会?但是也不行啊,用户体验太差。也不知道要休眠多少时间,因为每个MP3的长度不一样。...
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);
}
}
}
一楼真可爱。。。。。。,我有思路了。 展开
我要想全部播怎么办?难道要让线程休眠一会?但是也不行啊,用户体验太差。也不知道要休眠多少时间,因为每个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);
}
}
}
一楼真可爱。。。。。。,我有思路了。 展开
展开全部
乱七八糟的,我用MP4都不会像你一样乱,你弄的好乱,看了头都晕了!你自己看一下说明书不就OK了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1楼是电脑盲,肯定没听说过C#.NET
鉴定完毕
鉴定完毕
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
同意二楼!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询