MediaPlayer stopping when starting another activity - java

I have a problem with the MediaPlayer.
I have a MediaPlayer class that starts as soon as the user launches the App, but as soon as the user taps on the "credits" button another activity starts with the credits screen, but the MediaPlayer stops playing the audio stream.
Is there a way to avoid this?
MediaPlayer setup:
player = new MediaPlayer();
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
I change page with this:
Intent intent = new Intent(this, Credits.class);
startActivity(intent);

Create a local service and put the MediaPlayer in that Service. Bind to the Service from whatever Activity needs to control the MediaPlayer.

This is not possible since the mediaActivity will be pause. You might consider using a dialog instead of an intent for the credits.

Related

Is there a method to call activity back when onStop() immediately?

I want to create an app that can't be stopped (send to background). Is there a method to achieve this? this is my code
#Override
protected void onUserLeaveHint()
{
Intent i = new Intent(this, DemoActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
startActivity(i);
}
but this code didnt respond immediately, I search everywehere for solution but get nothing. Thank you.

How to keep the media player keep playing the same sound track and not starting another one in Android

I have an action bar icon on the main activity where when you click on it plays a soundtrack and I can pause and play it properly. I call this code below when the action bar play icon is pressed:
private void play() {
if (!mp.isPlaying()) {
mp.start();//play sound
play=true;
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
mp.seekTo(0);
}
});
} else {
mp.pause();
play=false;
}
}
onOptionsItemSelected code:
if (id == com.app.myapp.R.id.play) {
play();
invalidateOptionsMenu();
}
I have another activity that has a home button action icon where when I click it, it takes me back to the main activity and the sound track keeps playing but when I click the play icon again, it plays another instance of the same track which I don't want it to do. I'm use the following code to go back to the main activity:
if (id == com.app.myapp.R.id.homebutton) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity
}
What extra piece of code do I need to do what I want my app to do?
Any help would be greatly appreciated.
Jaffer
Every time you launch the main activity from sub activities your main activity is getting created. Since you are creating mediaPlayer instance in onCreate() function new media player instance is created causing two playbacks.
Modify how you launch the main activity like below.
if (id == com.app.twelveimams.R.id.homebutton) {
Log.e("kiran", "clear top from introu");
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity

Android Nougat Media Player how put music Animation in Lock Screen?

I am using the original Music player in android Nougat, while the music is playing in the Lock Screen Appear an animation in the Buttom like this screenshot.
But when i using my own App Media Player in the same Android Nougat and Same device in the Lock Screen dont appear that animation.
The question is: How can i Add that animation in my Media Player App? it´s not a gif image because The animation moves to the rhythm of the music.
**This is my Notification Method ** if i am missing something or if i have to add something else.
ublic void Custom_Notificacion(){
Notification notification = new Notification(R.drawable.logobubble, null, System.currentTimeMillis());
notificationView = new RemoteViews(getPackageName(), R.layout.layout_notificacion_personalizada);
notificationView.setImageViewBitmap(R.id.id_FotoAlbumNotif,FotoNotif);
notificationView.setTextViewText(R.id.id_NombreMp3Notif,NommbreArtista);
notificationView.setTextViewText(R.id.id_NombreCancionNotif,NombreCancion);
notification.contentView = notificationView;
notification.flags |= Notification.FLAG_NO_CLEAR;
startForeground(constantes.NOTIFICATION_ID.FOREGROUND_SERVICE,notification);
}
I've read an article about this,an feasible solution is custom a lock screen page and display the Animation view within it,to achieve this you need a Service listening to the LOCK_SCREEN Broadcast,and start your LockScreenActivity ;replace the system lock screen at the same time.
here is some code segment might be helpful:
Register Broadcast Receiver
IntentFilter mScreenOffFilter = new IntentFilter();
mScreenOffFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(mScreenOffReceiver, mScreenOffFilter);
  // on receive method
private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
#SuppressWarnings("deprecation")
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(NOTIFY_SCREEN_OFF)) {
Intent mLockIntent = new Intent(context, LockScreenActivity.class);
mLockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(mLockIntent);
}
}
Disable the Lock screen
KeyguardManager mKeyguardManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock mKeyguardLock = mKeyguardManager.newKeyguardLock("CustomLockScreen");
mKeyguardLock.disableKeyguard();
Hope this could be a little help

how to pause and play music player

I want to write a program which can pause and play every media players in android. How can i set priority of this program higher than the others? also i wrote the following code
public static final String SERVICECMD = "com.android.music.musicservicecommand";
public static final String CMDNAME = "command";
public static final String CMDPAUSE = "pause";
public static final String CMDPLAY = "play";
public static final String CMDTOGGLEPAUSE = "togglepause";
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (mAudioManager.isMusicActive()) {
Intent i = new Intent(SERVICECMD);
i.putExtra(CMDNAME, CMDPAUSE);
MainActivity.this.sendBroadcast(i);
Toast.makeText(this, "media player is pause!", Toast.LENGTH_LONG).show();
}
if (!mAudioManager.isMusicActive()) {
Intent i = new Intent(SERVICECMD);
i.putExtra(CMDNAME, CMDPLAY);
MainActivity.this.sendBroadcast(i);
Toast.makeText(this, "media player is play!", Toast.LENGTH_LONG).show();
}
and it can pause every players but i dont know how to play them again. I can play some players but not all of them i dont know why ???
Have an onClick for a play and pause button
You'll have to find a way to retrieve all of the MediaPlayer Objects.
Pausing:
mMediaPlayer.pause(); // Pause the MediaPlayer
Playing:
mMediaPlayer.prepare(); // Prepares the MediaPlayer to play a song
** Please have an onPrepare method as well
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start(); // play!
}
Also, here's a reference for you:
https://github.com/naman14/Timber
This open source music app should have most of the relevant resources you need to creating a music player controller or etc. You'll be able to understand how it works.
Also, the code you've presented won't allow me to help you with identifying any issues unless you have not implemented the MediaPlayer Object.
Here's why: Difference between Audiomanager and MediaPlayer
-- EDIT --
Here's how you can stop other MediaPlayer instances via the MediaPlayerRegistry.
Android: How to stop other active media players?

Call Server function and popup on Broadcast receiver android

I have an application in which I have to call off an alarm/notification each 30 Minutes.
I want the feature like
1- If app is closed, it open the app, Call a dialog box. On click it will call a serverFunction and if MainActivity is running, update its UI.
2- If the app is already opened , Call a dialog box. On click it will call a serverFunction. Since MainActivity is may or may NOT on the top, update its UI Or NOT.
In My MainActivity.class
private void callNotification()
{
AlarmManager service = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, AlarmReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(this, 0, i,PendingIntent.FLAG_CANCEL_CURRENT);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, Constants.TIME_CONSTANT);
service.set(AlarmManager.RTC_WAKEUP ,time.getTimeInMillis(), pending);
}
public class AlarmReceiver extends BroadcastReceiver
{
#Override
public void onReceive(final Context context, Intent intent)
{
}
}
The problem here is , I can't put a dialog box in onReceive since context is not Activity context. What If the app is opened , Now how am I suppose to implement above features.
In your onReceive place this to call your activity:
Intent i = new Intent(context, AlertActivity.class);
i.setFlags
startActivity(i);
Once you are in your activity you can open up a dialog.
I recommend you use a different activity than your main one to handle displaying the alert, as it makes sense from a design standpoint and it also makes implementation easier. Remember you can make Activities look like dialogs...

Categories

Resources