I want to do a speaker that directly alarm out from mini-computer. I want the function to be like “if there is obstacle in front of the robot, the robot will sound out an alarm to tell the user that there is an obstacle in front”. The voice will be like sound out that " There is an obstacle in front". The talking voice part i will do it myself.
i am doing a robot that helps visually impaired people for their daily life. I using a kinect to detect people/car in front of the user .So when the kinect detects that if there is obstacle in front of the user, the robot will say " there is people/car in front!! Please keep out". I wan to program the speaker system using C# programming and to try it out using computer for the sound system.
In general, simple playing of sounds or audio files in C# is rather straight forward if on a Windows platform (or any other platform that implements System.Media).
There is quite a lot of information online which explains how to do this and we quickly found the following as examples (seems to be some code provided too):
[code]using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Media;
namespace idogmotorbluetooth
{
public partial class Form1 : Form
{
byte] Buffer = new byte[3];
byte Header = 0x0F;
byte LeftMotorSpeed = 0x0F;
byte RightMotorSpeed = 0x0F;
SerialPort port;
String scom;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Buffer[0] = Header;
Buffer[1] = LeftMotorSpeed;
Buffer[2] = RightMotorSpeed;
}
public List<string> GetAllPorts()
{
List<String> allPorts = new List<String>();
foreach (String portName in System.IO.Ports.SerialPort.GetPortNames())
{
allPorts.Add(portName);
}
return allPorts;
}
void adddevice()
{
try
{ //add device n os
Process p = Process.Start("C:\\Windows\\System32\\DevicePairingWizard.exe");//here write ur own windows drive
while (true)
{
if (p.HasExited) //determine if process end
break;
}
//listBox1.DataSource = GetAllPorts();
richTextBox1.Text = richTextBox1.Text + Environment.NewLine + "COMPORT GENERATED";
}
catch (Exception ee)
{
if (DialogResult.Retry == MessageBox.Show("CANT FIND UR ADDED DEVICE..", "Problem occured", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error))
adddevice();
MessageBox.Show(ee.ToString());
}
}
void Removedevice()
{
try
{ //add device n os
Process p = Process.Start("C:\\Windows\\System32\\control.exe");//here write ur own windows drive
while (true)
{
if (p.HasExited) //determine if process end
break;
}
//listBox1.DataSource = GetAllPorts();
richTextBox1.Text = richTextBox1.Text + Environment.NewLine + "COMPORT REMOVED";
}
catch (Exception ee)
{
if (DialogResult.Retry == MessageBox.Show("..", "Problem occured", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error))
adddevice();
MessageBox.Show(ee.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
adddevice();
}
private void button2_Click(object sender, EventArgs e)
{
Removedevice();
}
private void connectbluetooth()
{
try
{
if (listBox1.SelectedIndex != -1)
{
scom = listBox1.SelectedItem.ToString();
port = new SerialPort(scom, 9600, Parity.None, 8, StopBits.One);
port.Open();
richTextBox1.Text = richTextBox1.Text + Environment.NewLine + "CONNECTION OPEN SUCCESSFUL"; ;
//return true;
}
else
MessageBox.Show("Please select com port", "Missing port", MessageBoxButtons.OK, MessageBoxIcon.Information);
// return false;
}
catch (Exception a)
{
if (DialogResult.Retry == MessageBox.Show(a.Message, "problem occured", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning))
connectbluetooth();
MessageBox.Show(a.ToString());
}
}
bool send()
{
try
{
Buffer[0] = Header;
Buffer[1] = LeftMotorSpeed;
Buffer[2] = RightMotorSpeed;
port.WriteTimeout = 10000;//define how much time wait for send data
port.Write(Buffer, 0, Buffer.Length);
return true;
}
catch (Exception a)
{
MessageBox.Show(a.ToString());
return false;
}
}
private void button3_Click(object sender, EventArgs e)
{
connectbluetooth();
}
private void button4_Click(object sender, EventArgs e)
{
listBox1.DataSource = GetAllPorts();
}
private void button5_Click(object sender, EventArgs e)
{
Header = 0x0F;
LeftMotorSpeed = 0xFF;
RightMotorSpeed = 0xFF;
send();
}
private void button6_Click(object sender, EventArgs e)
{
Header = 0x0F;
LeftMotorSpeed = 0xFF;
RightMotorSpeed = 0x00;
send();
}
private void button7_Click(object sender, EventArgs e)
{
Header = 0x0F;
LeftMotorSpeed = 0x00;
RightMotorSpeed = 0xFF;
send();
}
private void button8_Click(object sender, EventArgs e)
{
Header = 0x0F;
LeftMotorSpeed = 0x00;
RightMotorSpeed = 0x00;
send();
}
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
LeftMotorSpeed = 0xFF;
RightMotorSpeed = 0x00;
break;
case Keys.Right:
LeftMotorSpeed = 0x00;
RightMotorSpeed = 0xFF;
break;
case Keys.Up:
LeftMotorSpeed = 0xFF;
RightMotorSpeed = 0xFF;
break;
default:
LeftMotorSpeed = 0x00;
RightMotorSpeed = 0xFF;
break;
}
Header = 0x0F;
send();
}
public class SoundTestForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox filepathTextbox;
private System.Windows.Forms.Button playOnceSyncButton;
private System.Windows.Forms.Button playOnceAsyncButton;
private System.Windows.Forms.Button playLoopAsyncButton;
private System.Windows.Forms.Button selectFileButton;
private System.Windows.Forms.Button stopButton;
private System.Windows.Forms.StatusBar statusBar;
private System.Windows.Forms.Button loadSyncButton;
private System.Windows.Forms.Button loadAsyncButton;
private SoundPlayer player;
public SoundTestForm()
{
// Disable playback controls until a valid .wav file
// is selected.
EnablePlaybackControls(false);
// Set up the status bar and other controls.
InitializeControls();
// Set up the SoundPlayer object.
InitializeSound();
}
// Sets up the status bar and other controls.
private void InitializeControls()
{
// Set up the status bar.
StatusBarPanel panel = new StatusBarPanel();
panel.BorderStyle = StatusBarPanelBorderStyle.Sunken;
panel.Text = "Ready.";
panel.AutoSize = StatusBarPanelAutoSize.Spring;
this.statusBar.ShowPanels = true;
this.statusBar.Panels.Add(panel);
}
// Sets up the SoundPlayer object.
private void InitializeSound()
{
// Create an instance of the SoundPlayer class.
player = new SoundPlayer();
// Listen for the LoadCompleted event.
player.LoadCompleted += new AsyncCompletedEventHandler(player_LoadCompleted);
// Listen for the SoundLocationChanged event.
player.SoundLocationChanged += new EventHandler(player_LocationChanged);
}
private void ReportStatus(string statusMessage)
{
// If the caller passed in a message...
if ((statusMessage != null) && (statusMessage != String.Empty))
{
// ...post the caller's message to the status bar.
this.statusBar.Panels[0].Text = statusMessage;
}
}
// Enables and disables play controls.
private void EnablePlaybackControls(bool enabled)
{
this.playOnceSyncButton.Enabled = enabled;
this.playOnceAsyncButton.Enabled = enabled;
this.playLoopAsyncButton.Enabled = enabled;
this.stopButton.Enabled = enabled;
}
private void filepathTextbox_TextChanged(object sender,
EventArgs e)
{
// Disable playback controls until the new .wav is loaded.
EnablePlaybackControls(false);
}
private void loadSyncButton_Click(object sender,
System.EventArgs e)
{
// Disable playback controls until the .wav is
// successfully loaded. The LoadCompleted event
// handler will enable them.
EnablePlaybackControls(false);
try
{
// Assign the selected file's path to
// the SoundPlayer object.
player.SoundLocation = filepathTextbox.Text;
// Load the .wav file.
player.Load();
}
catch (Exception ex)
{
ReportStatus(ex.Message);
}
}
private void loadAsyncButton_Click(System.Object sender,
System.EventArgs e)
{
// Disable playback controls until the .wav is
// successfully loaded. The LoadCompleted event
// handler will enable them.
EnablePlaybackControls(false);
try
{
// Assign the selected file's path to
// the SoundPlayer object.
player.SoundLocation = this.filepathTextbox.Text;
// Load the .wav file.
player.LoadAsync();
}
catch (Exception ex)
{
ReportStatus(ex.Message);
}
}
private void player_LoadCompleted(object sender,
AsyncCompletedEventArgs e)
{
string message = String.Format("LoadCompleted: {0}",
this.filepathTextbox.Text);
ReportStatus(message);
EnablePlaybackControls(true);
}
// Handler for the SoundLocationChanged event.
private void player_LocationChanged(object sender, EventArgs e)
{
string message = String.Format("SoundLocationChanged: {0}",
player.SoundLocation);
ReportStatus(message);
}
}
}
}[/code]
This are the error I can’t solve. Please do help thank you!=)
Would it be possible to reply with an attachment of the source code file instead? The [code] tags are only easily read for small portions of code and yours is too long for that.
At this time troubleshooting this level of code is well beyond the support we can provide, but we encourage you to keep troubleshooting and perhaps take a look through functional sections of the code.
We also invite the community to provide their input and ideas.