Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.hardok.kolosik;
- import android.app.Service;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.os.IBinder;
- import android.widget.Toast;
- import static android.app.Service.START_STICKY;
- /**
- * Created by hardok .
- */
- public class MyService extends Service implements MediaPlayer.OnCompletionListener {
- MediaPlayer player;
- @Override
- public IBinder onBind(Intent arg0) {
- return null;
- }
- public void onCreate() {
- Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
- player = MediaPlayer.create(this, R.raw.sonata);
- player.setLooping(false); // Set looping
- player.setOnCompletionListener(this);
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- if (player.isPlaying()) {
- player.stop();
- }
- player.release();
- Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- if (!player.isPlaying()) {
- player.start();
- }
- return START_STICKY;
- }
- public void onCompletion(MediaPlayer _mediaPlayer) {
- stopSelf();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement