Android, Mediaplayer mp3, disturbing sounds - java

I am playing a mp3 file from a local source with MediaPlayer.
The song is played, but there are also weird "scratch" sounds that can be heard.
This is happening with every song.
Also after a short while (depends on song) the player stops playing, this takes from 5 seconds up to 2 minutes.
Error: /MediaPlayer(957): Error (1,-2147483648)
I searched a lot in google.. no solution.
Any idea? Thanks.

If the song doesn't glitch in any other media player, then consider re-encoding using mpg123 for instance. It may come from an encoding problem more than your android programming.

Related

Java - Use music playback control buttons

Usually on keyboards there is buttons to stop the current track, go to the next track, or to the previous one. When I'm playing sound on YouTube for example, and press the F10 key, the video stops.
I'm creating a Java/Kotlin music player. What currently happens when I have music playing, and then I click F10, is that the last YouTube video I have in an open tab starts playing, and the music player does not get effected at all.
What I want is: When I click F10 (or the other control keys), I want my music player to get affected by them, not YouTube.
How can I achieve that?
If I understand correctly, you have an application for playing back sound, but when control keys (e.g., F10 in particular) are pressed, your browser is receiving the key, not your application.
If that is the case, the question seems is about how to use the Swing KeyListener. The KeyEvent API shows that the keycode for the key to be VK_F10. Are you having problems with implementing this? Kotlin might have it's own way of doing key listening, though. I did find this SO question on Kotlin KeyListeners.
As far as starting or stopping a sound, that is usually accomplished by wrapping the audio playback class (Clip or SourceDataLine) in a class that can receive and handle commands to start, stop or continue. Are you having problems with implementing this?
The code that links these two functional areas should be loosely coupled. With that, pursing the two matters as two separate questions will be beneficial.
If your issue is a specific OS implementation question, it would be best to clarify that in the question with additional information.

MediaPlayer always repeats after 1 second

I have an mp3 file that should be played repeatedly.
I simply use this code:
mediaPlayer = MediaPlayer.create(_context, scanner);
mediaPlayer.setLooping(true);
mediaPlayer.start();
I have tested it on Android Studio simulator and it works fine, then I have tested it on my own phone and it works fine as well BUT when I connect my phone with a speaker device over Bluetooth, the sound will be played well at the first time but when it's getting replayed, it always starts after about 1 second. The duration of my mp3 file is 4 seconds. So there is always 1 second that is missing.
I have no idea what the reason for that issue could be. Is it because of my code? My speaker device? Bluetooth connection? What is it?
This may solve your issue its a bit roundabout because it creates a new MediaPlayer per loop. Not able to achieve Gapless audio looping so far on Android Its seems like this issue has been a thing for a while. If you don't want to do this you may want to explore using another library instead of MediaPlayer

Java Clip Not Rewinding Properly?

I'm not even sure if the problem is that it's not rewinding properly. I have a Sounds enumerator/class which lets me load all sound effects and then play them at will, and in my game, I initialize them and then each time I want to play one, I play it in its own thread so it doesn't freeze the rest of the game.
The problem is that the first time I hold down or click the mouse to shoot (and thus, play the sound), it plays the sound, even repeatedly, as it's supposed to. But after I release the mouse button and try again, the sound no longer plays.
Does anyone know what the cause of this could be? Here are some code snippets showing the Sounds class and the piece of my code where I am playing the sound.
Sounds class: http://pastebin.com/Hqt5dPQ6
Playing the sound: http://pastebin.com/Lex9ZmzJ
I appreciate any help, as I all but dropped this project a couple of months ago because I wasn't making any progress due to this one stupid problem.

Android Media Player Threading/Concurrency

I am using the default Android Media Player in an Activity, trying to play back a couple of video files. However I have a problem with programming what I want. The desired program outcome would be the following:
A video file is played back
After that, a dialog is shown, asking the user something
A pause of 5 seconds occurs
The next video is shown
and so forth
How am I to program this? Currently, I use a paradigm like the following:
I have a method that sets up the player for theĀ a file, e.g. playVideo(int) So I pass it the first file.
When the player is prepared, it will be started in onPrepared(MediaPlayer).
When the video is finished, the onCompletion(MediaPlayer) listener of the media player shows the dialog to the user by calling showDialog().
The user accepts the dialog. Before calling dismiss() on the dialog, the player object is started for the next file by calling playVideo(int).
This works, but it feels a bit quirky and not clean. And it's of course very procedural. The problems are:
that I can't figure out how to insert a pause after the dialog is dismissed.
that I maybe want to change the presentation order of dialogs and videos and this ain't too easy now
Has anyone got an idea?
For the pause, you could use the AlarmManager to schedule an alarm five seconds from now. It will launch an intent, and that intent could call playVideo(int).

PulpCore music playback - loop sound and animate volume

I have been experimenting with PulpCore, trying to create my own tower defence game (not-playable yet), and I am enjoying it very much I ran into a problem that I can't quite figure out. I extended PulpCore with the JOrbis thing to allow OGG files to be played. Works fine. However, pulpCore seems to have a problem with looping the sound WHILE animating the volume level. I tried this with wav file too, to make sure it isn't jOrbis that breaks it. The code is like this:
Sound bgMusic = Sound.load("music/music.ogg");
Playback musicPlayback;
...
musicVolume = new Fixed(0.75);
musicPlayback = bgMusic.loop(musicVolume);
//TODO figure out why it's NOT looping when volume is animated
// musicVolume.animate(0, musicVolume.get(), FADE_IN_TIME);
This code, for as long as the last line is commented out, plays the music.ogg again and again in an endless loop (which I can stop by calling stop on the Playback object returned from loop(). However, I would like the music to fade in smoothly, so following the advice of the PulpCore API docs, I added the last line which will create the fade-in but the music will only play once and then stop. I wonder why is that? Here is a bit of the documentation:
Playback
pulpcore.sound.Sound.loop(Fixed level)
Loops this sound clip with the
specified volume level (0.0 to 1.0).
The level may have a property
animation attached.
Parameters: level
Returns: a Playback object for this
unique sound playback (one Sound can
have many simultaneous Playback
objects) or null if the sound could
not be played.
So what could be the problem? I repeat, with the last line, the sound fades in but doesn't loop, without it it loops but starts with the specified 0.75 volume level.
Why can't I animate the volume of the looped music playback? What am I doing wrong? Anyone has any experience with pulpCore and has come across this problem? Anyone could please download PulpCore and try to loop music which fades-in (out)?
note: I need to keep a reference to the Playback object returned so I can kill music later.
If the animate method only sets the option, it can work unpredictably - try switching this line with the loop itself, so the animation applies first.
Can you animate the volume on an unlooped playback, and then, at the end of that playback, start the loop at the fixed level?
Finally I managed to get an explanation and a simple work-around for this issue from the pulp core author. So here it is:
It is a PulpCore bug. When the output
volume is zero, the sound player stops
looping the sound.
To work around it, animate from a
value that is not zero, like this:
musicVolume.animate(0.0001, 1, FADE_IN_TIME);
Link to this on pulpcore Google groups

Categories

Resources