I'm using ExoPlayer in my Android app to play few tutorial videos, and when I pause the video the audio focus gets abandoned and the user is not able to control the media volume via the physical buttons of the device (because the audio focus has been lost the buttons now control the ringtone volume) and I want to keep the audio focus even if the user pauses the video, and I only want it to be lost if the user exits out of the tutorial.
I already tried setting the "exoPlayer.setAudioAttributes" to null which suppose to prevent exoplayer from using its default audio focus settings but when that option is set to null ExoPlayer won't play at all... (I also tried to set to it an actual AudioAttributes like in this post: https://medium.com/google-exoplayer/easy-audio-focus-with-exoplayer-a2dcbbe4640e
but it didn't have any effect)
I also tried setting up AudioManager (the build-in one) and request audio focus but immediately everytime I set it its listener returns "AUDIOFOCUS_LOSS" (I tried setting it before setting ExoPlayer and after, and also I tried setting it again everytime the state of ExoPlayer changes, but every time it returned "AUDIOFOCUS_LOSS" right away.
Here's my code:
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.CONTENT_TYPE_MOVIE)
.build();
exoPlayer.setAudioAttributes(audioAttributes, true);
You have to use setAudioAttributes for focusing the Audio. Here is the code that works for my project:
val audioAttributes: AudioAttributes = AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.CONTENT_TYPE_MOVIE)
.build()
simpleExoPlayer.setAudioAttributes(audioAttributes, true)
If you want to know in detail you can read this article.
The problem was that I was not setting the stream type for the activity and that's why the hardware buttons weren't controling the music volume when the video was paused (Credit goes to a reddit user for posting this solution over there)
activity.setVolumeControlStream(AudioManager.{INSERT YOUR STREAM TYPE})
Android docs:
https://developer.android.com/reference/android/app/Activity.html#setVolumeControlStream(int)
Related
I have to create an app, which detects user inactivity, and then start activity which displays some videos with WebView, and then when displaying with WebView is finished, it has to play videos from SDCard. I've already handled part with WebView and SDCard (with JavaScriptInterface etc.)
This application has to work with API 19 all the way to the newest one.
The question is - Is there a possibility to detect if user is inactive and start my application, or keep the app running in background, and then start activity in the foreground after the user becomes inactive for certain time?
I'm not trying to play ads, when user is not looking at his screen. Application is for my client, who have stores with all kind of electrical equipments, including smartphones. The goal is to play video presentations with hardware details specific for each smartphone (informations about processor, ram, camera, screen etc.).
In short: I have to make an app which is similar to "Demo Apps" created for example by Samsung (playing some kind of presentations on screen).
So far I've read and tested things like:
1) BroadcastReceiver with combination of ACTION_SCREEN_OFF / ACTION_SCREEN_ON events.
Receiver works properly, I can detect this event and then start activity, but... The screen is already off so i can't see the displayed activity - it's visible running in the foreground after unlocking the phone. Is there a way to unlock the phone when the event is received?
That's my code so far.
EventReceiver Class:
class EventReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
StringBuilder().apply {
append("Action: ${intent.action}\n")
append("URI: ${intent.toUri(Intent.URI_INTENT_SCHEME)}\n")
toString().also { log ->
Log.d(TAG, log)
Toast.makeText(context, log, Toast.LENGTH_LONG).show()
}
}
if (intent.action == Intent.ACTION_SCREEN_OFF) {
val i = Intent(context, MainActivity::class.java)
context.startActivity(i)
}
}
}
MainActivity Class:
val br : BroadcastReceiver = EventReceiver()
val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION).apply {
addAction(Intent.ACTION_SCREEN_OFF)
addAction(Intent.ACTION_SCREEN_ON)
addAction(Intent.ACTION_BOOT_COMPLETED)
}
2) Foreground Services - I read that this is a great way to make some asyc stuff in the background and show notifications to user. Is there a way to start the activity with it?
3) Job Scheduler
4) Daydream / Dream Service - it actually works great with almost every API and manufacturer, but.. there's no way to set the app as Screen Saver on Huawei/Honor smartphones, at least from phone settings, I've read that this is possible with ADB etc. but this is not an option that I can use here.
It seems that none of these fullfill my expectations.
In HTML5WebView.java (pastebin link)(source) I have:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("TAG - BACK PRESSED IN WEB VIEW");
return super.onKeyDown(keyCode, event);
}
And in the activity that starts the web view I have:
#Override
public void onBackPressed() {
System.out.println("TAG - BACK PRESSED IN WEB PLAYER ACTIVITY");
super.onBackPressed();
}
Now when I'm playing an embedded youtube video normally (not fullscreen), both methods are called when I press the back button. When I put the video into fullscreen mode (using the youtube player fullscreen button), none of the methods are called. My only guess is, the back button is being consumed by the web view to undo the fullscreen action (but even that doesn't work).
I am trying to get the back button to immediatly kill the web view, even if there is a video in fullscreen mode.
See the problem in action here, had to post it externally because the gif > 2MB
If I was you I would delete the embed play of youtube video's or ask google that won't be seen as abuse. Because the embed play of youtube video's can be seen as a abuse of this:
Your app violates our Device and Network Abuse policy by downloading,
monetizing, or otherwise accessing YouTube videos in violation of the
YouTube Terms of Service or YouTube API Terms of Service.
More Info: https://play.google.com/about/privacy-security/device-network-abuse/ https://www.youtube.com/static?template=terms
For example, your app contains: YouTube background play functionality
This is a violation of the YouTube Terms of Service.
That is what I got in my experience because I also made a app where people could watch some YT embed video's but after some updates the app wouldn't be update becuase I got this error that it is abuse so I am warning you that's all.
I'm working in a notification app for Android and my customer has requested me to insert a video inside the notification preview.
I know that I'm able to create notifications with pictures and notifications with custom views, but when I try to add a video to the custom view my app gives me always this error:
FATAL EXCEPTION: main
Bad notification posted from package com.codeversed.example.Notifications: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.codeversed.example.Notifications user=UserHandle{0} id=0 tag=null score=0 key=0|com.codeversed.example.Notifications|0|null|10008: Notification(pri=0 contentView=com.codeversed.example.Notifications/0x1090089 vibrate=default sound=default defaults=0x7 flags=0x18 color=0x00000000 originalPackageName=N originalUserId=0 vis=PRIVATE))
I suppose that is it because videos aren't allowed to remote views. Does somebody know if this limitation can be sorted or if there is a way to show videos on notifications?
Regards,
No, you cannot show a video in a Notification. Partially, that is because RemoteViews does not support a SurfaceView, TextureView, or VideoView that you would need. Partially, that is because the party responsible for showing the video may not have rights to do so (e.g., may not have Internet access). Remember that you are not the one actually showing the Notification — somebody else is. You merely provide the specification.
i use this method to record video from front camera:
Recording video via Mediarecorder
it works fine in my Nexus 4 but some people says that there is a lot of phones that their front camera cant record video and they only can take pictures. My Android App functionality based on recording video from front camera and my question is that is this true that some phones can't record video via front camera? and how i can detect this and inform user?
Try calling some code like this
CameraInfo cameraInfo = new CameraInfo();
if (cameraInfo.facing = CameraInfo.CAMERA_FACING_FRONT) {
//do your code?
} else {
//alert the user via toast or dialog
}
no built in way to figure it out though.
EDIT:
should work on API 9 and above.
maybe try calling these methods to first get a camera object, then check to see if there is a camcorderProfile available for the front facing camera?
hasProfile (int cameraId, int quality)
setCamera(camera);
I want to disable ALL sound and vibration from the android device. As per the answers to similar questions, I'm currently using the following code to mute all the audio streams, set the ringer mode to silent, and to fake a voice call scenario:
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
//DOESNT DISABLE ALARM CLOCK
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
amanager.setStreamMute(AudioManager.STREAM_RING, true);
amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
amanager.setStreamMute(AudioManager.STREAM_DTMF, true);
amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
amanager.setStreamMute(AudioManager.STREAM_VOICE_CALL, true);
//disables vibrate and sound of ringer
amanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//fakes voice call...changes alarm to single tone+vibrate
amanager.setMode(AudioManager.MODE_IN_CALL);
amanager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
This works for disabling music and incoming calls, but, as noted in the comments, the android's built-in alarm clock app is still able to produce sound and vibration.
Does anyone know how to truly disable ALL sound and vibrations? Or venture a guess as to why the alarm clock app seems to by bypassing the audio streams?
setStreamMute did not work for me to mute the alarm, but using setStreamVolume for STREAM_ALARM setting volume to zero did. It is necessary to restore the alarm volume afterwards.
For example like this:
AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
manager.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);