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
Related
I would like to show a notification and play a sound when the user taps onto that notification. It works somehow when I use an activity to play the sound, as I have written in my own answer to this question: How can I create a notification that plays an audio file when being tapped? (in this question and answer there is also the source code showing how I create the notification and how my PlaySoundActivity looks like.
Yet, I have realized, that while the sound is playing, the appearance of my main application changes and it will not be restored without closing the application.
I have created my application from the "Tabbed Activity" project template.
This is how it looks after being started:
And this is how it looks when I have tapped onto the sound notification (the sections are gone):
Can anyone explain why this happens? Is it a wrong approach to play sound using an activity? But it does not work here when I use a service, I hear nothing! How to solve that?
According to the Android developer reference, "almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View)".
I had not done this. Therefore a new window was displayed, but there was no content.
The better solution for playing a sound is to use a PlaySoundService (service!!!) instead of an activity. It contains almost the same code as an activity would do, but the pending intent is created with PendingIntent.getService(...) instead of PendingIntent.getActivity(...). Using the wrong method does not result in an obvious error message, but the service won't work as expected.
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.
I have an array of times that correspond to events in a recorded video. These times inflate a list. When a list entry is selected, I want the video to open to the time selected
Right now, the following code only opens the video at the beginning of the video.
Uri video = Uri.parse(Environment.getExternalStorageDirectory()+"/recordedMedia/" +eventName+ ".mp4");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.setDataAndType(video, "video/*");
startActivity(intent);
Ideally directly at the specified time would be best, although starting at the beginning, then skipping to the specified time, would work too, if it is fairly quick.
I'm working with Android 2.2, API 8
Maybe you could use the seekTo() method from the MediaPlayer object?
I haven't tested this and you'll need to add in exception handling, but you could go for something along these lines:
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(Environment.getExternalStorageDirectory()+"/recordedMedia/" +eventName+ ".mp4");
mp.prepare();
mp.seekTo(/*a time*/);
I would recommend using the MediaPlayer class in your own code. The way you've done it will use the default player for the media type or one of the user's choosing. Even if you could figure out how to make that particular media player start from a certain position, you have no chance of getting it to work with the wide variety of players in use, including some devices where the Android defaults have been replaced with OEM variants.
Create your own activity using a MediaPlayer and use seekTo() to start from your desired position. There are loads of examples on the web.
Cheers
I have a problem with online radio streaming on Android (2.1). I'm using MediaPlayer like that:
player.setDataSource(mStationUrl);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.prepareAsync();
The problem is that after run this code I can hear a second of the stream, then streaming is breaking for a few seconds, and next it runs again - this time properly.
So - how can I avoid this silent gap, or maybe I'm doing something wrong with player preparation?
I've done this with a few streams - always with the same results.
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