Javafx MediaException: MEDIA_UNAVAILABLE jar file - javafx

When I create a jar package of my maven project, the program does not find my media files anymore. I found several similar question from the google, but none of them helped me.
I have tried all of these options:
Document Base: Works fine on IDE, jar package throws MEDIA_UNAVAILABLE
// I get documentBase from the class which extends Application
String documentBase = Application.getHostServices().getDocumentBase();
Media media = new Media(documentBase + "src/main/resources/music/music.mp3");
Paths.get: Works fine on IDE, jar package throws MEDIA_UNAVAILABLE
Media media = new Media(Paths.get("src/main/resources/music/music.mp3").toUri().toString());
Get resource: Throws null
Media media = new Media(Audio.class.getResource("/music.mp3").toString());
Inputstream: Throws null
InputStream in = Audio.class.getResourceAsStream("/music.mp3");
Media media = new Media(new InputStreamReader(in).toString());
Inputstream with getClassLoader(): Throws null
InputStream in = Audio.class.getClassLoader().getResourceAsStream("/music.mp3");
Media media = new Media(new InputStreamReader(in).toString());
The Audio class on my examples is an abstract class, which handles everything through static methods and variables. I am using Java 11.
EDIT:
Added the correct path to examples.

I was able to fix it by using this path:
Media media = new Media(Audio.class.getResource("/music/music.mp3").toString());
Since my file was inside a folder "music" inside resources folder, I had to include it too. Also note that "music/music.mp3" does not work, it needs the slash in front of it. This is quite confusing since when adding Images to the project, it does not need the slash.
Thanks to #fabian for explaining why my approaches did not work.

Related

'BitmapStub' does not exist in the namespace 'Android.Graphics' Using Xamarin.Forms

I implemented a application for Sunmi T1 mini device in-build printer in Xamarin forms and i want to print a image with some text like billing receipt.
I integrated AIDL file for Sunmi T1 mini device you can see in image.
But i am facing namespacing issue in IWoyouService.cs "the type of namespace name 'BitmapStub' does not exist in the namespace 'Android.Graphics'."
case TransactionPrintBitmap: {
data.EnforceInterface (descriptor);
Android.Graphics.Bitmap arg0 = default (Android.Graphics.Bitmap);
arg0 = Android.Graphics.BitmapStub.AsInterface (data.ReadStrongBinder ());
ICallback arg1 = default (ICallback);
arg1 = ICallbackStub.AsInterface (data.ReadStrongBinder ());
this.PrintBitmap (arg0, arg1);
reply.WriteNoException ();
return true;
}
I attach my AIDL Zip file https://drive.google.com/file/d/1lrCgZwDrfyqs6LAwTP3pQ6nMzYIW4hrY/view?usp=sharing.
How can resolve this error in Xamarin.Forms
I had this exact same problem, and managed to solve it by adding another AIDL file to define what a 'Bitmap' is.
I copied this file from the Android Open Source Project and just added it into my project and set the build action to Android Interface Definition like the other Sunmi AIDL files.
Not sure why Xamarin doesn't manage to handle this automatically, but adding this solved the problem for me and I was able to build and successfully invoke the interface methods that accept bitmap parameters.
In case the link ever goes bad, here is the entire file you need to add - it's very small:
/* Save this in Bitmap.aidl and add to your project alongside the others */
package android.graphics;
parcelable Bitmap;

Web Resource was not found

I've spent the last day trying to get a checkbox validator to work. I found some starting code on the 4GuysFromRolla website, however this was coded back in 2006 and doesn't play alongside updatepanels.
I've made changes and now it works. As I'd made quite a few changes I wanted to standardise it and use it alongside other utility classes, so I created a new class project and copied the code verbatim.
The problem is that the original works, but I get a WebResource not found error on my new class project.
The differences are: The original was a web application project:
my new project is simply a class library project.
The original assembly and namespace have changed.
Both contain a single class and a resource file (.js)
Both resource files are contained in the root directory
Both resource files are set to embed resource
I'm running out of ideas, and have exhausted the answers I've found on the web with no success.
Is there an issue with the fact that its a class library? Have I perhaps overlooked something else?
Its pretty difficult to see beyond those 2 questions because the project is so small and insignificant.
Here's the code that does work:
if (this.RenderUplevel && this.Page != null)
{
ScriptManager.RegisterClientScriptResource(Page, this.GetType(), "skmValidators.skmValidators.js");
}
Here's the code that doesn't:
if (this.RenderUplevel && Page != null)
{
ScriptManager.RegisterClientScriptResource(Page, this.GetType(), "ValidationExtender.EvaluationFunctions.js");
}
Anyone give any ideas?
Thanks in advance.
The error turned out to be a mismatch between the dll's in the test page.

#font-face with Flying Saucer

I am hopping somebody can help me... it seems like what I am trying to do should be fairly simple but I have been fighting this thing for over a day now and am out of ideas. I have found lots of information on StackOverflow and the Internet at large but nothing has helped me resolve this issue.
I am trying to use itext-2.0.8 along with core-renderer-R8 to create a PDF with an embedded font. I am trying to generate the PDF from valid XHTML and am embedding the font using the #font-face style tag. I have confirmed that the #font-face tag is including the font by opening the file in a browser. And I am always careful to keep the TTF fiel relative to teh XHTML/CSS doc.
In order to try and work my way through this I have created a small 'Hello World' type program to try and embed a font. I have taken two separate approaches and both of them fail to produce the desired result. I have place a copy of this little Eclipse program at http://christopherluft.com/FlyingSaucer.zip
The program produces a PDF in both instances but neither has the PDF embedded as expected. The first method using a file with setDocument produces no errors but also no fonts. And the second method produces a PDF but shows a java.net.MalformedURLException in the debug output.
I have tried numerous permutations of the various paths and URLs; however, none fail to produce the desired result. My suspicion is that I am failing to understand something about ITextRenderer.setDocument; however, I have had a really hard time finding any proper documentation specific to my use case.
The first method I am trying is:
public static void main(String[] args) throws IOException, DocumentException {
System.getProperties().setProperty("xr.util-logging.loggingEnabled",
"true");
XRLog.setLoggingEnabled(true);
String inputFile = "sample.xhtml";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
}
And the second method I am using (which is closer to the actual way we use it in our app) is:
public static void main(String[] args) throws IOException, DocumentException {
System.getProperties().setProperty("xr.util-logging.loggingEnabled", "true");
XRLog.setLoggingEnabled(true);
String inputFile = "sample.xhtml";
String url = new File(inputFile).toURI().toURL().toString();
DocumentBuilder documentBuilder;
org.w3c.dom.Document xhtmlContent;
try
{
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
documentBuilder.setEntityResolver(new XhtmlEntityResolver(new SuppressingEntityResolver()));
xhtmlContent = documentBuilder.parse(url);
System.out.println(url);
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(xhtmlContent,".");
renderer.layout();
renderer.createPDF(os);
System.out.println("Finishing up....");
os.close();
}
catch (SAXException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And the #font-face inclusion in the XHTML looks like this:
#font-face {
font-family: 'MisoRegular';
src: url("miso-regular-webfont.ttf");
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}
Now I feel like this is a really common use case and I imagine that I am just failing to perform one simple step in order to get this to work... the problem is that I have been at this for a while and think I am unable to see the forest through the trees. Any help anybody could offer me would be greatly appreciated. Thank you for your time.
I've experienced problems with Flying Saucer where it seemed the fonts were not embedded properly and it ended up being that the font I was trying to embed was corrupted or at least not complete. Attributes regarding the Font Family and description were missing. When I tried validating my font using the Validate File option in FontBook on OS X I was promptly presented with warnings saying:
'name' table usability
'name' table structure
However FlyingSaucer failed silently and just proceeded with embedding Times-Roman and Times-Bold as default fonts. When I tried using a font that passed the FontBook validation I found the font was embedded properly.
I spent hours trying to increase the verbosity in logging from FlyingSaucer to get more information and didn't discover this was the case until I stepped through the entire rendering process and just happened to note that FontFamily was set to "" in the debugger after loading the font and that it didn't include the name I expected it to be registered under.
Bottom line: If you're having an issue with FlyingSaucer and fonts and are banging your head against the wall make sure your font is valid.
I struggled with the same problem like tekgrunt, but finally I got a PDF with a custom font (Computer Modern) embedded. My experiences:
OTF format for the font didn't work (crash in an iText function)
TTF format DID work, after I made the following change: In the CSS, use the same font-family name like the one that is encoded in the font. Set a breakpoint in org.xhtmlrenderer.pdf.ITextFontResolver.addFontFaceFont() and find out which value the variable "fontFamily" gets. If this isn't the same like the CSS font-family, I guess it won't work.
Of course these problems should be reported by the library to the user.
I was facing a similar issue, font not getting applied to the final pdf, and m4t's suggestion about debugging really helped me in identifying the issue. So thanks for that!
In my case actually there were two issues:
The src attribute specified as part of #font-face rule had 2 values, the url and the format. It looked something like
#font-face {
src: url("../../static/fonts/OpenSans-Regular.ttf") format("ttf");
}
Because of the second value format, the CSSParser broke causing the src attribute to be completely skipped silently. As a result in further processing the render did not know where to pick the fonts from. The issue got sorted once I removed the format part from the value.
Once the url was parsed successfully by the CSSParser, it could not be resolved correctly, as the url specified in the css was a relative one and there was no baseUrl specified explicitly (optional 2nd parameter) when calling the setDocumentAsString on the renderer. I fixed the issue by loading uri of static resources explicitly in my code using the classloader, and then setting that as the base url. After that change the path for the font file was resolved successfully and applied to my pdf as well.
Critical thing to note here was, that the base url supplied to
setDocumentAsString must be a URI (file:/folder/structure/static/),
and must end with a trailing slash ('/'). Otherwise the file and url
doesn't get resolved correctly.
Hope this helps someone facing a similar problem.

"Link" against a SWC in Flex

I'm trying to do a very simple app in Flash/Flex, that loads an image embedded in the swf itself and then shows it. The thing is I'm trying to do it using the command line only (mxmlc and compc) and without using #Embed, and failing miserably.
I have a very simple Main.as :
package
{
import flash.display.*;
import flash.utils.*;
public class Main extends Sprite
{
public function Main () : void
{
var pDef:Class = getDefinitionByName("icon_big.png") as Class;
var _image:BitmapData = new pDef(0, 0);
var pSprite:Sprite = new Sprite();
pSprite.graphics.beginBitmapFill(_image);
pSprite.graphics.drawRect(0, 0, _image.width, _image.height);
pSprite.graphics.endFill();
addChild(pSprite);
}
}
}
This works fine if I add icon_big.png to the Library using the Flash IDE, but I can't figure out how to do it from the command line.
I'm using compc to put the png inside a swc :
compc --include-file icon_big.png icon_big.png -output assets.swc
This generates a 17 kb assets.swf, slightly bigger than icon_big.png. Then I try to compile and link Main.as :
mxmlc -include-libraries+=assets.swc Main.as
This produces a 944 byte Main.swf, which clearly doesn't include the asset, and fails at runtime.
According to the mxmlc docs I found, -include-libraries should link with every class, including the ones not directly referenced by code (as is the case here, since I'm getting the class from code), and it unsurprisingly fails at runtime.
Note that this same code (or, more precisely, quite equivalent code) works when used within a Flash project - I'm not looking to fix the code, but how to do in the command line whatever Flash does internally.
I feel I'm just "not getting" something... any clues?
I recommend you to download the swftools from swftools.org. Once you have them, run:
swfdump -D assets.swf
Take a look in particular at the output which relates to SWF tag with value 76 (0x4C), called SYMBOLCLASS.
Here is an example of an exported class, named IntegerMemberBySlot:
[04c] 24 SYMBOLCLASS
exports 0001 as "IntegerMemberBySlot"
What symbols are you exporting from your assets.swf?
Have you tried adding the path of the assets to your source path parameter when compiling with mxmlc?
-source-path ./PATH/TO/ASSET
I think you need to embed the PNGs in a class, then compile that class into a SWC. I don't think you can put the PNGs directly into a SWC like you are trying to, but I could be wrong.
I had a similar situation (large project, resource swc with graphic assets), and no matter what I tried, I could't get Flex to include assets and classes not directly referenced from the project (I wanted to include different skins, and then instantiate the proper one at runtime, depending on the configuration).
Finally, I found a workaround by instead of static swc liking, switching over to runtime shared libraries (RSLs). Basically, flex unbundles the swc, and loads and links the included swf in runtime, before running the application itself. All of the classes and assets in the swf are loaded this way. May not be exactly what you're after, but it works for me.

Preventing Flex application caching in browser (multiple modules)

I have a Flex application with multiple modules.
When I redeploy the application I was finding that modules (which are deployed as separate swf files) were being cached in the browser and the new versions weren't being loaded.
So i tried the age old trick of adding ?version=xxx to all the modules when they are loaded. The value xxx is a global parameter which is actually stored in the host html page:
var moduleSection:ModuleLoaderSection;
moduleSection = new ModuleLoaderSection();
moduleSection.visible = false;
moduleSection.moduleName = moduleName + "?version=" + MySite.masterVersion;
In addition I needed to add ?version=xxx to the main .swf that was being loaded. Since this is done by HTML I had to do this by modifying my AC_OETags.js file as below :
function AC_FL_RunContent(){
var ret =
AC_GetArgs
( arguments, ".swf?mv=" + getMasterVersion(), "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
, "application/x-shockwave-flash"
);
AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}
This is all fine and works great. I just have a hard time believing that Adobe doesn't already have a way to handle this. Given that Flex is being targeted to design modular applications for business I find it especially surprising.
What do other people do? I need to make sure my application reloads correctly even if someone has once per session selected for their 'browser cache checking policy'.
I had a similar problem, and ended up putting the SWF files in a sub-directory named as the build number. This meant that the URL to the SWF files pointed to a different location each time.
Ideally this should be catered for by the platform, but no joy there. But this works perfectly for us, and integrates very easily into our automated builds with Hudson - no complaints so far.
Flex says:
http://www.adobe.com/livedocs/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001388.html
What I have done is checksum the SWF file and then add that to its url. Stays the same until the file is rebuilt/redeployed. Handled automagically by a few lines of server-side PHP script
here is sample.
function AC_FL_RunContent(){
var ret = AC_GetArgs(arguments, ".swf?ts=" + getTS(), "movie",
"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000",
"application/x-shockwave-flash");
AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}
function getTS() {
var ts = new Date().getTime();
return ts;
}
AC_OETags.js is file and it exists html-template several places.
but as my posting said, I am facing another type of problem.
The caching is not done by Flash Player but by the browser, so it's out of Adobe's control. I think you have found a workable solution. If I want to avoid caching I usually append a random number on the URL.

Resources