I have two activities in my android application.I am playing video in video view in both the activities.When i move to second activity its started playing video on videoview. But when I return back to previous activity and try to interact with UI, I am getting:
java.lang.IllegalArgumentException: Receiver not registered:
android.media.MediaPlayer$1#4123c640
And also I am getting:
Activity com.exe.sample.Test1 has leaked IntentReceiver android.media.MediaPlayer$1#4123c640 that was originally registered here. Are you missing a call to unregisterReceiver()?
in Second activity.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
Button b=(Button)findViewById(R.id.button1);
VideoView videoview = (VideoView) findViewById(R.id.videoView1);
videoview.setVideoURI(Uri.parse(url));
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(MainActivity.this, Test1.class));
}
});
}
Second Activity code:
public class Test1 extends Activity { private VideoView videoview;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.tset);
videoview = (VideoView) findViewById(R.id.videoView2);
videoview
.setVideoURI(Uri
.parse(url));
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
super.onCreate(savedInstanceState);
}
add mp.release() in Test1.onStop().
the log show that 'android.media.MediaPlayer$1#4123c640' is a anonymous inner BroadcastReceiver class of MediaPlayer, it should be unregistered in MediaPlayer.release().
Related
I have 3 activity, when I move from 1st act.to 2nd act. one music will start, and when I move from 2nd act. to 3rd act., 2nd activity's music should stop and 3rd activity's music should start. As per my coding, 2nd activity's music is stopping, but 3rd activity's music does not start. my code is given here-under: please help me.
//1st activity code start
public class MainActivity extends AppCompatActivity {
Button b;
static MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
final MediaPlayer mp = MediaPlayer.create(this,R.raw.bakaratwozeroone);
b = (Button) findViewById(R.id.butmove);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
});
}
}
//1st activity code end
//2nd activity code start
public class Main2Activity extends AppCompatActivity {
Button b;
static MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
#Override
protected void onResume() {
super.onResume();
final MediaPlayer mp = MediaPlayer.create(this, R.raw.mumfive);
b = (Button) findViewById(R.id.butmove2);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
startActivity(new Intent(Main2Activity.this, Main3Activity.class));
}
});
}
#Override
protected void onPause() {
super.onPause();
if(mp !=null);
mp.release();
mp = null;
}
}
//2nd activity code end
No need to keep MediaPlayer instance on every activity, Use BoundService to run media player and make communication with Service and activities.
For reference,use this link
https://github.com/SimpleMobileTools/Simple-Music-Player
I have an activity (A), it has a VideoView. I setup video view with onPrepareListener. When I start new activity (B) from A. After that, I come back to A from B, all element in onPrepared is called??? Why it happen??? So I'm sorry my english not good, thank you :D
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getBaseContext(), SecondActivity.class));
}
});
mVideoView = (VideoView) findViewById(R.id.video_view);
mVideoView.setVideoPath(
"/storage/emulated/0/Download/Charlize Theron's Variety Cover Shoot - Behind the Scenes.3gpp");
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer m) {
Log.i("test", m.toString());
}
});
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
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
...
...
}
Below are my codes. My videoview restarts everytime you press on the home/power button. Are there any way i can make it continue where my video has been playing before pressing the home/power button.
Edited : I have put in the codes mentioned by Bharat. But it seems that I'me getting error vd not resolved.
public class Video1 extends Activity {
private MediaController mc;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videomain);
//AdView adView = (AdView)this.findViewById(R.id.adView);
//adView.loadAd(new AdRequest());
VideoView vd = (VideoView) findViewById(R.id.VideoView);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.opening);
mc = new MediaController(this);
vd.setMediaController(mc);
vd.setVideoURI(uri);
vd.start();
vd.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
Intent intent=new Intent(Video1.this,GalleryVideo.class);
startActivity(intent);
}
});
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
protected void onPause() {
vd.pause(); /**ERROR - vd cannot be resolved**/
super.onPause();
}
#Override
protected void onResume() {
vd.resume(); /**ERROR - vd cannot be resolved**/
super.onResume();
}
}
Try this in your class
#Override
protected void onPause() {
vd.pause();
super.onPause();
}
#Override
protected void onResume() {
vd.resume();
super.onResume();
}
when your activity will go on pause it will pause your video view. when your activity will resume it will resume it in on resume.