For android, I've researched for controlling Audio using AudioManager. Will there be one for video which I can control the video player to play/stop/pause.
The purpose of AudioManager isn't really to control audio playback, but to control such properties as volume, audio mode, and so on. If there is a media player in the background, that doesn't really mean that you control it, you can only send certain requests to the AudioManager which cause the system either to take some actions regarding the audio, or to send some events, which the audio player may respond to. So you don't have direct control over the other application.
Likewise, there is no system service which will make the video pause. The best equivalent of AudioManager you have which controls what is shown on the screen is WindowManager, but I think it's obvious that it can't be used to make a video player pause.
Your best bet for controlling an external video player would be to look up its intents, it may have some ways to control the playback from outside. This, of course, will be application specific.
There is no such API like VideoManager
You can handle videos through Primary API MediaPlayer and also see this useful article of multiple ways to handle videos
Related
On Android Web Browser (Chrome or other browsers as well), the sound comes out to media sound, not the call sound. (in Demo too)
So, when the user controls the volume up/down buttons, it controls the call sound not the media sound. So, this misleads the users behaviours and it cause a lot of uncomfortable situations. And it's a bit difficult to control it on old android devices. Is there any way to solve this problem?
I tried with appRTC demo(https://appr.tc/). And the sound comes out to the call sound. And I can control the volume with the volume up/down buttons.
Please help me out. Thanks.
There isn't an API method to play audio as a call volume on the Agora Web SDK as of now. Media volume is the intended way of playing media on the browser. If you want to use the call volume you can check out the Android SDK instead.
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.
I'm working on a music app which consists in a Service playing music while other apps are running.
In order to enjoy the music and only the music, the Service mutes all the other apps. In order to do that, it mutes STREAM_MUSIC and play music over STREAM_VOICE_CALL (found that solution to mute other apps here)
As the Service uses STREAM_VOICE_CALL to play music, what I'm trying to find is a way to make the volume buttons control this stream when a sound is playing.
What I already tried:
setVolumeControlStream: only works in Activities
Capturing volume keys pressed events: Services do not receive such events (source)
audioManager.setMode(AudioManager.MODE_IN_CALL) : does not work in my case. Plus, the SDK documentation states that this "mode should only be used by the telephony application when it places a phone call".
At this point, I'm out of options and any help would be appreciated. Thanks!
I would personally not suggest you to STREAM_VOICE_CALL as it is meant to be recommended to use it for Voice calls as compared to media playback. Using this may prevent you from using features exposed by Android for media playback.
Having said this & taking into consideration your use-case, I think you need to look for changing 'call volume' level as compared to 'media volume' level.
Have a look at the MediaPlaybackService.java as used in the stock Music Player app and try to customize it as per your needs.
I havn't tried it myself. but looks like it should work. Kindly try it out.
m_audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
m_audioManager.setMode(AudioManager.MODE_IN_CALL);
The android audio manager has a large number of different audio streams available, including DTMF, SYSTEM, RING etc.
Not that I'm saying its a good idea, but is there any significant disadvantage to playing audio on a stream other than MUSIC? The standard appears to be to play on the MUSIC stream, but if, for example, I want to use the ringer volume, is there any disadvantage to just playing on the RING stream instead?
There are a number of cases where playing on streams other than music offer some advantages in addition to the case I provided above, but I don't want to risk breaking more important functionality if I can help it.
I'd be curious to hear whether anyone has any experience playing on and/or manipulating other streams, and what side affects, if any, they've encountered (or incurred...)
Why do you need it? I think media stream is enough for any of your purposes.
Look, for example you use ring stream. And for example your program controls it volume. He sets "no volume" => it means that he goes into "vibrate mode" and he cant hear incoming rings because of your application
I would like to know if it is possible to embed the default audio player into one of my activities. Or at least be able to embed the section that has the play/pause button and the audio track seek bar.
The reason I would like to embed it rather than open it in a new intent (like this:
Intent audioActivity = new Intent(Intent.ACTION_VIEW);
audioActivity.setDataAndType(Uri.parse(getAudioURL()), "audio/*");
startActivity(audioActivity);
)
is that I want the users to be able to read some content within the current activity while listening to the audio. I have been able to use a progress bar which updates based on the progress in the audio track, but this does not look good and i would rather have the look and feel of the default audio player. I know the default video/audio players are not documented very well but if this is at all possible I would like to know. Thanks!
Hi, I would like to know if it is possible to embed the default audio player into one of my activities.
No, sorry.
First, there is no such thing as "the default audio player" as a single entity. Each device has its own "default audio player".
Second, even if there were a single universal "default audio player", there is no way in Android to embed some other application's UI into your own.
Or at least be able to embed the section that has the play/pause button and the audio track seek bar.
No, sorry. See above.
I think what you're looking for is MediaController. This answer has a good example of how to use this, though I'm not sure if it is possible to have the controls displaying all the time (they disappear after 3 seconds in the example):
How can I show a MediaController while playing audio in Android?
There's another discussion of it here:
How to implement an audio player for Android using MediaPlayer And MediaController?