Problems with Acitivity LifeCycle with VideoView playback - android

Hi all i've ran into another problems with VideoView.
Then video is playing, and I put device asleep, using hard button, onPause() is called. But it followed by:
03-17 11:26:33.779: WARN/ActivityManager(884): Activity pause timeout for HistoryRecord{4359f620 com.package/com.package.VideoViewActivity}
And then i have onStart()/onResume() again and Video starts playing. I've try to move code around onStart()/onStop() - doesn't seems to make difference.
sample code :
public class VideoViewActivity extends Activity {
private String path = "";
private VideoView mVideoView;
private static final String MEDIA_URL = "media_url";
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.videoview);
mVideoView = (VideoView)findViewById(R.id.surface_view);
path = getIntent().getStringExtra(MEDIA_URL);
}
#Override
public void onResume() {
super.onResume();
mVideoView.setVideoPath(path);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.start();
}
#Override
public void onPause() {
super.onPause();
mVideoView.stopPlayback();
mVideoView.setMediaController(null);
}
}
Why is it happening? And how do I stop that?
It's not a greatest experience than you put your device to sleep and it starts playing video

OK, Looks like the behavior is related to activity lifecycle and the fact that VideoViewActivity is set to landscape in the manifest. Adding
android:configChanges="keyboardHidden|orientation"
for that activity
seems to fix the problem and then you put device to sleep only onPause() called vs before - all lifecycle methods were executed.
I'll do more testing to make sure it fixed...

Related

MediaPlayer mp3 won't resume after pause, known solutions not working

My activity is playing mp3 file while is active, and my intention is to pause it while app takes user to another activity and resume when this activity is again active. Solution that was here around seems to be the right one but unfortunately id doesn't work, the audio file every time starts from beginning. My code is obvious:
private int length;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
mediaPlayer = new MediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.bensound_thejazzpiano);
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(0.4f,0.4f);
mediaPlayer.start();
}
#Override
protected void onPause() {
super.onPause();
mediaPlayer.stop();
length = mediaPlayer.getCurrentPosition();
}
#Override
protected void onResume() {
super.onResume();
mediaPlayer.seekTo(length);
mediaPlayer.start();
}
I would also be satisfied with methods that mutes sound and restores the volume after goin back to activity (and muted tune can go on in background), but replacing start/stop methods with setVolume also doesn't provide to any results...
Maybe there are wrong methods I've overridden?
You have to "pause"(not stop) the mediaplayer in onPause, then start in onResume. That worked for me.

MediaPlayer to play music across Activites

I'd like to play music across Activities and I'm using a simple class to implement it. This class ( BackgroundMusic ) starts the music with MediaPlayer when I call the startMusic() method and stops it when I call the stopMusic() method. When I use it only in one Activity it works perfectly. OnCreate calls startMusic() method and onPause calls stopMusic() method and the MediaPlayer behave on the right way. The problem starts when I'd like to move to another Activity. When I'd like to stop the music it throws me NullPointerExepction for the mediaplayer.stop() . So it looks like the app thinks that I want to stop a never started MediaPlayer. I tried to call the startMusic() method in every onCreate method but the music starts again and again and I'd like to play only one music which don't stop and starts again when I move to another Activity. Is it possible to do that with class or I have to use Service? I hope you can help me to that with class.
BackgroundMusic
public void startMusic() {
mediaPlayer1 = MediaPlayer.create(context, R.raw.zenenegy);
if(palya <= 5 || palya > 15){
mediaPlayer1.start();
mediaPlayer1.setVolume(0.2f, 0.2f);
mediaPlayer1.setLooping(true);
play = true;
}
}
public void stopMusic(){
if(play){
mediaPlayer1.stop();
mediaPlayer1.reset();
mediaPlayer1.release();
mediaPlayer1 = null;
play = false;
}
}
An Activity
BackgroundMusic bm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fomenu);
bm = new BackgroundMusic(mentes,this);
if(sounds){
bm.startMusic();
}
}
#Override
protected void onPause() {
if(sounds){
bm.stopMusic();
}
super.onPause();
}
If I set the mediaplayer to static in the BackgroundMusic it works perfectly.

Playback video when back button was pressed

Hi All I have got a video set as a background in my app. When the app starts the video plays at the main menu and everything works great. Now when I select to go to next activity the video stop and the next activity starts and when the user is then finished with this activity and presses the back button to go to go to the main menu the video should play again, however it doesn't. Hope some one can help me with this. here is my code:
public class MainActivity extends Activity {
VideoView animation;
private MediaController mc;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = MediaPlayer.create(this, R.raw.leftbanktwo);
mp.setLooping(true);
VideoView animation = (VideoView) findViewById(R.id.imageAnimation);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.cartoon);
mc = new MediaController(this);
animation.setMediaController(mc);
animation.requestFocus();
animation.setVideoURI(uri);
animation.start();
}
This is because your onCreate() method is not called again when user back to first Activity. If you want it to work like you described put the code which starts video in onResume() method.
Also, I would recommend to check out Activity Lifecycle.
Try This
#Override
protected void onResume() {
super.onResume();
if(mp!=null){
mp.reset();
mp.start();
}
}
#Override
protected void onPause() {
super.onPause();
if(mp!=null && mp.isPlaying()){
mp.pause();
}
}

Android VideoView not detecting streaming error

Whenever videoview is playing a video and there is a network connection drop and the video freezes, my code is not throwing an error even though I have set the videoview error listener.
public class MainActivity extends Activity {
static MediaController mc;
static VideoView vw;
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
mc = new MediaController(this);
vw = (VideoView) findViewById(R.id.videoView);
vw.setVideoPath("http://173.45.164.105:1935/live/myStream/playlist.m3u8");
vw.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
vw.stopPlayback();
vw.start();
Log.i("VIDEO ERROR ", "FROZE");
return true;
}
});
vw.requestFocus();
vw.start();
}
}
To my understanding the native VideoView element only calls the onError() method when opening the video.
See: VideoView Source
In the openVideo() method -- called when setting the VideoView path/URI -- a couple of exceptions are handled by throwing a MediaPlayer.MEDIA_ERROR_UNKNOWN error.
I'm not positive what goes on in the background with the MediaPlayer whenever a connection is lost with the video source, but it seems that it simply waits indefinitely waiting to re-establish a connection. It will never throw an error via the onErrorListener.
However if you call the setVideoPath() or setVideoUri() method again it will call the onError() method b/c an Exception will be thrown in openVideo().
You'll have to either figure out some way to detect that the connection is lost or wait to see if the time on the video is getting updated and throw an error manually.
If anyone finds a better way to fix this let me know.

Stop media player

I am new in android and I have another (simple?) problem. I don't know how to stop Media Player. This is my simple code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
MediaPlayer mp;
mp = MediaPlayer.create(this, R.raw.sauronsound);
mp.setLooping(false);
mp.start();
#Override
protected void onDestroy()
{
// Stop play
super.onDestroy();
mp.stop();
}
}
After pressing back button app goes to my first activity but sound is on. When I leave an app it is on too. What should I do to turn off the sound?
As always excuse me for my poor English.
I solved the problem thanks to you Guys. Working code:
public class SauronEye extends Activity {
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
mp = MediaPlayer.create(this, R.raw.sound);
mp.setLooping(false);
mp.start();
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(10000);
}
#Override
protected void onStop()
{
// Stop play
super.onStop();
mp.stop();
}
}
Is it correct (it works)? Thank you for helping me.
mp reference that you are using on onDestroy is different from the one you are using on onCreate. Move the MediaPlayer mp; line to outside the onCreate class.
Check this out http://developer.android.com/reference/android/media/MediaPlayer.html
You can call stop or pause based on your requirement.When you select back button your onpause would be called, in that method you can call mp.stop(), onDestroy would be called only when activity is completely destroyed
onDestroy is only called when the activity is killed by the system. Rather than placing it in onDestroy, you should put it in onPause(), which is what's called whenever your activity is moved to the background but remains in memory. (Which is what happens with a back button being pressed or leaving the app)
#Override
protected void onPause() {
super.onPause();
mp.stop();
}
you can call the override implements source codes really easily and add them each into your code. All you need to do is right click the insertion point where you want them and click on Source->Override/Implement Methods. It will bring up a dialog box and you click on the methods you need, try using ondestroy, onpause, onstop. For your code and after it implements each of them just add the following to each.
protected void onDestroy{
super.onDestroy();
mp.release();
}
protected void onStop{
super.onStop();
mp.stop();
}
protected void onPause{
super.onPause();
mp.pause();
}
Also if you want a little more with you soundcodes you can try this link
stealthcopters link or you can try this video series
cornboyzAndroid

Categories

Resources