Delphi tmediaplayer don't play mp3 file with "strange" cover image embedded - image

I have developed a mp3 player with Delphi (XE) using the BASS library.
Due to certain reasons, I want to remove the BASS libraries and want to use the TMediaPlayer component in Delphi (also want to "move" the project to Delphi 10 Seattle).
Now I found out that some of my mp3 files have a "strange" jpg image embedded.
Means, that the Delphi components run into an error due to the image.
With long time debugging I can say the following:
try
mplMain.FileName := CurrentSong;
progbSong.Max := mplMain.Duration;
lblDuration.Text := DurationToString(mplMain.Duration);
PlayClick(Self);
except
on E: Exception do
begin
FMX.Dialogs.MessageDlg('Cannot play song: ' + CurrentSong + #10 + #13 +
'Reason: ' + E.Message,
TMsgDlgType.mtWarning, [TMsgDlgBtn.mbOK], 0,
procedure(const AResult: TModalResult)
begin
MediaNext;
end
);
end;
end;
This line:
mplMain.FileName := CurrentSong;
causes the problem.
Diving deeper in debugging it comes to here:
FMX.Media library:
procedure TMediaPlayer.SetFilename(const Value: String);
...
FMedia := TMediaCodecManager.CreateFromFile(FFileName);
...
At the end it ends up in FMX.Media.Win:
constructor TWindowsMedia.Create(const AFileName: string);
...
HR := FGraphBuilder.RenderFile(PChar(AFileName), nil);
...
When the line
HR := FGraphBuilder.RenderFile(PChar(AFileName), nil);
is called, in debug mode, the program just returns to the IDE.
In runtime mode, nothing happens. No error message, just "nothing".
As you can see, I wrapped the related line into a try...except block, but no error is raised. The program/player doesn't continue.
That's very bad for me, because I wanted to catch this "special case" and log the affected mp3 files to a logfile so that I can change the embedded image.
I found out that it is only caused by some images. Maybe they are "somehow corrupt", but shown in all other players.
When I remove the image and embed a "new" one and save the file, everything is fine and the TMediaPlayer can play the file.
How can I catch this certain kind of "error" to get the list of affected files?

I got it managed now.
Only in debug mode the application/player is exited without any thrown error and I find myself back in the IDE.
During runtime the try...except block works, when I chose an "affected file" manually. For the case that one file is played ("good one") and the next file is a "bad one", I had to change my "MediaNext" procedure. In this procedure I also had a try...except block when the filename was associated to the TMediaPlayer, but I just had set a bool variable for further use and didn't "jump" to the next file.
The code just was:
try
mplMain.FileName := CurrentSong;
except
on E:Exception do
SongNotPlayable := true;
end;
Here I can implement a routine to log the affected mp3 files into a logfile and then jump to the next file (if exists). :-)
Thanks again to all!

Related

Delphi: Unable to open .bmp file

I'm trying to load an image into a stringgrid cell
if CheckBox5.Checked = True then
begin
Ver := Ver + 1;
Bitmap := TBitmap.Create;
Bitmap.LoadFromFile('media/belgie_vlag.bmp');
Form3.StringGrid1.Canvas.StretchDraw
(Form3.StringGrid1.CellRect(Hor,Ver),Bitmap);
Bitmap.Free;
end;
When I run the application, an error shows saying the .bmp file cant be opened. Any help?
The most likely problem is that the file cannot be found, because you didn't specify an absolute path. You should always use absolute paths (e.g., C:\Users\Andreas Rejbrand\Desktop\image.png, not image.png).
For instance, if the media folder is a sibling to the executable file, the absolute path is ExtractFilePath(Application.ExeName) + 'media\belgie_vlag.bmp'.
The following code should then work:
if CheckBox5.Checked then
begin
Ver := Ver + 1;
Bitmap := TBitmap.Create;
try
Bitmap.LoadFromFile(ExtractFilePath(Application.ExeName) + 'media\belgie_vlag.bmp');
Form3.StringGrid1.Canvas.StretchDraw
(Form3.StringGrid1.CellRect(Hor,Ver),Bitmap);
finally
Bitmap.Free;
end;
end;
Notice a few more things:
You must always protect resources with try..finally blocks, as above. Otherwise, in this case, if an exception is raised when you load or draw the image, you will leak the image object, making your application eat memory and get a limited lifetime.
There is no need to write if mybool = True then; if mybool then is enough.
You should only draw in the string grid in the appropriate event handler. I cannot tell where your code is placed, so I don't know if you are doing it the right way or not.

Is there a way to print to output console? (twincat3)

Is there a way to print to output to console like debug.print() in VB.NET using structured text? (twincat3)
You can send messages through ADS commands from TwinCAT code. The function is called ADSLOGSTR. There also also own functions for DINT and REAL, but the STRING function of course can be used with anything.
The function has three inputs:
msgCtrlMask
Mask that describes the message type
Types can be found here
For example, to show warning message and save it to Windows log: msgCtrlMask := ADSLOG_MSGTYPE_WARN OR ADSLOG_MSGTYPE_LOG
To show just a Windows MessageBox: msgCtrlMask := ADSLOG_MSGTYPE_MSGBOX
msgFmtStr
The message to be shown as STRING
A %s can be used to add parameter without CONCAT functions. See the last parameter.
strArg
A STRING that is replaces the %s in previous string.
Here is an example the probably is what you need:
IF test THEN
ADSLOGSTR(
msgCtrlMask := ADSLOG_MSGTYPE_HINT,
msgFmtStr := 'Test message. Parameter is %s',
strArg := 'ABC'
);
test := false;
END_IF
When you set the test true, and call the function, you will see this on your Visual Studio error list. Note that it is not written to console.
I often use error messages (ADSLOG_MSGTYPE_ERROR) because I hide notes and warnings quite often and the I wouldn't notice my own entries. Other good way is to add the entry to the Windows log, if you want to log something to be seen later.

Read/Write to file on old IBM PS/2 in turbo pascal 5.5

The Question: I recently acquired a 1989 IBM PS2 and I am trying move large files from my newer UNIX-based machine to this IBM via floppy. I have a bash script that splits my files into ~2MB chunks, now I am trying to write a pascal program to reconstruct these files after they have been transferred.
I am unable to find the correct read/write to file methods on this computer. I have tried various pascal tutorial sites, but they are all for newer versions (the site I followed with File Handling In Pascal). I am able to create an empty file (as described below), but I am unable to write to it. Does anyone know the correct pascal read and write methods for this type of computer?
I know this is an obscure question, so thank you in advance for any help you can give me!
The Details:
The current test code that creates a file correctly is this:
program testingFiles;
uses Crt, Win;
const FILE_NAME = 'testFile.txt';
var outFile : File;
begin
writeln('creating file ...');
Assign(outFile, FILE_NAME);
rewrite(outFile);
end.
This is some test code that does not work, the method's append() and close() could not be found:
program testingFiles;
uses Crt, Win;
const FILE_NAME = 'testFile.txt';
var outFile : File;
begin
writeln('creating file ...');
Assign(outFile, FILE_NAME);
append(outFile);
writeln('this should be in the file');
close(outFile);
end.
This is an alternative that also did not work, the writeln() method only ever prints to the terminal. But otherwise this does compile.
program testingFiles;
uses Crt, Win;
const FILE_NAME = 'testFile.txt';
var outFile : File;
begin
writeln('creating file ...');
Assign(outFile, FILE_NAME);
rewrite(outFile);
writeln('this should be in the file');
close(outFile);
end.
The system: As was previously mentioned, this is a 1989 IBM PS2.
It has Windows 3.0 installed and can also run DOS and MS-DOS terminals.
It has Microsoft SMARTDrive Disk Cache version 3.06
It has Turbo Pascal 5.5 installed and I am using turbo as my command line pascal editor. (the readme was last updated in 1989)
It has Turbo debugger 1.5 installed.
Again, I know this is an obscure question, so thank you in advance for any help you can give me!
My Pascal memory is VERY rusty... but as other have pointed out, here is what you should consider:
program testingFiles;
uses Crt, System;
//No need of importin Win Win is for Windows enviorment, however I'm not sure if you need to use System, Sysutils or was there a Dos class???
const FILE_NAME = 'testFile.txt';
var outFile,inFile : File;
begin
writeln('creating file ...');
Assign(outFile, FILE_NAME);
rewrite(outFile);
//Now Open the first chunk of the file you want to concatenate
AssignFile(inFile, "fisrt_chunk.dat");
reset(inFile);
while not eof(inFile) do
begin
readln(inFile, s);
writeln(outFile,s);
end;
close(inFile);
end.
I don't have Turbo/Borland Pascal installed any longer so I couldn't compile it myself, no promise that it will work it is more like an idea:
Key thing to remember, readln and writeln will ALWAYS add a return at the end of the string/line, read and write on the other hand will leave the cursor wherever it is without jumping to a new line.
Here's some old Delphi code that should be at least close to syntax-compatible that will give you the gist of copying a file (with limited error checking and resource handling in case of error - I'll leave that as an exercise for you). It works to copy both binary and text content.
program Project2;
uses
SysUtils;
var
NumRead, NumWritten: LongInt;
pBuff : PChar;
SrcFile, DstFile: File;
const
BuffSize = 2048; // 2K buffer. Remember not much RAM available
InFileName = 'somefile.txt';
OutFileName = 'newfile.txt';
begin
NumRead := 0;
NumWritten := 0;
AssignFile(SrcFile, InFileName);
AssignFile(DstFile, OutFileName);
// Allocate memory for the buffer
GetMem(pBuff, BuffSize);
FileMode := 0; // Make input read-only
Reset( SrcFile, 1 );
FileMode := 2; // Output file read/write
Rewrite( DstFile, 1 );
repeat
// Read a buffer full from input
BlockRead(SrcFile, pBuff^, BuffSize, NumRead);
// Write it to output
BlockWrite(DstFile, pBuff^, NumRead, NumWritten);
until (NumRead = 0) or (NumWritten <> NumRead);
// Cleanup stuff. Should be protected in a try..finally,
// of course.
CloseFile(SrcFile);
CloseFile(DstFile);
FreeMem(pBuff);
end.
The above code compiles under Delphi 2007 currently (the oldest version I have installed). (See the note below.)
As a side note, this was from an archived version of some code I had that compiled both for 16-bit Delphi 1 and was extended to also compile under 32-bit Delphi 2 back in the mid-to-late 90s. It's still hanging around in my source repositories in an old tagged branch. I think I need to do some pruning. :-) I cleaned it up to remove some other functionality and removed a lot of {$IFDEF WIN32} ... {$ELSE} ... {$ENDIF} stuff before posting.)

Lazarus(Pascal) RunError(5)

My program exits with RunError(5), which would suggest that it can't access the file, which it should be able to. I have checked and the file is used as it should be, the file isn't read-only, etc. What the program does is, it creates a .dat file if one doesn't exists and uses it for saving stuff. If I run the program and the file doesn't exist, the file is created, but after that, in the same execution, the program won't access the file. This ONLY happens if the file was created in the current execution.
This is the way in which the procedures are called(the code is quite long but I am giving you the first few lines, where the error occurs):
fileName := 'labSave.dat';
CreateFile;
assign(labyrinthFile,fileName);
writeln(CheckFileSize);
and then there is each of the procedures:
procedure Initialize;
begin
fileName := 'labSave.dat';
assign(labyrinthFile,fileName);
end;
procedure CreateFile;
begin
if not FileExists(fileName) then FileCreate(fileName);
end;
function CheckFileSize: integer;
begin
reset(labyrinthFile);
CheckFileSize := FileSize(labyrinthFile);
close(labyrinthFile);
end;
According to Lazarus forum (http://forum.lazarus.freepascal.org/index.php?topic=4936.0):
Runtime Error 5 means Access denied. The file maybe readonly and you
use the wrong (default) filemode, or you try to re-open the file with
a new filehandle without having closed it before (somewhere in the
while and repeat loops possibly you assignfile more then once, then
the reset fails?).
If I recall correctly now, the workflow should be as follows for create:
AssignFile(f, filename); Rewrite(f); CloseFile(f);
and for existing file:
AssignFile(f, filename); Reset(f); CloseFile(f);
Seeing other mistakes found in your code through questions in comments, I strongly suggest you to devote more time to debugging and when such errors happen - strip out ALL of the irrelevant code and check your code design for cases like above (assigning file before creating it, etc.).

What have I missed in this custom format copy and paste?

My copy code:
if OpenClipboard(mainwnd.Handle) then
MemHandle := GlobalAlloc(GHND or GMEM_SHARE, Succ(StrLen(pLclCopies)));
if MemHandle <> 0 Then
Begin
try
StrCopy(GlobalLock(MemHandle), pLclCopies);
GlobalUnlock(MemHandle);
SetClipboardData(cf_LocalVar,MemHandle);
Finally
CloseClipboard;
GlobalFree(MemHandle);
end;
end;
and my paste code:
if clipboard.HasFormat(cf_LocalVar) then
begin
ClipBoard.Open;
try
MyHandle := Clipboard.GetAsHandle(cf_LocalVar);
LocalsTextPtr := GlobalLock(MyHandle);
CheckForCopiedLocals(LocalsTextPtr, TextPtr); //What I do with the pasted data.
GlobalUnLock(MyHandle);
finally
Clipboard.Close;
end;
end;
My goal is to copy not only text from a special editor in my program, but also to copy some underlying variable data related to that editor. Most everything seems to be working fine clipboard wise - I'm seeing my copied text, and the 'cf_LocalVar' format appear in the ClipBook viewer on windows.
It's when I get to the paste side and the line
LocalsTextPtr := GlobalLock(MyHandle); doesn't get the copied data from the first bit of code. I see that it makes it into pLclCopies but then can't be sure that it's stored in the clipboard.
NB I have left out emptyclipboard from my code as this would get rid of the cf_text that I need along with the cf_LocalVar.

Resources