I have implemented media resumption to show recent tracks after phone restart.
According to dev blog After tapping play button "static media controls will be swapped with the media controls created from your notification" but for me it is not swapped and I have static media control notification and new media notification created by that.
What could be wrong. How the system know what notification should be swapped?
My code:
public BrowserRoot onGetRoot(#NonNull String clientPackageName, int clientUid,
#Nullable Bundle rootHints) {
//ANDROID 11 playback resumption - https://developer.android.com/guide/topics/media/media-controls#java
if (rootHints != null) {
if (rootHints.getBoolean(BrowserRoot.EXTRA_RECENT)) {
// Return a tree with a single playable media item for resumption.
Bundle extras = new Bundle();
extras.putBoolean(BrowserRoot.EXTRA_RECENT, true);
KLog.d(clientPackageName + " -> onGetRoot BrowserRoot.EXTRA_RECENT");
return new BrowserRoot(MEDIA_ID_RECENT, extras);
}
}
return new BrowserRoot(MEDIA_ID_ROOT, null);
}
onPlay:
#Override
public void onPlay() {
super.onPlay();
CommonOperations.crashLog("mediaSessionCallback onPlay");
KLog.d("mediaSessionCallback onPlay");
fakeStartForeground();
if (realm == null || realm.isClosed()) {
initRealm();
}
if (playlist != null && currentEpisode != null) {
play();
} else {
List<Episode> unfinished = UserDataManager.getInstance(URLPlayerService.this)
.getUnfinishedEpisodesData();
if (unfinished != null && unfinished.size() > 0) {
EpisodePlaylist list = new EpisodePlaylist(unfinished);
URLPlayerService.startActionSetPlaylist(URLPlayerService.this, list, 0, true);
} else {
KLog.w("stopself");
if (!wasForegroudStart) {
fakeStartForeground();
}
CommonOperations
.crashLog("stopself #" + new Exception().getStackTrace()[0].getLineNumber());
stopSelf();
cancelNotification();
}
}
}
I think I have fixed my issue. There is probably some bud in Android 11 and when I used startForeground with not MediaStyle notification before using MediaStyle notification the problem occurs very often. Even without it I get double notifications from time to time.
I have ended up with using PlayerNotificationManager from ExoPlayer extension.
Related
I made an OCR application that makes a screenshot using Android mediaprojection and processes the text in this image. This is working fine, except on Android 9+. When mediaprojeciton is starting there is always a window popping up warning about sensitive data that could be recorded, and a button to cancel or start recording. How can I achieve that this window will only be showed once?
I tried preventing it from popping up by creating two extra private static variables to store intent and resultdata of mediaprojection, and reusing it if its not null. But it did not work (read about this method in another post).
// initializing MP
mProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
// Starting MediaProjection
private void startProjection() {
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
}
// OnActivityResult
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
if (requestCode == 100) {
if(mProjectionManager == null) {
cancelEverything();
return;
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(mProjectionManager != null)
sMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
else
cancelEverything();
if (sMediaProjection != null) {
File externalFilesDir = getExternalFilesDir(null);
if (externalFilesDir != null) {
STORE_DIRECTORY = externalFilesDir.getAbsolutePath() + "/screenshots/";
File storeDirectory = new File(STORE_DIRECTORY);
if (!storeDirectory.exists()) {
boolean success = storeDirectory.mkdirs();
if (!success) {
Log.e(TAG, "failed to create file storage directory.");
return;
}
}
} else {
Log.e(TAG, "failed to create file storage directory, getExternalFilesDir is null.");
return;
}
// display metrics
DisplayMetrics metrics = getResources().getDisplayMetrics();
mDensity = metrics.densityDpi;
mDisplay = getWindowManager().getDefaultDisplay();
// create virtual display depending on device width / height
createVirtualDisplay();
// register orientation change callback
mOrientationChangeCallback = new OrientationChangeCallback(getApplicationContext());
if (mOrientationChangeCallback.canDetectOrientation()) {
mOrientationChangeCallback.enable();
}
// register media projection stop callback
sMediaProjection.registerCallback(new MediaProjectionStopCallback(), mHandler);
}
}
}, 2000);
}
}
My code is working fine on Android versions below Android 9. On older android versions I can choose to keep that decision to grant recording permission, and it will never show up again. So what can I do in Android 9?
Thanks in advance, I'm happy for every idea you have :)
Well the problem was that I was calling
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
every time, which is not necessary (createScreenCaptureIntent() leads to the dialog window which requests user interaction)
My solution makes the dialog appear only once (if application was closed it will ask for permission one time again).
All I had to do was making addiotional private static variables of type Intent and int.
private static Intent staticIntentData;
private static int staticResultCode;
On Activity result I assign those variables with the passed result code and intent:
if(staticResultCode == 0 && staticIntentData == null) {
sMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
staticIntentData = data;
staticResultCode = resultCode;
} else {
sMediaProjection = mProjectionManager.getMediaProjection(staticResultCode, staticIntentData)};
}
Every time I call my startprojection method, I will check if they are null:
if(staticIntentData == null)
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
else
captureScreen();
If null it will request permission, if not it will start the projection with the static intent data and static int resultcode, so it is not needed to ask for that permission again, just reuse what you get in activity result.
sMediaProjection = mProjectionManager.getMediaProjection(staticResultCode, staticIntentData);
Simple as that! Now it will only showing one single time each time you use the app. I guess thats what Google wants, because theres no keep decision checkbox in that dialog like in previous android versions.
I can create games with invites, I can invite and accept invitations, the thing mostly works fine. The problem appears when I make a game for 3 or more people:
user A creates game with 3 people or 2 people at least
user A chooses to invite someone and then adds another person to auto-pick for the room
User A invites user B
User B accepts
They both wait until the "Start now" button appears
User B presses the start now button and OnRoomConnected() is called 3 times for some reason and the game doesn't start (the room was also never left as far as I can tell, because this user can't receive invitations anymore)
Nothing changes from the perspective of user A, he still waits for the game to start or search for another auto pick opponent
I made sure that the problem is not from my code. I created a separate simple project, that I used only for testing purposes and it does exactly the same thing. So I was starting to think maybe it's not a problem from my side and I didn't see similar problems on the internet. So I decided to ask here. What should I do? What could be the problem?
That's basically it. Even if I restrict the number of players to 3 or 4 (min and max number of players are equal, 3 or 4), it still lets me start the game prematurely and I have the same problem with OnRoomConnected() being called multiple times and the game doesn't start.
Thanks in advance. If you have a link or something that would help me solve this problem, it would be greatly appreciated.
Here's the basic code I used for logging in the game and creating a room.
public class GPGM : MonoBehaviour, RealTimeMultiplayerListener{
public static GPGM instance;
public static int target;
private void Awake()
{
target = 60;
QualitySettings.vSyncCount = 0;
Application.targetFrameRate = target;
if (instance == null)
{
DontDestroyOnLoad(gameObject);
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
}
}
// Use this for initialization
void Start () {
Login();
}
// Update is called once per frame
void Update () {
}
public void Login()
{
StartCoroutine(checkInternetConnection((isConnected) =>
{
LoginGPG();
}));
}
IEnumerator checkInternetConnection(Action<bool> action)
{
WWW www;
www = new WWW("http://google.com");
yield return www;
if (!String.IsNullOrEmpty(www.error))
{
Debug.Log("DebugM | no internet connection");
action(false);
}
else
{
Debug.Log("DebugM | There IS connection");
action(true);
}
}
public void LoginGPG()
{
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().WithInvitationDelegate(OnInvitationReceived).Build();
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.DebugLogEnabled = true;
PlayGamesPlatform.Activate();
Debug.Log("DebugM | LoginGPG");
Auth();
}
public void Auth()
{
Debug.Log("DebugM | Auth");
try
{
//doesn't work sometimes for some reason. It gives null data if success is false
//reason for false success is unknown
Social.localUser.Authenticate((bool succes) =>
{
if (succes)
{
Debug.Log("DebugM | Logged in");
}else
{
Debug.Log("DebugM | authentication failed");
}
});
}
catch (Exception e)
{
Debug.Log("DebugM | Auth() has failed with error: " + e.Message);
}
}
public void OnInvitationReceived(Invitation invitation, bool shouldAutoAccept)
{
StartCoroutine(InvitationCo(invitation, shouldAutoAccept));
}
Invitation mIncomingInvitation;
IEnumerator InvitationCo(Invitation invitation, bool shouldAutoAccept)
{
yield return new WaitUntil(() => SceneManager.GetActiveScene().name == "Lobby");
Debug.Log("DebugM | Invitation has been received!!!");
//StartCoroutine(LM.LoadingAnim());
if (shouldAutoAccept)
{
Debug.Log("DebugM | Should auto accept: TRUE");
PlayGamesPlatform.Instance.RealTime.AcceptInvitation(invitation.InvitationId, instance);
}
else
{
// The user has not yet indicated that they want to accept this invitation.
// We should *not* automatically accept it. Rather we store it and
// display an in-game popup:
Debug.Log("DebugM | Should auto accept: FALSE");
Lobby LM = FindObjectOfType<Lobby>();
LM.invPanel.SetActive(true);
mIncomingInvitation = invitation;
}
}
public void AcceptGoogleInv(GameObject panel)
{
if (mIncomingInvitation != null)
{
// show the popup
//string who = (mIncomingInvitation.Inviter != null &&
// mIncomingInvitation.Inviter.DisplayName != null) ?
// mIncomingInvitation.Inviter.DisplayName : "Someone";
Debug.Log("DebugM | Invitation has been accepted");
PlayGamesPlatform.Instance.RealTime.AcceptInvitation(mIncomingInvitation.InvitationId, instance);
panel.SetActive(false);
}
}
public void CreateQuickRoom()
{
PlayGamesPlatform.Instance.RealTime.CreateWithInvitationScreen(1, 3, 1, instance );
}
public void OnRoomSetupProgress(float percent)
{
Debug.Log("OnRoomSetupProgress()");
PlayGamesPlatform.Instance.RealTime.ShowWaitingRoomUI();
}
public void OnRoomConnected(bool success)
{
SceneManager.LoadScene("Game");
Debug.Log("DebugM | Room conected");
}
public void OnLeftRoom()
{
throw new NotImplementedException();
}
public void OnParticipantLeft(Participant participant)
{
throw new NotImplementedException();
}
public void OnPeersConnected(string[] participantIds)
{
throw new NotImplementedException();
}
public void OnPeersDisconnected(string[] participantIds)
{
throw new NotImplementedException();
}
public void OnRealTimeMessageReceived(bool isReliable, string senderId, byte[] data)
{
throw new NotImplementedException();
}}
Edit (pictures):
This is when I wait for the last auto pick slot to fill (it works like this only when people were invited to the game)
The game goes to lobby for the person who pressed start and the others still wait for the last autopick even if 1 player practically left the room
You can do it like:
public void OnRoomConnected (bool success)
{
if (success)
{
//Start the game here
SceneManager.LoadScene("Game");
Debug.Log("DebugM | Room conected");
}
else
{
//Do somthing else.
}
}
or the best way to do it by checking connected participans count.
public void OnPeersConnected (string[] participantIds)
{
List<Participant> playerscount = PlayGamesPlatform.Instance.RealTime.GetConnectedParticipants();
if (playerscount != null && playerscount.Count > 1)//this condition should be decided by you.
{
//Start the game here
SceneManager.LoadScene("Game");
}
}
I'm working on PJSIP Android app and facing a problem with call hold. While the caller is call to the receiver when caller is put call on hold, receiver how can identify is remote server call on hold? Which event is occurs in receiver hand?
According to pjsip docs:
virtual void onCallMediaState(OnCallMediaStateParam &prm)
Notify application when media state in the call has changed.
Normal application would need to implement this callback, e.g. to connect the call’s media to sound device.
This method is in Call class in pjsip java:
CallMediaInfo class contains pjsua_call_media_status.
Using pjsua_call_media_status we can know whether current call media status is active or is on call hold.
#Override
public void onCallMediaState(OnCallMediaStateParam prm)
{
CallInfo ci;
try {
ci = getInfo();
} catch (Exception e) {
return;
}
CallMediaInfoVector cmiv = ci.getMedia();
CallMediaInfo callMediaInfo = null;
for (int i = 0; i < cmiv.size(); i++) {
CallMediaInfo cmi = cmiv.get(i);
if (cmi.getType() == pjmedia_type.PJMEDIA_TYPE_AUDIO &&
(cmi.getStatus() ==
pjsua_call_media_status.PJSUA_CALL_MEDIA_REMOTE_HOLD ||
cmi.getStatus() ==
pjsua_call_media_status.PJSUA_CALL_MEDIA_LOCAL_HOLD) )
{
//set ur call status as hold on your TextView
}else if (cmi.getType() == pjmedia_type.PJMEDIA_TYPE_AUDIO &&
(cmi.getStatus() ==
pjsua_call_media_status.PJSUA_CALL_MEDIA_ACTIVE))
{
//set ur call status as connected on your TextView
}
}
}
I am trying to implement an AccessibilityService that records the user's actions (only click events at this point) and stores them, such that they can be replayed at a later point in time.
For this I register for AccessibilityEvent.TYPE_VIEW_CLICKED events and check if I could somehow recover them later on (when all I have is a reference to the window / root node of the activity) by using two strategies:
Get the clicked view's id and look for this id in the root node's tree
Get the clicked view's text and look for this text in the root node's tree
I have tested this in various applications and different parts of the Android system and the results have been very confusing. About half of the views were not recoverable by any of the two strategies, and some views were sometimes reported as being recoverable and sometimes not. I found out that the latter was due to a race condition, since the accessibility service runs in a different process than the application to which the clicked views belong.
My question now is whether there is a better way to get a handle to a view in an accessibility and find this view again in a later execution of the application.
Below you find the code of my AccessiblityService class:
public class RecorderService extends AccessibilityService {
private static final String TAG = "RecorderService";
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
switch (event.getEventType()) {
case AccessibilityEvent.TYPE_VIEW_CLICKED:
AccessibilityNodeInfo node = event.getSource();
if (node == null) {
Log.i(TAG, "node is null");
return;
}
AccessibilityNodeInfo root = getRootInActiveWindow();
if (root == null) {
Log.i(TAG, "root is null");
return;
}
// Strategy #1: locate node via its id
String id = node.getViewIdResourceName();
if (id == null) {
Log.i(TAG, "id is null");
} else {
List<AccessibilityNodeInfo> rootNodes = root.findAccessibilityNodeInfosByViewId(id);
if (rootNodes.size() == 1) {
Log.i(TAG, "success (via id)");
return;
} else {
Log.i(TAG, "multiple nodes with that id");
}
}
// Strategy #2: locate node via its text
CharSequence text = node.getText();
if (text == null) {
Log.i(TAG, "text is null");
} else {
List<AccessibilityNodeInfo> rootNodes = root.findAccessibilityNodeInfosByText(text.toString());
if (rootNodes.size() == 1) {
Log.i(TAG, "success (via text)");
return;
}
}
Log.i(TAG, "failed, node was not recoverable");
}
}
#Override
protected boolean onKeyEvent(KeyEvent event) {
Log.i("Key", event.getKeyCode() + "");
return true;
// return super.onKeyEvent(event);
}
#Override
public void onInterrupt() {
}
}
I am developing this on SDK Version 21 (Lollipop) and testing it on a HTC Nexus M8 and a Samsung Galaxy Note2, both showing similar results.
I am creating a video player app for Amazon Fire TV using the Google Leanback code (I know it wasn't intended for Fire TV, but I have done what's necessary to make it work - except for this). I have made it so that a video resumes where you left off if you exit the app by pressing the Home button, or if you pause it, watch a different video, and go back.
However, Amazon is rejecting my app because if you do a microphone search while the video is playing and then go back, the video starts over instead of resuming.
What am I missing? Here is my code for startVideoPlayer() and onPause() from the PlayerActivity:
private void startVideoPlayer() {
Bundle bundle = getIntent().getExtras();
mSelectedMovie = (Movie) getIntent().getSerializableExtra( VideoDetailsFragment.EXTRA_MOVIE );
if( mSelectedMovie == null || TextUtils.isEmpty( mSelectedMovie.getVideoUrl() ) || bundle == null )
return;
mShouldStartPlayback = bundle.getBoolean( VideoDetailsFragment.EXTRA_SHOULD_AUTO_START, true );
sprefs = PreferenceManager.getDefaultSharedPreferences(this);
startPosition = sprefs.getInt(mSelectedMovie.getVideoUrl(), 0);
mVideoView.setVideoPath( mSelectedMovie.getVideoUrl() );
if ( mShouldStartPlayback ) {
mPlaybackState = PlaybackState.PLAYING;
updatePlayButton( mPlaybackState );
if ( startPosition > 0 ) {
mVideoView.seekTo( startPosition );
}
mVideoView.start();
mPlayPause.requestFocus();
startControllersTimer();
} else {
updatePlaybackLocation();
mPlaybackState = PlaybackState.PAUSED;
updatePlayButton( mPlaybackState );
}
}
#Override
protected void onPause() {
super.onPause();
if ( mSeekbarTimer != null ) {
mSeekbarTimer.cancel();
mSeekbarTimer = null;
}
if ( mControllersTimer != null ) {
mControllersTimer.cancel();
}
mVideoView.pause();
mPlaybackState = PlaybackState.PAUSED;
updatePlayButton( mPlaybackState );
SharedPreferences.Editor editor = sprefs.edit();
editor.putInt(mSelectedMovie.getVideoUrl(), mVideoView.getCurrentPosition());
editor.apply();
}
I ended up having to call startVideoPlayer in onResume, like so:
#Override
public void onResume() {
super.onResume();
startVideoPlayer();
}