基本信息
源码名称:C# MediaPlayer 播放声音示例源码
源码大小:0.73M
文件格式:.zip
开发语言:C#
更新时间:2018-10-18
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
打开game2.exe输入 xxx 即可测试,按键 zs 退出程序
打开game2.exe输入 xxx 即可测试,按键 zs 退出程序
退出时候输入 zs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using System.Configuration;
using Microsoft.Win32;
using System.Reflection;
namespace Game2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
//String m_strRunPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
public string m_strRunPath = AppDomain.CurrentDomain.BaseDirectory "files\\";
public SerialPort serialPort;//串口对象类
private MediaPlayer m_Media = new MediaPlayer();
private System.Windows.Threading.DispatcherTimer m_Timer;
private bool m_bSerival = false;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string sCOM = System.Configuration.ConfigurationSettings.AppSettings["串口号"];
string bRun = System.Configuration.ConfigurationSettings.AppSettings["AutoRun"];
if (bRun == "0")
{
RegRun("Game2", false);
}
else if(bRun == "1")
{
RegRun("Game2");
}
btnImage.ImageSource = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory "btn.png", UriKind.Relative));
SetFullScreen();
SetBackgroundImage( AppDomain.CurrentDomain.BaseDirectory "back.jpg");
if (!InitCOM(sCOM))
{
MessageBox.Show("串口异常,当前配置串口" sCOM "请检查配置和设备连接。", "警告");
return;
}
else
{
m_bSerival = true;
m_Timer = new System.Windows.Threading.DispatcherTimer();
m_Timer.Interval = TimeSpan.FromSeconds(1);
m_Timer.Tick = dtimer_Tick;
//m_Timer.Start();
}
TextBox.Focus();
}
void dtimer_Tick(object sender, EventArgs e)
{
SendCommand("0xaa");
}
private void SetFullScreen()
{
this.Left = 0.0;
this.Top = 0.0;
this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
this.WindowStyle = System.Windows.WindowStyle.None;
this.ResizeMode = System.Windows.ResizeMode.NoResize;
//this.Topmost = true;
}
private void SetBackgroundImage(string path)
{
try
{
ImageBrush b = new ImageBrush();
b.ImageSource = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
this.Background = b;
}
catch { };
}
private void BtnSelect_Click(object sender, RoutedEventArgs e)
{
string strText = TextBox.Text;
if (strText == "")
{
return;
}
string[] strFiles = System.IO.Directory.GetFileSystemEntries(m_strRunPath);
if (strFiles.Length > 0)
{
for (int i = 0; i < strFiles.Length; i )
{
string strTemp = strFiles[i];
string str1 = strTemp.Substring(0, strTemp.Length - 4);
string str2 = str1.Remove(0, m_strRunPath.Length);
if (strText == str2)
{
SendCommand("0xaa");
if (System.IO.File.Exists(strTemp))
{
//转换成小写一并处理
string strFileStyle = strTemp.Substring(strTemp.Length - 3).ToString().ToLower();
//如果mp3文件则直接播放音频不弹出相应窗口
if (strFileStyle == "mp3")
{
m_Media.Open(new Uri(strTemp, UriKind.Relative));
m_Media.Play();
return;
}
}
WindowPop pop = new WindowPop();
pop.m_FilePath = strTemp;
pop.ShowDialog();
return;
}
}
}
else
{
MessageBox.Show("files中没有找到任何文件。","警告");
return;
}
m_Media.Open(new Uri(AppDomain.CurrentDomain.BaseDirectory "xxx.mp3", UriKind.Relative));
m_Media.Play();
MessageBox.Show("非法输入!", "警告");
m_Media.Stop();
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
WindowInput WinInput = new WindowInput();
WinInput.ShowDialog();
}
}
/// 串口接收通信配置方法
/// <param name="PortName">端口名称</param>
public bool InitCOM(string PortName)
{
serialPort = new SerialPort(PortName, 9600, Parity.None, 8, StopBits.One);
serialPort.DataReceived = new SerialDataReceivedEventHandler(serialPort_DataReceived);//DataReceived事件委托
serialPort.ReceivedBytesThreshold = 1;
serialPort.RtsEnable = true;
return OpenPort();//串口打开
}
/// 数据接收事件
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] readBuffer = new byte[serialPort.ReadBufferSize];
serialPort.Read(readBuffer, 0, readBuffer.Length);
string str = System.Text.Encoding.Default.GetString(readBuffer);
MessageBox.Show(str);
}
//打开串口的方法
public bool OpenPort()
{
try//这里写成异常处理的形式以免串口打不开程序崩溃
{
serialPort.Open();
}
catch { }
if (serialPort.IsOpen)
{
return true;
}
else
{
return false;
}
}
//向串口发送数据
public void SendCommand(string CommandString)
{
try
{
byte[] WriteBuffer1 = Encoding.ASCII.GetBytes(CommandString);
byte[] WriteBuffer = new byte[] { 0xaa };
serialPort.Write(WriteBuffer, 0, WriteBuffer.Length);
}
catch { }
}
private bool AltDown = false;
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt)
{
AltDown = true;
}
else if (e.SystemKey == Key.F4 && AltDown)
{
e.Handled = true;
}
if (e.Key == Key.R && (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Shift)) == (ModifierKeys.Control | ModifierKeys.Shift))
{
//MessageBox.Show("CTRL SHIFT TAB");
if (m_bSerival)
{
SendCommand("0xaa");
}
else
{
MessageBox.Show("复位失败,请检查串口连接和通讯。");
}
}
}
private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
{
if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt)
{
AltDown = false;
}
}
void RegRun(string appName, bool f = true)
{
RegistryKey HKCU = Registry.CurrentUser;
RegistryKey Run = HKCU.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
bool b = false;
foreach (string i in Run.GetValueNames())
{
if (i == appName)
{
b = true;
break;
}
}
try
{
if (f)
{
Run.SetValue(appName, Assembly.GetExecutingAssembly().Location);
}
else
{
Run.DeleteValue(appName);
}
}
catch
{ }
HKCU.Close();
}
}
}