I have a videoview that plays as a background. How do I pause it when my app looses focus and resume the video when the app has focus again? Here is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
VideoView videoView = (VideoView) findViewById(R.id.launcherVideo);
Uri src = Uri.parse("android.resource://com.package/raw/video");
videoView.setVideoURI(src);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setVolume(0, 0);
mp.setLooping(true);
}
});
//videoView.setMediaController(new MediaController(this));
videoView.start();
}
override onPause() method and write code for pause on that method and override onResume() method and write code for resume on that method
Related
I'm trying to play music in all activities in my app but when I switch between mainactivity and another activity music stopped please someone help me without using Service
MainAcivity :
ToggleButton MusicButton;
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.vaporv2);
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mediaPlayer.start();
}
});
//code
#Override
protected void onPause() {
super.onPause();
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
#Override
protected void onResume() {
super.onResume();
if(mediaPlayer!=null && !mediaPlayer.isPlaying()){
if(MusicButton.isChecked()){
mediaPlayer.pause();
}
else
mediaPlayer.start();
}
}
I don't know what I can do in the other activity.
You can pass the current seek position using extras, but the Music may stop while switching between activities.
I suggest that you use service then in each activity onPause event you check if you are switching to another activity in your application or not, if not send some broadcast to the service to stop playing music.
I'm trying to play music when the user clicks in the MUSIC ON toggle button, and music will pause when he clicks in MUSIC OFF. I also need to play music when opening the app. This is my code but doesn't work:
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.vapor);
mediaPlayer.start();
.....
}
MusicButton = (ToggleButton)findViewById(R.id.toggleButton);
MusicButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(MusicButton.isChecked()){
mediaPlayer.start();
}
else{
mediaPlayer.stop();
mediaPlayer.release();
}
}
});
#Override
public void onPause() {
mediaPlayer.stop();
mediaPlayer.release();
super.onPause();
}
#Override
public void onResume() {
mediaPlayer = MediaPlayer.create(this, R.raw.vapor);
mediaPlayer.setLooping(false);
mediaPlayer.start();
super.onResume();
}
Error log:
java.lang.IllegalStateException
at android.media.MediaPlayer._start(Native Method)
at android.media.MediaPlayer.start(MediaPlayer.java:1384)
at com.myapp$MainActivity$2.onClick(MainActivity.java:80)
first,you should not use setOnClickListener() on ToggleButton,ToggleButton has function setOnCheckedChangeWidgetListener to listene the status of compent
second,about you error,maybe your ToggleButton is unchecked,when you clicked it, ToggleButton become checked and invoke you code mediaPlayer.start();,but you start mediaPlayer at onCreate() already,you can check it.
I have a simple app where a video loads from a url and when you click on the video, the MediaController pops up.
This works fine, but I'm wondering if there is a way I can just skip that step and have it so that clicking the video automatically triggers the 'play' function of the MediaController without having to ever see it.
These videos are short and I don't need to be able to control them other than just pressing play.
Here is my code:
private static final String MOVIE_URL ="http://mywebsite.com/videos/testvideo.mkv";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView vid = (VideoView) findViewById(R.id.videoView);
Uri video = Uri.parse(MOVIE_URL);
vid.setMediaController(new MediaController(this));
vid.setVideoURI(video);
vid.seekTo(1);
vid.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View view)
{
vid.start();
vid.requestFocus();
}
}
);
}
EDIT
I needed to use onTouchListener instead of onClickListener because it's a videoview. This works perfectly!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView vid = (VideoView) findViewById(R.id.videoView);
Uri video = Uri.parse(MOVIE_URL);
// vid.setMediaController(new MediaController(this));
vid.setVideoURI(video);
vid.seekTo(1);
vid.setOnTouchListener(
new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event) {
vid.start();
vid.requestFocus();
return false;
}
}
);
}
Add a PreparedListener to listen for video if prepared . When the VideoView is prepared to start the play
vid.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
vid.start();
vid.requestFocus();
}
});
vid.setVideoURI(video);
When setVideoURI, the instance of MediaPlayerwill be created and register for MediaPlayer setOnPreparedListener . So that when MediaPlayer is prepared the VideoView onPrepared listener will get called.
In my nexus-5 the progress indicator which i have set on video view never dismisses.
My code is as
PlayerActivity extends Activity{
private VideoView mVideoView;
private OnPreparedListener mListner=new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
//dismiss progress indicator
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playback_activity);
mVideoView = (VideoView) findViewById(R.id.video_view);
mVideoView.setOnPreparedListener(mListner);
// show progress dialog on videoView
...
...
}
I have a VideoView and it is pause when I start an Email intent. When the email intent is done, I want the videoView to continue playing, however, it restarts from the beginning.
#Override
public void onPause() {
Log.d(TAG, "onPause called");
super.onPause();
videoView.pause();
}
#Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume called");
videoView.start();//resume() doesnt work
}
How can I get the videoView to resume from where it left off.
What about this:
#Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume called");
videoView.seekTo(stopPosition);
videoView.start(); //Or use resume() if it doesn't work. I'm not sure
}
// This gets called before onPause so pause video here.
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
stopPosition = videoView.getCurrentPosition();
videoView.pause();
outState.putInt("position", stopPosition);
}
Then in your onCreate() call the stopPosition from the bundle and set it globally
#Override
protected void onCreate(Bundle args) {
super.onCreate(args);
if( args != null ) {
stopPosition = args.getInt("position");
}
Using seekTo gives a slight flickering efeect while resuming the video. Better approach would be using pause() and start() methods of MediaPlayer class instead of using methods from VideoView. The start() method of VideoView restarts the video from the beginning, but start() method of MediaPlayer resumes the video if the pause() method had been previously called.
Here is what official android doc says
public void start ()
Added in API level 1 Starts or resumes playback. If playback had
previously been paused, playback will continue from where it was
paused. If playback had been stopped, or never started before,
playback will start at the beginning.
You can get the MediaPlayer associated with a VideoView in OnPreparedListener of VideoView.
public class MainActivity extends Activity {
private MediaPlayer mMediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
videoView.setVideoPath(path);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer = mp;
}
});
}
}
Then subsequent pause() and resume() methods can be called on MediaPlayer object itself.
//To pause the video
mMediaPlayer.pause();
//To resume the video
mMediaPlayer.start();
Very simple yet efficient solution. Hope it helps!