Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. package com.example.audioplayer;
  2.  
  3.  
  4. import android.app.Service;
  5. import android.content.Intent;
  6. import android.media.AudioManager;
  7. import android.media.MediaPlayer;
  8. import android.os.Binder;
  9. import android.os.IBinder;
  10. import android.support.annotation.Nullable;
  11. import android.util.Log;
  12.  
  13.  
  14. import java.io.IOException;
  15.  
  16. public class service extends Service {
  17.  
  18. private final String TAG = service.class.getSimpleName();
  19.  
  20. MediaPlayer mediaPlayer;
  21.  
  22. IBinder binder = new LocalBinder();
  23.  
  24. @Override
  25. public void onCreate() {
  26. mediaPlayer = new MediaPlayer();
  27. super.onCreate();
  28. }
  29.  
  30. @Nullable
  31. @Override
  32. public IBinder onBind(Intent intent) {
  33. return binder;
  34. }
  35.  
  36. class LocalBinder extends Binder {
  37.  
  38. public service getService() {
  39. Log.i(TAG, "getService: ");
  40. return service.this;
  41. }
  42. }
  43.  
  44. @Override
  45. public void onDestroy() {
  46. super.onDestroy();
  47. Log.i(TAG, "onDestroy: ");
  48. if (mediaPlayer != null) {
  49. mediaPlayer.release();
  50. mediaPlayer = null;
  51. }
  52.  
  53. }
  54.  
  55. public void play(String path) {
  56. Log.i(TAG, "play: ");
  57.  
  58. mediaPlayer.reset();
  59. try {
  60. mediaPlayer.setDataSource(path);
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. }
  64. mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  65. try {
  66. mediaPlayer.prepare();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70. mediaPlayer.start();
  71. }
  72.  
  73. public void playerResume() {
  74. Log.i(TAG, "player resume: ");
  75. mediaPlayer.start();
  76. }
  77.  
  78. public void playerStop() {
  79. Log.i(TAG, "playerstop: ");
  80. mediaPlayer.pause();
  81. }
  82.  
  83. public int playerProgress() {
  84. if (mediaPlayer == null)
  85. return 0;
  86. return mediaPlayer.getCurrentPosition();
  87. }
  88.  
  89. public boolean isPlaying() {
  90. if (mediaPlayer.isPlaying()) return true;
  91. return false;
  92. }
  93.  
  94.  
  95. public void seekTo(int position){
  96. Log.i(TAG, "seekto: ");
  97. mediaPlayer.seekTo(position);
  98. }
  99.  
  100. public int duration() {
  101. if (mediaPlayer == null)
  102. return 0;
  103. return mediaPlayer.getDuration();
  104. }
  105.  
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement