Video Freeze after lock screen on SurfaceView using SurfaceView and MediaPlayer - android

I was trying to use SurfaceView and MediaPlayer to Display video on Android.
I got a bug now that if I press the power button to lock the screen and then unlock it, The Video displayed on the MediaPlayer is gone and there is only sound. Normally for this situation the surfaceView would be somehow freeze to the last frame of the video before I lock the phone.
It works fine when I hide the screen(such as go to home and open another application) But it just does not work when I lock the phone. I think they both pass the onPause and onResume so I do not understand why would there be different logic.
I'd like to know if there is a solution to solve my problem.
Thanks!

I'm not sure exactly what's going on, but there's a general class of problem that is likely the cause of your trouble.
If your lockscreen is orientation-independent, or wants to be oriented the same way the device is, then the Activity won't be restarted to change orientation. In this case, turning the display off and back on with the power button won't cause the SurfaceView's surface to be destroyed. So if your app is relying on the surfaceCreated and surfaceChanged callbacks to fire to make things happen, then things won't happen.
If you leave the Activity and come back, the Surface does get destroyed and re-created.
The relationship between the Activity lifecycle and the Surface lifecycle is a bit complicated; you can find some notes about it in this article, including two basic patterns for structuring an app. Examples of both "styles" of SurfaceView management can be found in Grafika.
I can't make specific recommendations without knowing how your app works, but mainly you just need to look at what your app does with the idea that Activity and Surface lifetimes are not tightly coupled.

OK, I fix this in a weird way, I think it is probably a bug of Android. Don't have time to do enough testing on this.
First surely as #fadden says you need to have a mMediaPlayer.setDisplay(myHolder) elsewhere not in the surfaceChanged or surfaceCreated since they may not be called while resume from locked screen.
For me the problem still persist after I changed it this way. Here is what I changed that solved the problem: Instead of mMediaPlayer = new MediaPlayer(), we should use mMediaPlayer = MediaPlayer.create(MediaActivity.this, fileUri)

Related

Activity Methods don't work when App is unfocused

I'm running into an issue and I would appreciate some guidance. I'm writing a small Android program in Android Studio that uses MediaProjection to grab a screenshot of the whole screen and then I want to pass that screenshot as a Bitmap to my System Overlay (based on the chatheads example). When the MediaProjection runs and the Imagereader creates the bitmaps, I'm passing the bitmap into a class global variable so I can pass it into my System Overlay service and display it. The problem I'm running into is if I "tab out" to a different app, my methods stop working from the MainActivity. When my app is in focus, everything works well, when a different app is focused, the system overlay contracts and expands like it should, but the screenshots are not being taken anymore. My understand is its because my app becomes "asleep" since it's not in focus. Could anyone recommend a solution? Can I momentarily awake my mainActivity so it can take a screenshot? Can I keep my activity from falling asleep on app switch? Can I bring focus to my app, take the screenshot and then switch focus to the previous app? Any other ways?
My code can be found here: https://github.com/asheron21/FacebookLikeChathead
MainActivity:https://github.com/asheron21/FacebookLikeChathead/blob/master/app/src/main/java/com/example/chatheads/MainActivity.java
Service: https://github.com/asheron21/FacebookLikeChathead/blob/master/app/src/main/java/com/example/chatheads/ChatHeadService.java
Global Variables:https://github.com/asheron21/FacebookLikeChathead/blob/master/app/src/main/java/com/example/chatheads/Globals.java
Thanks!

Map not loading and shows empty screen with black icons

I am using 'sdktools' for calculating routes and navigation. Meanwhile i am getting this issue in design, I tried with 'preserveGLContext' to true in the mapView. How can i solve this issue?
Without knowing your app architecture I can't provide a lot of feedback - can you replicate this behaviour in the demo project? (your map seems corrupted but I can't tell what is happening behind the scenes to arrive at this scenario).
When using multiple map views make sure that the GLSurfaceView is notified when the activity is paused and resumed. GLSurfaceView clients are required to call onPause() when the activity pauses and onResume() when the activity resumes. These calls allow GLSurfaceView to pause and resume the rendering thread, and also allow GLSurfaceView to release and recreate the OpenGL display.

texture corruption on Galaxy Note3 updated with KitKat 4.4.2 when back button pressed

I add some textures to GL Surface, GL displays it, and I press back button,
which means the GL thread stops, the activity destroys, and my application goes onto background. After that, somehow all the texts on the menus that the next activity displays goes corrupted looking images like rectangle and never recovered.
I have searched to solve this problem in stackoverflow and found this might be related with h/w acceleration. But, it doesn't seem to work on my circumstances and I really need a essential solution to this.
Are there anyone that can help me, or experience the same problem with mine?
The release date of my app is upcoming, and any information that you have would be very appreciated.
Do you call GLSurface.onPause () at the appropriate time? Among other things, this will release the OGL Display appropriately (http://developer.android.com/reference/android/opengl/GLSurfaceView.html look at the Activity Life-cycle section).
If this doesn't fix your problem then please provide code snippets.

Android VideoView, seamless playback during orientation change

I'm using a VideoView to play a mp4 file in my application. Is there a way to save all the content that was buffered so when the orientation of the screen is changed the user doesn't have to wait another 10 seconds or more for the video to start playing again.
Perhaps "saving the buffered video" isn't the right solution, I'm just looking for a way to have the playback almost appear seamless when the orientation of the screen is changed.
I'm already saving the video positioning and using the "seekTo" method to start the video where it left off. But I'm looking to prevent the rebuffering as well. I searched all over stackoverflow and couldn't find any discussions on this.
Thanks!
Bradley4
You could handle the configuration change yourself so that the application isn't restarted.
Another solution might be to put the VideoView into a Fragment. Then rather than killing the Fragment with the Activity, you might just pause the video detach the Fragment and re-attach after the configuration change. Although now that I think about it, I think the Fragment may also be re-created on the configuration change...
Very interested in how you solve this, please post your solution if no one answers.

Getting an android app to keep it's OpenGL Context after hitting the home button

First a bit of context: I'm developing a video game for both the Android and iPhone platforms. The way the iPhone works, when a user hits the home button and returns to the game later, in most circumstances the game will pick up RIGHT where it left off with no hicups. I immediately jump back into my rendering and game loop. Setting this up on the iOS platform was absolutely easy for me to do. Accomplishing this on the Android has left me in a fit of rage after hours of wrestling with google results :P
I have my own OpenGL setup for both the iPhone and Android, and everything has been working great. The root of the problem, I believe, is that I need a SurfaceHolder to create an OpenGL Context. Here's the sucky part, when the screen loses focus of the game (ie the home button was hit), Android calls surfaceDestroyed in my SurfaceView class and basically KILLS my opengl context. I could recreate a new one with a new SurfaceHolder when surfaceCreated is called, but then I need to reload all of my art assets which defeats the purpose of everything I'm trying to accomplish.
Can I somehow prevent the Android OS from killing my surface holder, is there some sort of custom view I can use to get this to work? Is there some setting in the Manifest that can help me out here (I doubt it as I have thoroughly tested most of the flags)? I know this is possible because Angry Birds does this perfectly on the Android OS.

Categories

Resources