I have an application that allows users to view images and hear sound files.
The images and soundclips are stores in a SQL database in image fields.
Originally, I transferred the data from an Access database using the Upsize
wizard. I have been able to retrieve the data from the SQL database using
memorystreams. To do so, I have been using offests to bypass a header that is
present: for the images, I use offest 78 and for the sounds, it's 86.
All has been well and good but now I need to load images and sound clips
directly using my app, not Access. I have tried uploading the files using no
offset and/or the offsets I have previously needed to use, all to no avail.
For instance, after I store a .wav file to the database, I receive a message
"The wave header is corrupt".
Now, I see all kinds of examples to store and retrieve blob data on the
internet. None of them deal with headers.
Can anyone clear up this mystery? I am using VS 2005 Express C# edition and
SQL Express 2005.
Code examples:
public void PlaySound()
{
if (ds.Tables["Cards"] != null)
{
if (isSound)
{
sqlConn.Open();
SqlDataAdapter sql = new SqlDataAdapter(
"SELECT Sound FROM Sounds WHERE Sounds.CardID=" +
ds.Tables["Cards"].Rows[curMgr.Position]["CardID"].ToString(),
sqlConn);
DataTable dt = new DataTable();
sql.Fill(dt);
sqlConn.Close() ;
DataRow dr = dt.Rows[0];
byte[] result = (byte[])dr["Sound"];
try
{
// Retrieve the sound file using the header offset
(86)
MemoryStream ms = new MemoryStream(re sult,
SNDHDROFFSET, result.Length - SNDHDROFFSET);
SoundPlayer sp = new SoundPlayer(ms) ;
sp.Play();
}
catch (Exception e)
{
MessageBox.Show ("Sound header error! The sound clip
is not properly formatted in the SQL database. "+ e.Message);
}
}
}
}
}
public void AddSound(string soundFilename)
{
SqlCommand cmd = new SqlCommand();
FileStream stream = new FileStream(soun dFilename, FileMode.Open,
FileAccess.Read );
byte[] snd = new byte[stream.Length];
stream.Read(snd , 0, (int)stream.Len gth);
stream.Close();
sqlConn.Open();
if (!isSound)
cmd = new SqlCommand("Ins ertSound", sqlConn);
else
cmd = new SqlCommand("Upd ateSound", sqlConn);
cmd.CommandType = CommandType.Sto redProcedure;
cmd.Parameters. Add("@CardID", SqlDbType.Int). Value =
ds.Tables["Cards"].Rows[curMgr.Position]["CardID"];
cmd.Parameters. Add("@Sound", SqlDbType.Image ).Value = snd;
cmd.ExecuteNonQ uery();
sqlConn.Close() ;
isSound = true;
}
ALTER PROCEDURE dbo.InsertSound
(
@CardID int,
@Sound image
)
AS
INSERT INTO Sounds (CardID, Sound)
VALUES (@CardID, @Sound)
RETURN 5 3042
Your assumption here that the header for a WAV sound file is either 78 or 86
bytes is incorrect. WAV headers are typically 44 bytes. In addition, you
would still need the WAV header to be intact in order to play the sound using
the WaveOut Windows API, unless you were to play it as RAW PCM Data at a
fixed sampling rate and channel(s).
--
Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
"wschlichtm an" wrote: I have an application that allows users to view images and hear sound files. The images and soundclips are stores in a SQL database in image fields. Originally, I transferred the data from an Access database using the Upsize wizard. I have been able to retrieve the data from the SQL database using memorystreams. To do so, I have been using offests to bypass a header that is present: for the images, I use offest 78 and for the sounds, it's 86.
All has been well and good but now I need to load images and sound clips directly using my app, not Access. I have tried uploading the files using no offset and/or the offsets I have previously needed to use, all to no avail. For instance, after I store a .wav file to the database, I receive a message "The wave header is corrupt".
Now, I see all kinds of examples to store and retrieve blob data on the internet. None of them deal with headers.
Can anyone clear up this mystery? I am using VS 2005 Express C# edition and SQL Express 2005.
Code examples:
public void PlaySound() { if (ds.Tables["Cards"] != null) { if (isSound) { sqlConn.Open(); SqlDataAdapter sql = new SqlDataAdapter( "SELECT Sound FROM Sounds WHERE Sounds.CardID=" +
ds.Tables["Cards"].Rows[curMgr.Position]["CardID"].ToString(), sqlConn); DataTable dt = new DataTable(); sql.Fill(dt); sqlConn.Close() ; DataRow dr = dt.Rows[0];
byte[] result = (byte[])dr["Sound"];
try { // Retrieve the sound file using the header offset (86) MemoryStream ms = new MemoryStream(re sult, SNDHDROFFSET, result.Length - SNDHDROFFSET); SoundPlayer sp = new SoundPlayer(ms) ; sp.Play(); } catch (Exception e) { MessageBox.Show ("Sound header error! The sound clip is not properly formatted in the SQL database. "+ e.Message); } } } } }
public void AddSound(string soundFilename) { SqlCommand cmd = new SqlCommand(); FileStream stream = new FileStream(soun dFilename, FileMode.Open, FileAccess.Read ); byte[] snd = new byte[stream.Length]; stream.Read(snd , 0, (int)stream.Len gth); stream.Close();
sqlConn.Open(); if (!isSound) cmd = new SqlCommand("Ins ertSound", sqlConn); else cmd = new SqlCommand("Upd ateSound", sqlConn); cmd.CommandType = CommandType.Sto redProcedure;
cmd.Parameters. Add("@CardID", SqlDbType.Int). Value = ds.Tables["Cards"].Rows[curMgr.Position]["CardID"]; cmd.Parameters. Add("@Sound", SqlDbType.Image ).Value = snd; cmd.ExecuteNonQ uery(); sqlConn.Close() ; isSound = true; }
ALTER PROCEDURE dbo.InsertSound ( @CardID int, @Sound image ) AS INSERT INTO Sounds (CardID, Sound) VALUES (@CardID, @Sound) RETURN
Actually, the header information I have (86 bytes) is correct. I am using,
and have been using, the System.Media.So undPlayer class to play the file from
a database image field. It works perfectly. Attempting to use any other
offest other than 86 throws an exception with an invalid header.
I am not using the WaveOut API as there is no longer a need to do so since
the latest .NET class library includes replacement functionality.
"Peter Bromberg [C# MVP]" wrote: Your assumption here that the header for a WAV sound file is either 78 or 86 bytes is incorrect. WAV headers are typically 44 bytes. In addition, you would still need the WAV header to be intact in order to play the sound using the WaveOut Windows API, unless you were to play it as RAW PCM Data at a fixed sampling rate and channel(s). -- Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com
Ok. In that case what I would do is retrieve on of these sound files from
your database image field, and save it on the filesystem with a .Wav
extension. Then attempt to play it using your application.
If it doesn't play, you have more information about where and what is going
wrong.
Cheers,
Peter
--
Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
"wschlichtm an" wrote: Actually, the header information I have (86 bytes) is correct. I am using, and have been using, the System.Media.So undPlayer class to play the file from a database image field. It works perfectly. Attempting to use any other offest other than 86 throws an exception with an invalid header.
I am not using the WaveOut API as there is no longer a need to do so since the latest .NET class library includes replacement functionality.
"Peter Bromberg [C# MVP]" wrote:
Your assumption here that the header for a WAV sound file is either 78 or 86 bytes is incorrect. WAV headers are typically 44 bytes. In addition, you would still need the WAV header to be intact in order to play the sound using the WaveOut Windows API, unless you were to play it as RAW PCM Data at a fixed sampling rate and channel(s). -- Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com
Yep, it works if I retrieve the soundclips that I upsized from Access. Here's
the code:
sqlConn.Open();
SqlDataAdapter sql = new SqlDataAdapter(
"SELECT Sound FROM Sounds WHERE Sounds.CardID=" +
ds.Tables["Cards"].Rows[curMgr.Position]["CardID"].ToString(),
sqlConn);
DataTable dt = new DataTable();
sql.Fill(dt);
sqlConn.Close() ;
DataRow dr = dt.Rows[0];
byte[] result = (byte[])dr["Sound"];
try
{
// Retrieve the sound file using the header offset
(86)
using (MemoryStream ms = new MemoryStream(re sult,
SNDHDROFFSET, result.Length - SNDHDROFFSET))
{
// Test that sound file offest is correct
using (FileStream fs = new
FileStream("c:\ \test.wav", FileMode.Create ))
{
fs.Write(result , SNDHDROFFSET, result.Length
- SNDHDROFFSET);
}
SoundPlayer sp = new SoundPlayer(ms) ;
sp.Play();
}
}
catch (Exception e)
{
MessageBox.Show ("Sound header error! The sound clip
is not properly formatted in the SQL database. "+ e.Message);
}
The constant SNDHDROFFSET is set to 86. The soundclip sounds exactly the
same using either the sp.Play() method or by using MS Media Player.
Any ideas?
"Peter Bromberg [C# MVP]" wrote: Ok. In that case what I would do is retrieve on of these sound files from your database image field, and save it on the filesystem with a .Wav extension. Then attempt to play it using your application.
If it doesn't play, you have more information about where and what is going wrong. Cheers, Peter -- Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com
As a followup, I believe my problem may be in the stored procedure I posted
in my original message. I am very noobish to SQL, esp. in stored procedures,
and think maybe I'm supposed to be referring to READTEXT or textptr(), but am
not sure. As it stands, I pass the byte[] buffer in one of the parameters to
the INSERT/UPDATE commands in my code. I do no other processing. Am I correct
in assuming this may be where the problem lies?
The reason I deduce this is that when I look at the hex representation of
the stored data as opposed to the original sound clip file, they are nothing
alike.
"Peter Bromberg [C# MVP]" wrote: Ok. In that case what I would do is retrieve on of these sound files from your database image field, and save it on the filesystem with a .Wav extension. Then attempt to play it using your application.
If it doesn't play, you have more information about where and what is going wrong. Cheers, Peter -- Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com
"wschlichtm an" wrote:
Actually, the header information I have (86 bytes) is correct. I am using, and have been using, the System.Media.So undPlayer class to play the file from a database image field. It works perfectly. Attempting to use any other offest other than 86 throws an exception with an invalid header.
I am not using the WaveOut API as there is no longer a need to do so since the latest .NET class library includes replacement functionality.
"Peter Bromberg [C# MVP]" wrote:
Your assumption here that the header for a WAV sound file is either 78 or 86 bytes is incorrect. WAV headers are typically 44 bytes. In addition, you would still need the WAV header to be intact in order to play the sound using the WaveOut Windows API, unless you were to play it as RAW PCM Data at a fixed sampling rate and channel(s). -- Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: benjamin |
last post by:
A pygame/python game resource question
######################################
I wander whether there is any possibility to compile a bunch of
resources for a program, like images and soundfiles into a package
like, let´s say "game.dat", so they do´t fly around in the programs
folder und can be edited by everyone.
Hope somebody can help me.
|
by: Vinay |
last post by:
Hi All:
I have a small application that stores images either in the database or as
files (depending on the user preference). I'm in the process of providing a
web interface to this application.
1. If the images are stored in the DB then, every an image is requested, it
will need to be pulled out and a temp file created and then displayed? What
is the best way to do this?
|
by: Chris Beall |
last post by:
I'm struggling with how to handle, on a web page, images that contain
text that the user must be able to read. Examples: tombstone photos,
photos or scans of historic documents (handwritten or typed), a map with
place names, route numbers, etc.
(An appropriate alternative for audio UAs would be provided and is not a
part of my concern).
I see two ways to handle these images:
1. Dynamic scaling, with the source image containing enough...
|
by: mikeoley |
last post by:
Ok I have a Javascript slideshow working. Every image is linked to a
another page in the site. Problem is...The link wont refresh to the
next link once the second images rollovers in the slideshow. It only
stays at the first images link.
Is this a cache issue? Or is there anyway to create a random number to
trick this or make it work properly. I'm very raw with Javascript so
I'm having trouble figuring this out.
Thank you in advance
|
by: Neo Geshel |
last post by:
I am seeking to hand-roll my own blog in ASP.NET 2.0 and SQLExpress
2005. Why? Because I can. Because I will gain experience.
The one thing that has me stumped at square one is inline images. That
is, images inline with the actual content of the blog itself. Is there
an example that I can be pointed to, where I can examine some code and
figure out how to do this? Frankly I haven't got a clue, aside from
breaking the content up into...
| |
by: kp87 |
last post by:
I am a little bit stuck ....
I want to play a bunch of soundfiles randomly, but i want to give each
soundfile a rating (say 0-100) and have the likelihood that the file be
chosen be tied to its rating so that the higher the rating the more
likely a file is to be chosen. Then i need some additional flags for
repetition, and some other business. I am guessing a dictionary would
be a great way to do this, with the key being the soundfile...
|
by: phil-news-nospam |
last post by:
Why does SVG need a different tag than other images?
IMHO, SVG should be implemented as an image type just like any other image
type, allowing it to work with <img> tags, and ... here is the important
part ... also work with backgrounds in other tags.
I fail to see any wisdom in making SVG different than say PNG (of course
the implementation of the rendering code would obvious be different).
--
|
by: Xah Lee |
last post by:
The following is a program to generate thumbnail images for a website.
Useful, if you want to do that.
It is used to generate the thumbnails for my “Banners, Damsels, and
Mores” project gallery. (
http://xahlee.org/Periodic_dosage_dir/lanci/lanci.html )
Comments and versions in other lang welcome.
Xah
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |