So this is what I want to do. I want to load an .wav music file from my project folder. I have done this with a image, as displayed below. I want to do this with an audio file. Is this possible? Is there an easy way to convert my old method to a new one. The reason I want to do this is because I want to have all the files for my program in a .jar file so I can just send that and it plays.
static audiotest music = new audiotest( "C:\\WINDOWS\\Media\\POL-purple-hills-short.wav" );
music.start();
URL mountainImage2 = Main.class.getResource("Mountains.png");
mountainImage = ImageIO.read(mountainImage2);
Related
I want to add audio to my JavaFX project in Replit. Is there a way to use a public accessible URL link that references an .mp3 file in order to play audio?
For example, the following allows me to include an image only using a URL without having to reference a file:
Image image= new Image("https://cdn.discordapp.com/attachments/952426047645843537/954119846046629928/play3.png");
Is something similar possible for audio files?
You can try this
Media sound = new Media("https://yourURL/file.mp3");
MediaPlayer mediaPlayer = new MediaPlayer(sound);
mediaPlayer.play();
I'm recently starting with codenameone and I'm working on a music player app. I want to get the musics from a folder. I found out in the documentation a code about "Audio Capture & Recording".
But what I'm looking for is to just play audios from a specific folder.How can I adjust this code? I'm also not finding where these audios are being saved to.
Here's the code+ a footage of the Recording code.
Button button = new Button("musiques!");
Form hi = new Form("musiques", new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE));
hi.addComponent(BorderLayout.CENTER, LayeredLayout.encloseIn(BoxLayout.encloseY(button)));
button.addActionListener((e) -> {
InputStream streamFromResource = CN.getResourceAsStream("/filename.mp3");
try {
MediaManager.createMedia(streamFromResource, "audio/mp3");
} catch (IOException ex) {
System.out.print("non");
}
});
The audio is in fs.getAppHomePath() + "recordings/". Bundling the audios into the jar will increase your jar size and final app size which will exceed the free quota so we don't normally recommend that.
If you still choose to go with that route you can store the files in the root of the src folder and access them using CN.getResourceAsStream("/filename.mp3").
Then you can just use:
Media m = MediaManager.createMedia(streamFromResource, "audio/mp3");
Alternatively you can store the files on the web and use an HTTPS URL instead of a file URL to play them.
You can't dynamically list the files since there's no listing option for HTTP or for the contents of the jar.
I want to play .mp3 files in Vaadin 14. This is my audio player.
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Tag;
#Tag("audio")
public class AudioPlayer extends Component {
/**
*
*/
private static final long serialVersionUID = 1L;
public AudioPlayer(){
getElement().setAttribute("controls",true);
}
public void setSource(String path){
getElement().setProperty("src",path);
}
}
AudioPlayer player = new AudioPlayer();
player.setSource("music/my music file.mp3");
add(player);
But when I try to play .mp3 files, nothing happens. What have I missed?
Do I need to convert .mp3 files to .wav before? How can I do that just temporary.
I'm not planning to save any .wav files on the computer, because I already have .mp3 files stored.
Your approach should work, I just create a PR to the Vaadin cookbook with a recipe for this.
Note that the browser needs to be able to access the audio file through that same path. If you set the src to audio/mysong.mp3, then you should be able to open it in the browser also as e.g. localhost:8080/audio/mysong.mp3 (or the equivalent URL for your setup).
Take a look at the ways of importing in Vaadin to see where to put your file, in particular the Resource Cheat Sheet for static files.
Edit:
I'm not sure why your files don't work on the first try, but I could reproduce it in your project, also with my own mp3 files. You can see an error 416 in the console, something to do with a mismatch in the range of bytes requested.
I found a workaround that you could try (you might want to move your audio to just src/main/resources for this, and/or update the AudioPlayer to accept an AbstractStreamResource):
if(!reverseTranslation.getValue()) {
frenchSentence.setValue(sentenceInFrench);
String audioPath = "/META-INF/resources/audio/" + sentenceInFrench + ".mp3";
AbstractStreamResource resource =
new StreamResource(sentenceInFrench, () -> getClass().getResourceAsStream(audioPath));
player.getElement().setAttribute("src", resource);
}
I'm building an application that edits tags on mp3 files using JAudioTagger. The only problem is that the mp3 file's fileds don't change when i open the file in Google Music app.
TagOptionSingleton.getInstance().setAndroid(true);
File tempFile = new File(Environment.getExternalStoragePublicDirectory("smoething"), songname + ".mp3");
AudioFile audioFile = AudioFileIO.read(tempFile);
Tag tag = audioFile.getTag();
tag.setField(FieldKey.ARTIST, "artist"); // when i open music app the artist is "unknown"
audioFile.setTag(tag); // even without this i'm getting the same result
audioFile.commit();
According to this answer the code should be working fine but for some reason it doesn't. Does someone have a clue what i have done wrong?
So i found out that the problem was with the method commit() (it just didn't worked) so i used AudioFileIO.write(audioFile) instead that does the same thing.
I made a small game that requires to play background music on it, I have images, a txt file and an audio file, they all work after exporting the JAR except for the audio file.
here is the code I used to import :
The Images :
(new ImageIcon(getClass().getResource("/data/131868.jpg"))
The Text file :
BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/data/dictionnaire.txt")));
The Audio file ( I included also the code to play it that I found while searching) :
File f =new File(Main.class.getResource("/data/gmu.mp3").getFile());
final Player p=Manager.createRealizedPlayer(f.toURI().toURL());
p.start();
while(true){
if(p.getMediaTime().getSeconds()==p.getDuration().getSeconds()){
p.stop();
p.setMediaTime(new Time(0));
p.start();
}
}
Basically the File Object was : File f = new File("/data/gmu.mp3") I just added modifications to make it look like the others ...
It did work in Eclipse, but not JAR.
You'd better know that: File is just the name of the file, not the file itself. Like the house number, it tells you the house's location, but is doesn't represent the house.
So, you can use it like this:
InputStream is = this.getClass().getResourceAsStream("/data/gmu.mp3");
File fi = new File(is);
Tell me the result:)
Solution
Try placing the gmu.mp3 inside the source before exporting the jar.
Why?
When you export a jar, it wraps all the fun code up that is inside the source folder. File f = new File("/data/gmu.mp3") simply points the program to that file names gmu.mp3 in the file system. If you place the gmu.mp3 inside the source folder and update the File constructor to reflect the new location, the mp3 should get wrapped up into the jar along with all the code.
Let me know how it goes -Scott
A java.io.File represents a jar-ed file as "ThePacked.jar!/path/inside/file.mp3", which makes problem when used as a File.
To read a jar-ed file's content, you can read from an InputStream given by getResourceAsStream(String filename).
or
To use the file as a real java.io.File, (what I did is to) read it from the InputStream and copy it to a location outside the jar (eg. to a temprary file).