How to achieve precise audio file play durations with Exoplayer2? - android

I am using Exoplayer2 to play an audio file in a loop. The audio file is 3600ms in length. However, different loops are played with slightly varied durations of 3811ms, 3611ms, 3589ms, 3605ms, 3602ms, 3595ms, etc. I want this to be precisely 3600ms. How do I achieve this?
I am recording the time durations in onPositionDiscontinuity with Player.DISCONTINUITY_REASON_PERIOD_TRANSITION.
I create Exoplayer MediaSource using SingleSampleMediaSource.Factory.
Could it be due to Exoplayer2's attempt to play gapless playback? If so, how do I turn gapless off.

Related

SoundPool - What position in a sound file

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.

Loop audio file without gap

I'm using MediaPlayer on Android to play audio files. Looping on low end devices causes a very clear stutter in the sound at the end of the file. I can imagine a seamless playback by playing another instance of MediaPlayer before the previous one ends with some fade effects. Is there no easier way to achieve seamless looping playback of audio?

Play audio in background without interrupting mediaplayer

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.

Android ExoPlayer : Does it solve gapless / seamless playback issue that is broken for the Android Media Player

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.

Playing two MediaPlayer objects in sync [Android]

I have two media files located locally - mp4 video and m4a audio, which have to be played in sync. I use MediaPlayer objects for this purpose, all start/pause methods are called simultaneously.
Sometimes I see the difference between audio and video right after players start, sometimes after tapping on pause/resume.
I added logs, and see that after pausing media players, their position differs (e.g. MediaPlayer with audio file: 1820ms, MediaPlayer with video file: 1760ms).
One more interesting thing is that seekTo operation with audio file works good, while with video it's really unpredictable.
Please suggest what is the reason of such a behavior and which solutions are available in order to fix that?
I think that this effect with the video timing depends on how the " key frames", which contain a full frame of information have been encoded. It's only possible to locate to one of these keyframes.
Video editing software gets over this by rolling the deltas from intervening i-frames to the exact seek position.

Categories

Resources