I am creating an app which can play my radio stream. I am using MediaPlayer, but it is not enough. For example, buffering time is unpredictable, buffering time is not available. I would like to use an other mediaplayer, which is fast, and e.g. I can set the buffering time. Is there any open source solution?
Related
I'm trying to use Android's MediaPlayer to play a backing audio track to a music game which should stay in sync with other events.
However, I'm finding that on different devices the backing track seems out of sync.
After some frustration, I'm coming to the conclusion that the time it takes to start playing the audio is indeterminate.
Is there a way around this? Or some kind of callback from the MediaPlayer that represents "starting to play NOW"?
AFAICT I can't use the SoundPool for this backing track as the audio file is too long.
I have loaded sounds using Soundpool in Android.
I have used the play function to play the sound.
How do I know how long a file is and where I am currently in that file as it is playing?
Sound File Duration
MediaPlayer is probably your best bet, here:
MediaPlayer player = MediaPlayer.create(ctxt, R.raw.mysound);
int duration = player.getDuration();
player.release(); //(If not using this player later on)
Current Playback Position
SoundPool does not support this functionality. Your three main options:
Use MediaPlayer for playback. MediaPlayer supplies a getCurrentPosition() method.
Use AudioTrack -- manually sending uncompressed audio to the stream on a byte-by-byte basis. AudioTrack has some basic functionality for determining playback position (usually accurate to within a few tens of milliseconds, on most devices), and besides that you can know exactly how much audio's been added to the playback buffer.
Stick with SoundPool, but time the playback yourself. That is, take a time hack (SystemClock.UptimeMillis()) or set a CountDownTimer, and estimate your playback position by assuming that playback began as soon as play() returned. This approach is rife with problems and inaccuracies, but has the benefit of not involving AudioTrack's complexity.
I want to restrict buffering after pausing a video in android. When I pause the video player, the video stops but buffering does not. It continues to consume data. I want to stop data to be consumed.
Thanks in advance.
With a standard mp4/webm, you can’t. It’s totally up the the browser. If you use fragmented mp4 and feed the buffer via MSE and Ajax, you can do whatever you want. But it’s a thousand time more work on your part.
You can't skip buffering it depends on the network
How can I play background audio, in Android, without interrupting the MediaPlayer playback, by either using MediaPlayer (preferred) or OpenSL ES?
I know SoundPool is able to play sound effects without interrupting any MediaPlayer playback, but the size is limited to 1M per effect, which is way to less. Not requesting audio focus, via AudioManager doesn't seem to work either, audio doesn't play at all in this case.
And in the case of OpenSL ES, all audio generally stops when I start to play a longer asset file. It's similar to the behaviour of SoundPool described above.
Edit from the comments:
I don't want to interrupt other music players, it's the background
audio of a game, which shall play without interrupting the, for
example, music of a player. Games like Subway Surfer, Clash Royale and
such seem to have this achieved somehow, but I could not achieve it
via OpenSL ES, or MediaPlayer.
In order to play sound in background you can use SoundPool, AudioTracks and OpenSlES.
Soundpool: Use small files and make a sequence. In my last project i use 148 sound files (all small) in different scenarios and played using soundpool. Make a list and play one by one or in parallel. Also in games usually you have a small set of sound for particular scenario and it loops. Best is soundpool for that. You can also perform some effects like rate change. Also ogg files are way small, so use them.
AudioTrack: You can use RAW PCM data in audio track. If you want you can extract pcm data using MediaExtractor from almost all formats and use it. But it will be a little work for you in your case, but it is really good (supports huge data, even live streaming).
OpenSLES: Apparently android uses opensles in background for all its purpose. So using it will help you a lot. But it's not easy to get everything done on it. You need to learn more for lesser work.
I have been deeply working on OpenSlES for about 20 days and still i will say for small purpose use soundpool, medium to high level implementation use AudioTracks and for kickass implementation use OpenSLES.
PS: It will be bad effect on your user if you play game sound in background while they are playing their music or their call. Personal experience.
Has anyone tried using ExoPlayer to achieve this?
I tried looking online with no success.
When I say gapless playback, I am referring to the problem of using the media player to play local videos back to back. After the first video is done playing, there is a noticeable delay of 1 second before the second video starts.
Hoping this question helps in understanding this issue further.
For reference please look at the following question:
Android: MediaPlayer gapless or seamless Video Playing
ExoPlayer 2, which is now officially released, seems to support gapless playback using the ConcatenatingMediaSource class. From its developer guide:
Transitions between sources are seamless. There is no requirement that the sources being concatenated are of the same format (e.g. it’s fine to concatenate a video file containing 480p H264 with one that contains 720p VP9). The sources may even be of different types (e.g. it’s fine to concatenate a video with an audio only stream).
And the example code:
MediaSource firstSource = new ExtractorMediaSource(firstVideoUri, ...);
MediaSource secondSource = new ExtractorMediaSource(secondVideoUri, ...);
// Plays the first video, then the second video.
ConcatenatingMediaSource concatenatedSource =
new ConcatenatingMediaSource(firstSource, secondSource);
EDIT: ExoPlayer 2 supports gapless playback, but as of the time of writing is still unreleased as a stable version.
You will most likely never be able to achieve perfect gapless playback of multiple tracks with ExoPlayer or Android Media Player. Neither have been written to support starting multiple tracks and I imagine it will stay out of scope for both of them.
You can achieve gapless playback by using 2 different player instances, once you have started and played the first, you can load the second and start playback once the first finishes. Using this method you could have a gapless solution, as long as you prepare the second video during playback of the first.
To take it further, you can also use 2 different surface textures for rendering the multiple videos, once the first video reaches the end you could fade out the texture and fade in the new one. Resulting in a nice seamless video effect.
Because of the nature of playing multiple videos at once you will most likely want to create your own timer for incrementing the time and deciding when to switch to the next video, rather than trying to use the callbacks from ExoPlayer or Android Media. This will allow you to keep track of the time in a more accurate fashion, without needing to keep talking to multiple video codecs.
I know this is not the answer you've been looking for, but it's the only reasonable answer. The sole way to ensure no gaps in playback is to download the entire file first and begin playback when it's done. Otherwise, in the event that you lose connectivity before the file is finished downloading, pausing is inescapable.
I just tried switching to ExoPlayer from the standard MediaPlayer implementation and the gap is the same if not worse. However I have used a very simple method of restarting the player when the status changes to ended. I don't know if there's a better proper way to do it, perhaps with 2 different ExoPlayers.