How can I make background music for android project - java

I'm making a small android app like image below:
I have a sound button. All I want is background music will play across all activity and when I click on sound button, music will stop. Or click to play music again.
I dont know how to make it. PLease help me! or give me link to refer.
I'd gladly appreciate your help it Thanks!

Hi please Follow below steps
initialized
MusicPlayer object when your app starts MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
set this in your button's click event.
Start it by mediaPlayer.start();
3.Stop it by mediaPlayer.stop();
but when you stopping mediaPlayer before that just check mediaPlayer.isPlaying() with if Condition. then Stop. else error thrown.
or follow this Example Turorial Link

In order for media to be played in the background of your app location when the user is interacting with it you must start a Service from your application's main activity and the service shall contain all the methods related to playback. To allow activity to interact with the service, a service connection is also required. In short, we need to implement a bounded service.
Refer http://www.codeproject.com/Articles/258176/Adding-Background-Music-to-Android-App

Related

Interfacing with MainActivity from video player

I'm new to android app development and just creating a simple application for my workplace that allows video playback control from a web panel, so the app pings the panel on a loop and if it gets a request to play a video, it plays it. If it gets a request to stop the video or play something else, then it does that.
From MainActivity I'm using startActivity to start an instance of ExoPlayer, I know how to pass variables to the activity, but how do I send information the other way or control the playback? Basically that loop that runs constantly to check for new actions is running in MainActivity, once the player is started I have no ability to stop playback, get stream metrics or do anything with that instance at all. I realise I'm probably doing things backwards and should have a background service do the checks, but I still have no idea how to pass information back and forth.
Does anyone have any tips or suggestions? Thank you!
Instead of using two activities, you have to use an acitivty (the main) and a fragment (the video player). Then define a callback interface which will implement the activity and thus manage the communication between fragment and activity
See this https://blog.mindorks.com/how-to-communicate-between-fragments/

Android media player stop playing in service

I already asked about it before, but my question even after edits about my progress of fix this noone answered to it anymore. This is original question:
Android media player stop playing while in background
So in short, I'm making music player app, but mediaPlayer stop sometimes when loading next song. After many tests I find out that it stops at mediaPlayer.prepare() and it won't continue as long as I don't trigger any action on phone like turning on display, volume up/down, click headset button. I'm out of ideas what can be a reason of it.
maybe you need to call prepareasync() instead of prepare().

android - MediaPlayer stops working at some point

I have been working on an app, and it involves sound.
Now, I use this to start it:
final MediaPlayer tick = MediaPlayer.create(getApplicationContext(), R.raw.tick_16);
tick.start();
But for some reason, after some time (depends on the device, on HTC One it is a few seconds and in others a few minuets) all sound stopps.
What is the cause of it and how can I solve it?
Use this tutorial to play audio :
http://www.tutorialspoint.com/android/android_mediaplayer.htm
you may be use tick.start(); in onResume() method of your activity class
Try starting the MediaPlayer in a Service and a Notification inside startForeground().

The sound stopped when press lock button

I had the Android App which play sound when specific times , I did my
code well but when I locked the screen the sound isnot play , I
checked my code and I find that the code that I added when user press
lock button is the reason of the problem .How to solve this issue ?
#Override
protected void onPause() {
Player.stopAzan();
finish();
super.onPause();
}
Maybe try using a service? Services are basically same that activities but they run in background and have no content view if I get them right :D
http://www.vogella.com/tutorials/AndroidServices/article.html
What you are trying to achieve is impossible since when the screen is locked, the Activity is stopped.
You either decide if want to play in background or not, because when the screen is locked, the Activity goes background.
If you don't need to play in background then you're good to go, just remove the finish() method.
If you do need to play the music in background, use a Service to start and stop player based on Intents passed from Activity user controls.

Calling onCreate() from locked screen

I am developing an android emergency app and I want to add a feature that can call a certain method from my activity class when the screen is locked. Is there anyway for my app to detect any kind gestures like shake, screen tap pattern or any kind of movement when the screen is locked?
Not sure if it will work, but I think you can put a listener in a service and put the servicee in the foreground. I have no clue what exactly youwant to achieve but this is what I can think of right now. BTW -- pass information from a service the foreground activity

Categories