Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. package com.example.hardok.kolosik;
  2.  
  3. import android.app.Service;
  4. import android.content.Intent;
  5. import android.media.MediaPlayer;
  6. import android.os.IBinder;
  7. import android.widget.Toast;
  8.  
  9. import static android.app.Service.START_STICKY;
  10.  
  11. /**
  12.  * Created by hardok .
  13.  */
  14. public class MyService extends Service implements MediaPlayer.OnCompletionListener {
  15.  
  16.     MediaPlayer player;
  17.     @Override
  18.     public IBinder onBind(Intent arg0) {
  19.         return null;
  20.     }
  21.  
  22.     public void onCreate() {
  23.         Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
  24.         player = MediaPlayer.create(this, R.raw.sonata);
  25.         player.setLooping(false); // Set looping
  26.         player.setOnCompletionListener(this);
  27.     }
  28.  
  29.  
  30.     @Override
  31.     public void onDestroy() {
  32.         super.onDestroy();
  33.         if (player.isPlaying()) {
  34.             player.stop();
  35.         }
  36.         player.release();
  37.         Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
  38.     }
  39.  
  40.  
  41.     @Override
  42.     public int onStartCommand(Intent intent, int flags, int startId) {
  43.         if (!player.isPlaying()) {
  44.             player.start();
  45.         }
  46.         return START_STICKY;
  47.     }
  48.  
  49.     public void onCompletion(MediaPlayer _mediaPlayer) {
  50.         stopSelf();
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement