Android Alarm with user defined playlist as sound and time - android

I'm creating an Alarm based Android app where I should pass an custom playlist as alarm sound when alarm start user have to set how much time its gonna run and that playlist sounds run as alarm started and I don't have any idea how to create it I just want an logic no code.
App reference Alarm clock HD
https://play.google.com/store/search?q=alarm%20clock%20hd

public class serv extends Service{
MediaPlayer mp;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate()
{
mp = MediaPlayer.create(this, R.raw.b);
mp.setLooping(false);
}
public void onDestroy()
{
mp.stop();
}
public void onStart(Intent intent,int startid){
Log.d(tag, "On start");
mp.start();
}
}

Related

Audio will still play even if I am using other app or the screen is locked

I am creating an app similar to other audio streaming apps. Is there a way to let a third party player stream music even though the screen of the mobile phone is locked or I am using other application?
You require to create the unbound service to play the media in background while other app may be opened or screen might be lock.
You can refer this for understanding in detail regarding use of services: https://developer.android.com/guide/components/services.html
Like this:
Create Intent first
Intent svc=new Intent(this, BackgroundSoundService.class);
startService(svc);
Create class for the service to run in the background:
public class BackgroundSoundService extends Service {
private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.idil);
player.setLooping(true); // Set looping
player.setVolume(100,100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
}
public void onPause() {
}
#Override
public void onDestroy() {
player.stop();
player.release();
}
#Override
public void onLowMemory() {
}
}
Do not forget to call service in manifest file
<service android:enabled="true" android:name=".BackgroundSoundService " />
Hope this helps you.

Service background music does not turn off

In my app there is a background music, for what I have a service. It works fine, but it "keeps playing" when either I close the app or turn off the screen.
Should I change something in onPause or onStop?
My code:
public class MyService extends Service {
private final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.id);
player.setLooping(true); // Set looping
player.setVolume(100, 100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
protected void onStop() {
player.pause();
}
public void onPause() {
player.pause();
}
#Override
public void onDestroy() {
player.stop();
player.release();
}
#Override
public void onLowMemory() {
}
}
As screen turns off your activity becomes invisible which triggers onPause followed by onStop
Screen on, on the other hand, triggers onStart followed by onResume.
when ever you want to stop service use
stopService(new Intent(ActivityName.this, ServiceClassName.class));
Probably copies of the player are created somehow. Try to make MediaPlayer variable static and then try to deallocate it after pausing onPause(). Anyway it is weird, that you are creating MediaPlayer instance in service.

Controlling Background Music android

I am using a Service to play background Music. The problem is that the music continues playing when i have finished the activity. when i press home button the music stop i want to make music continue playing while application is use and stop when home button press help me please. can some tell me how to do this
private static final String TAG = null;
MediaPlayer player;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.sample);
player.setLooping(true); // Set looping
player.setVolume(100,100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TODO
}
public IBinder onUnBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onStop() {
}
public void onPause() {
}
#Override
public void onDestroy() {
player.stop();
player.release();
}
#Override
public void onLowMemory() {
}
}
You should use a service and play the music.
http://developer.android.com/guide/components/services.html
Service runs in the background so even when you switch activities it still runs and you can play your music there.
You can start a service and then stop it when you need. Also you can bind the service to the activity. Look at the docs in the above link for more info.
Example :
https://thenewcircle.com/s/post/60/servicesdemo_using_android_services
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.braincandy);
// song is res/raw/yourmusicfile.mp3
player.setLooping(false); // Set looping
}
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player.start();
}
}
Now just start the service and you can stop the service when you need. You can switch between activities and the service still runs playing the music.
Since the code i huge i uploaded to DropBox. Here's the sample i made
https://www.dropbox.com/s/fjc4krpcwsg7qm6/ServicesDemo.zip

Trying to make a pause/resume music activity using service

I'm trying to create an activity with one button in its layout which will pause or resume the music when it's pressed. the problem is that when it's pressed to resume the music it starts the audio file all over again. Any idea how to make it play from the last point it stopped rather than restarting it?
Main activity
public class MainActivity extends Activity implements OnClickListener {
private Boolean isMusicPlaying;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View musicButton = findViewById(R.id.music_button);
musicButton.setOnClickListener(this);
startService(new Intent(getBaseContext(), MusicService.class));
isMusicPlaying = true;
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.music_button)
if (isMusicPlaying) stopService(new Intent(getBaseContext(),
MusicService.class));
else startService(new Intent(getBaseContext(), MusicService.class));
isMusicPlaying = !isMusicPlaying;
}
}
Service class
public class MusicService extends Service {
MediaPlayer player;
#Override
public IBinder onBind(Intent arg0) {
return null;
}
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.song);
player.setLooping(true); // Set looping
player.setVolume(100, 100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return START_STICKY;
}
#Override
public void onDestroy() {
player.pause();
}
}
It seems like MediaPlayer object in your service is not kept when you stop the service.
You can get the current position of your song using getCurrentPosition() method before you stop your service,
player.pause();
currentPos = player.getCurrentPosition();
and send this value back to activity before it is stopped.
Then, you can use seekTo(int) method to move to specified time position before playing.
player.seekTo(currentPos);
player.start();
For communication between your service and activity, you might register broadcastreceiver and communicate using intent or use a bound service.
UPDATE:
I found following tutorial and you'd better to follow this. It's about using a service with MediaPlayer.
Especially, you are not using prepare method at all when you initialize your MediaPlayer. put these lines:
player.setOnPreparedListener(this);
player.prepareAsync();
and add listener:
public void onPrepared(MediaPlayer player) {
player.start();
}
http://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices
Seeing that the activity is re-started why don't you save the states to Shared Preferences

Start android app without launching main gui?

I just want an app that runs some code to shortcut to a certain part of ics. How can I run this code at run time without having to launch the main.xml? I see the manifest launches the main.xml but that is about all I know.
Have you heard of Service Components in Android ? Service allows you to do work in background without disturbing your current GUI. You can do most of all the work that you want to hide from the user in GUI.
Have a look at example of Audio playing code, ( Original Source from Makarana's Blog. )
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.braincandy);
player.setLooping(false); // Set looping
}
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player.start();
}
}

Categories

Resources