I'm having big problems getting good sound out of my Raspi using Java.
I want to write an little AirPlay Client to a media server I wrote in Java. I started with using the Player Class from javazoom (http://www.javazoom.net/javalayer/docs/docs1.0/javazoom/jl/player/package-summary.html). Which gave me a not really choppy but somehow distorted and slower than normal playback of an mp3 file I streamed over to the Raspi.
My first idea was, that maybe the decoding of an mp3 was a bit too much for the Raspi, especially since overclocking helped a little.
So now I'm converting the mp3 to a wav file on the server and then stream it to the Raspi playing it with the javax.sound.sampled.* stuff. Yet, no improvement :|
Has anybody some experience with playing soundFiles from Java on an Raspi? Any advice helps!
Thanks, Stefan
I suggest you try using JavaFX instead. It is way better than support for media on the standard JDK. Besides, you are already using Java 7, so migrating will be easy. Here is how:
Media media = new Media(Utils.findURI(baseDirName, soundFileName).toString());
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Platform.runLater(new Runnable() {
#Override
public void run() {
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setCycleCount(1);
mediaPlayer.play();
}
});
}
});
the implementation of sound in OpenJDK has lots of bugs. One of them is that it abuses the sound system and monopolizes it. So on Linux you have better chances to get things working if you do as above.
Related
using UnityEngine;
using System.Collections;
public class audio : MonoBehaviour
{
public AudioClip hitsound;
void Update ()
{
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Ball")
{
GetComponent.<AudioSource>().PlayOneShot (hitsound);
}
}
}
I assign .mp3 file to inspector, and also, I added Audio Source component but I can't hear hit sound. Cubes which need to be destroyed is moving during game. I added that script and audio source component on parts which are not moving and when ball hit that non-moving parts, sound is playing (every time).
I hope that someone can help me with this.
Thanks and kind regards
YOU HAVE A TYPO
GetComponent.<
SHOULD BE
GetComponent<
Doesn't the . after GetComponent give you an error?
Anyway, make sure your colliders are set as triggers (checkbox on component).
Also I think Unity recommend using CompareTag() instead of ==.
It's worth putting a Debug.Log into the OnTriggerEnter2D to see if that is even firing.
Finally, make sure your colliders are the 2D versions, not just the regular colliders.
I'm developing a simple game using eclipse and libgdx.
Currently, I'm using 'Music' instead of 'Sound' for sound effects for my game.
I made a button for muting all of the sound fx but having a problem when it comes to 'sound' instead of music.
Here's my current code:
public static Music jump;
public static void load() {
jump = Gdx.audio.newMusic(Gdx.files.internal("data/jump.wav"));
}
public static void muteFX() {
lesgo.setVolume(0);
public static void normalizeFX() {
jump.setVolume(1f);
//'muteFX' and 'normalizeFX' to be called on different class
I wanted to change it to 'Sound' (the reason is I wanted it to be more responsive on fast clicks,) So it could be like this:
public static Sound jump;
public static void load() {
jump = Gdx.audio.newSound(Gdx.files.internal("data/jump.wav"));
}
/* here comes my problem, didn't know how to set mute and normal
volume for the jump sound. I know there is also a set volume method
to 'Sound' but really confused on the terms (long soundID, float volume)
Can someone make this clear to me on how to implement the long and soundID?
*/
I'm really new to libgdx, as well as in java. I researched many forums regarding this and still can't find a more clearer explanation.
Any help will be greatly appreciated.
Thanks a lot in advance! =)
A quick suggestion would be to use a global variable for sound
public static VOLUME = 1.0f;
The sound api allows you to play a sound at a certain volume, so what you could do is have all the sounds in your game play at that global value when needed.
jump.play(VOLUME);
this way, all your switch will do is change the float value of volume.
public static void muteFX(){
VOLUME = 0.0f;
}
public static void normalizeFX(){
VOLUME = 1.0f;
}
It would be in your best interest to not use the Music class for sound effects due to memory constraints.
I hope this helps
In the documentation it states that the id is returned when play() or loop() methods succeed. This might not be convenient depending what are you trying to achieve. Basicly you can get the id and use it with something similar to this:
long id = sound.play(1.0f); // play new sound and keep handle for further manipulation
sound.stop(id); // stops the sound instance immediately
sound.setPitch(id, 2); // increases the pitch to 2x the original pitch
When I wanted to have a button Volume OFF and Volume ON I had a global variable which I would check before I play the sound somethign similar to this:
In my main Game Class:
public static boolean soundEnabled;
Whenever I wanted to play sound.
if (MyMainClass.soundEnabled)
sound.play();
In case you want any other control of the sound then inevetably you need to get the sound's id.
I'm using the Monotouch sample to play Audio file from a remote server (https://github.com/xamarin/monotouch-samples/tree/master/StreamingAudio), and I need to implement the Fast forward and Rewind functionalities.
The StreamingPlayback Class has implements the OutputAudioQueue Play and Pause functions. I have worked with the AVAudioPlayer in Objective-C and could do something like:
-(void) rewind()
{
myPlayer.currentTime -= 10.0;
}
However with the OutputAudioQueue, the CurrentTime is Read-Only, and it looks to me like there is no way to implement the Rewind/Fast Forward with this. Anyone has a solution to this, or a better way to play my remote audio files?
Thanks
I am writing an app that uses the serial port exposed by the SerialPort class in mono. What I have written so far works perfect in windows, however in linux the DataReceived event handler is never entered, so I cannot receive any data form my device. I have declared the event handler as follows:
comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
Basically I am exploring good cross-platform options and this is a deal-breaker. Any advise on how to fix this or what is going on?
Edit-
I should also point out that I have tested the serial port and device on linux with other applications and all appears to be working.
Maybe it has changed lastly, but as far as I know, events are not currently implemented in Mono's serial port. You have to make another thread in any flavour to read data from serial port, which happens in blocking manner. Try it and tell if it worked.
On Antanas Veiverys blog you can find two possible ways to solve it.
(2012) By adjusting the mono source code.
http://antanas.veiverys.com/enabling-serialport-datareceived-event-in-mono/
(2013) By not touching the mono source but solving the issue in a derived class.
http://antanas.veiverys.com/mono-serialport-datareceived-event-workaround-using-a-derived-class/
(2014)
However, I encourage you to read Ben Voigts blog post where he ignores using the DataReceivedEvent and instead used the BaseStream async BeginRead/EndRead functions to read data from the serial port.
http://www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport#comment-840
Implementing and using the given code sample works on both Windows/Unix, so I have tested. And it is more elegant than using the blocking Read() function in a threaded fashion.
mono does not support Event for serialport.
It is shown on mono's website
I have the same problem with SerialPort.DataReceived. Konrad's advice.
using System.IO.Ports;
using System.Threading;
namespace Serial2
{
class MainClass
{
public static void Main(string[] args)
{
Thread writeThread = new Thread(new ThreadStart(WriteThread));
Thread readThread = new Thread(new ThreadStart(ReadThread));
readThread.Start();
Thread.Sleep(200); // TODO: Ugly.
writeThread.Start();
Console.ReadLine();
}
private static void WriteThread()
{ // get port names with dmesg | grep -i tty
SerialPort sp2 = new SerialPort("/dev/ttyS5", 115200, Parity.None, 8, StopBits.One);
sp2.Open();
if(sp2.IsOpen)
Console.WriteLine("WriteThread(), sp2 is open.");
else
Console.WriteLine("WriteThread(), sp2 is open.");
sp2.Write(" This string has been sent over an serial 0-modem cable.\n"); // \n Needed (buffering?).
sp2.Close();
}
private static void ReadThread()
{
SerialPort sp = new SerialPort("/dev/ttyS4", 115200, Parity.None, 8, StopBits.One);
sp.Open();
if(sp.IsOpen)
Console.WriteLine("ReadThread(), sp Opened.");
else
Console.WriteLine("ReadThread(), sp is not open.");
while(true)
{
Thread.Sleep(200);
if(sp.BytesToRead > 0)
{
Console.WriteLine(sp.ReadLine());
}
}
}
}
}
File formats I would like to play include .wav, .mp3, .midi.
I have tried using the Wireless Toolkit classes with no success. I have also tried using the AudioClip class that is part of the Samsung SDK; again with
If this device supports audio/mpeg you should be able to play mp3 use this code inside your midlet...
This works on my nokia symbian phones
// Code starts here put this into midlet run() method
public void run()
{
try
{
InputStream is = getClass().getResourceAsStream("your_audio_file.mp3");
player = Manager.createPlayer(is,"audio/mpeg");
// if "audio/mpeg" doesn't work try "audio/mp3"
player.realize();
player.prefetch();
player.start();
}
catch(Exception e)
{}
}
As for emulators my nokia experience is that I couldn't make it emulate mp3 player but when I put application on phone it works...
Without source code to review, I would suggest using the wireles toolkit (from http://java.sun.com)first.
It contains the standard J2ME emulator for windows and example code that will allow you to play a wav file.
assuming that works OK for you, try the same code on your Samsung device (of course, the location of the wav file will probably change so you have a tiny modification to make in the example code).
Assuming that works, compare your code that doesn't with the example code.