How to synchronize sound playback in android? - android

I'm writing my first android app, trying to playback two 10min soundfiles synchronously (imagine an instrumental track and an acapella), to be able to change the volume of each track independently). I am using two MediaPlayers for this, since a SoundPool is targeted at shorter audio samples as far as I read.
Now my problem is that, when pausing and resuming the playback, sometimes the players are not synchronous anymore, even though I set their positions to the same value before resuming playback.
I know that this is kind of inevitable, because they cannot be started at exactly the same moment and they may require different amounts of time for starting playback, but: Is there maybe any other approach to meet my requirements?

You can take a look at JetPlayer, this may accomplish what you want as far as synchronization.
To use it you create audio channels (your instrument channel and vocal channel) as MIDI files in a track and the player can keep them synchronized while allowing you to mute or unmute the different channels as appropriate.
The user guide for creating JET resouces can be found here.

Related

Android: play multiple mp3s simultaneously, with precise sync and independent volume control

I want to create an Android app that plays multiple mp3s simultaneously, with precise sync (less than 1/10 of a second off) and independent volume control. Size of each mp3 could be over 1MB, run time up to several minutes. My understanding is that MediaPlayer will not do the precise sync, and SoundPool can't handle files over 1MB or 5 seconds run time. I am experimenting with superpowered and may end up using that, but I'm wondering if there's anything simpler, given that I don't need any processing (reverb, flange, etc.), which is superpowered's focus.
Also ran across the YouTube video on Android high-performance audio, from Google I/O 2016. Wondering if anyone has any experience with this.
https://www.youtube.com/watch?v=F2ZDp-eNrh4
Superpowered was originally made for my DJ app (DJ Player in the App Store), where precisely syncing multiple tracks is a requirement.
Therefore, syncing multiple mp3s and independent volume control is definitely possible and core to Superpowered. All you need is the SuperpoweredAdvancedAudioPlayer class for this.
The CrossExample project in the SDK has two players playing in sync.
The built-in audio features in Android are highly device and/or build dependent. You can't get a consistent feature set with those. In general, the audio features of Android are not stable. That's why you need a specialized audio library which does everything "inside" your application (so is not a "wrapper" around Android's audio features).
When you are playing compressed files (AAC, MP3, etc) on Android in most situations they are decoded in hardware to save power, except when the output goes to a USB audio interface. The hardware codec accepts data in big chunks (again, to save power). Since it's not possible to issue a command to start playing multiple streams at once, what will often be happening is that one stream will already send a chunk of compressed audio to hardware codec, and it will start playing, while others haven't yet sent their data.
You really need to decode these files in your app and mix the output to produce a single audio stream. Then you will guarantee the desired synchronization. The built-in mixing facilities are mostly intended to allow multiple apps to use the same sound output, they are not designed for multitrack mixing.

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.

Multiple audio players(Java or C++), that can be modified real-time

Looking for some help with audio playback on Android. We have an OpenGL app (Java + C++) and now we want to play sounds effects. Players should allow to modify playback rate and volume while playing.
Might be OpenSL or Audiotrack.
First question? Is there any free or commercial library/wrapper that can do the thing?(might be java or native)
..,if not, I'll explain what we made so far, and problems we experience.
We created MusicPlayer class (extends AsyncTask) with AudioTrack instance. In activity's onResume() we created 5 instances of it, executing it on thread pool. In task's doInBackground() we have a running loop checks states change, load files, and write to buffer. In JNI we have singleton that stores events queue and send them to java once per 10 miliseconds. It somehow works, but is rather far away from being acceptable. We experience following problems:
When file starts to play we can hear short noise on start. Like click or something.
Even if we flush or release AudioTrack it seems the sound plays in queue (especially when need to change buffers quick)
We can't create a loop in MODE_STREAM
When we modify AsyncTask's local variable CHANGE_RATE and RATE it should call audioTrack.setPlaybackRate(RATE). It does, but nothing happens.
I used to write in Obj-C for iOS and there are plenty "ready-to-use" solutions(e.g. cocoacontrols). Never thought dealing with sound on Android would be such a nightmare;/ Any help will be highly appreciated:)
Android 4.0 or higher does not support playback rate control anymore. I'm not sure what was the reason for disabling it. I had to adopt SOLA algorithm implemented by SoundTouch library to add playback rate control to my app.
If you want to use it, you need to write a player using OpenSL. There are a lot of working examples around. Take the algorithms and use it for modifying PCM stream before sending the stream to the output sink.
Update 22.01.2016: Android appears to include Sonic library into 6.0 release. Thus playback rate control should be available starting from Android M.

MediaPlayer -- how to separate a narration track?

I'm working on an android app that plays video (using video view). the video is meant to have both music (left and right) and narration, but I want to selectively be able to turn off the narration track in the MediaPlayer.
Is the way to do this correctly to encode by mp4 video file with 3 audio tracks (right left and narration) and then turn off the naration audio track with deselectTrack()?
Not clear to me from the documentation that MediaPlayer can handle more than 2 audio tracks.
If the audio tracks are limited to 2, would it make sense to run two media player simultaneously (synching them up with seekTo())when I want the narration track to play?
Thanks.
Sorry to burst your bubble, but...
1) You have a misunderstanding about what a "track" denotes. A track can have multiple channels (e.g., a stereo track has left and right channels). As I understand it, stereo is the extent of the Android AudioTrack implementation at present. I haven't yet checked if the OpenSL implementation is more extensive than the Java API.
2) Only 1 audio track can be selected at a time, so you wouldn't be able to have background and narration simultaneously in the way you were thinking.
3) Audio tracks can only be selected in the prepared state (i.e., not after playback has started). The documentation mentions this limitation is not ideal, so it will probably change in the future. If not for this problem, your goal could be accomplished with two audio tracks encoded in the stream, one with both background & narration, the other just background.
You will probably find it difficult to synchronize two MediaPlayers, but I haven't tried. Maybe this approach would be acceptable for your situation, although be forewarned the seekTo method isn't accurate. It depends on the encoding of the files.
Something I would try if I were you is to have two complete encoded videos, one with narration, the other without. Use two MediaPlayers and keep them both prepared. When you want to switch use seekTo to put the correct one at (or near) the desired location. That way you don't have to worry about synchronization. If the video is large, this method could use significantly more resources, though.

Android audio delay - how to calculate delay

i understand that there are some issues why android can't ply low latency audio and has a >100ms delay on everything (well.. actually vibrations are faster as audio!!! Shame on you!).. but is there some possibility to figure out how much earlier i need to run the sound to actually be on time?
e.g. how to calculate audio delay?
Im creating a rhythm game and i need to play "ticks" in sync with music.
Im using libGDX Sound - e.g. sound pool - play() now.
Any suggestions?
Your app could emit a sound with the speaker and then use the microphone to detect the sound emited by itself (something similar to remote.js).
Even though there are many variables involved (the mic will also have a latency), you can make the device calibrate it self by continuously trying to guess how long the sound will take to be detected, and emiting it again and again until your guess gets "fine tuned".

Categories

Resources