Advertisement
bestarmcong

MediaManager

Sep 21st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. package tc.com.nicemp3player;
  2.  
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.media.MediaPlayer;
  6. import android.provider.MediaStore;
  7.  
  8. import java.io.IOException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. /**
  14. * Created by thanh on 19-Jul-16.
  15. */
  16. public class MediaManager {
  17. public static int STATE_IDLE = -1;
  18. public static int STATE_PLAYING = 1;
  19. public static int STATE_PAUSE = 2;
  20. public static int STATE_STOP = 3;
  21. public static int STATE_SEEKING = 4;
  22. private static final String TAG = "TAG";
  23. // private Context mContext;
  24. private MediaPlayer mediaPlayer;
  25. private List<Song> listSong = new ArrayList<>();
  26. private int mediaState;
  27. private int indexSong = 0;
  28. private OnPlayStartedListener listener;
  29.  
  30. public void setIndexSong(int indexSong){
  31. this.indexSong = indexSong;
  32. }
  33.  
  34. public int getIndexSong(){
  35. return indexSong;
  36. }
  37.  
  38. public MediaManager(Context context){
  39. // mContext = context;
  40. getListSong(context);
  41. mediaPlayer = new MediaPlayer();
  42. mediaState = STATE_IDLE;
  43. mediaPlayer.setOnCompletionListener((MediaPlayer.OnCompletionListener)context);
  44. listener = (OnPlayStartedListener)context;
  45. }
  46.  
  47. public List<Song> getSong(){
  48. return listSong;
  49. }
  50.  
  51. public void playOrPause(boolean isClickItemView){
  52. if (mediaState == STATE_IDLE || mediaState == STATE_STOP||(mediaState==STATE_PLAYING&&isClickItemView)){
  53. try {
  54. mediaPlayer.reset();
  55. Song song = listSong.get(indexSong);
  56. mediaPlayer.setDataSource(song.getPath());
  57. // mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  58. mediaPlayer.prepare();
  59. // mediaPlayer.setLooping(false);
  60. mediaPlayer.start();
  61. listener.onMediaStarted(song.getName(),song.getArtist(),song.getTime(),mediaPlayer.getDuration());
  62. mediaState = STATE_PLAYING;
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. }else if (mediaState == STATE_PLAYING){
  67. mediaPlayer.pause();
  68. mediaState = STATE_PAUSE;
  69. }
  70. else if (mediaState==STATE_PAUSE) {
  71. mediaPlayer.start();
  72. mediaState = STATE_PLAYING;
  73. }
  74. }
  75.  
  76. public void next(){
  77. if (indexSong<listSong.size()-1){
  78. indexSong++;
  79. }else {
  80. indexSong = 0;
  81. }
  82. if (mediaState==STATE_PLAYING){
  83. playOrPause(true);
  84. }else{
  85. Song song = listSong.get(indexSong);
  86. listener.onMediaStarted(song.getName(),song.getArtist(),song.getTime(),mediaPlayer.getDuration());
  87. mediaState = STATE_STOP;
  88. }
  89. }
  90.  
  91. public void previous(){
  92. if (indexSong>0){
  93. indexSong--;
  94. }else {
  95. indexSong = listSong.size()-1;
  96. }
  97. if (mediaState==STATE_PLAYING){
  98. playOrPause(true);
  99. }else{
  100. Song song = listSong.get(indexSong);
  101. listener.onMediaStarted(song.getName(), song.getArtist(), song.getTime(), mediaPlayer.getDuration());
  102. mediaState = STATE_STOP;
  103. }
  104.  
  105. }
  106.  
  107. public void stop(){
  108. if (mediaState==STATE_IDLE){
  109. return;
  110. }
  111. mediaPlayer.stop();
  112. Song song = listSong.get(indexSong);
  113. listener.onMediaStarted(song.getName(), song.getArtist(), song.getTime(), mediaPlayer.getDuration());
  114. mediaState = STATE_STOP;
  115. }
  116.  
  117. public void seekTo(int progress){
  118. mediaPlayer.seekTo(progress);
  119. }
  120.  
  121. private void getListSong(Context context){
  122. if (listSong.size()>0){
  123. return;
  124. }
  125. String projection[] = new String[]{
  126. //name
  127. MediaStore.MediaColumns.TITLE,
  128. //file name
  129. MediaStore.MediaColumns.DISPLAY_NAME,
  130. //path
  131. MediaStore.MediaColumns.DATA,
  132. MediaStore.Audio.Media.ARTIST,
  133. MediaStore.Audio.Media.ALBUM,
  134. MediaStore.Audio.Media.DURATION
  135. };
  136. Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  137. projection,
  138. null,
  139. null,
  140. null);
  141. if (cursor == null){
  142. return;
  143. }else {
  144. cursor.moveToFirst();
  145. while (cursor.isAfterLast()==false){
  146. String name = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE));
  147. String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME));
  148. String path = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
  149. String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
  150. String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
  151. int duration = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
  152. listSong.add(new Song(name, fileName, path, artist, album, duration));
  153. cursor.moveToNext();
  154. }
  155. cursor.close();
  156. }
  157. }
  158.  
  159. public int getCurrentTime() {
  160. if (mediaState==STATE_PLAYING){
  161. return mediaPlayer.getCurrentPosition();
  162. }
  163. return -1;
  164. }
  165.  
  166. public int getCurrentPosition(){
  167. return mediaPlayer.getCurrentPosition();
  168. }
  169.  
  170. public interface OnPlayStartedListener{
  171. public void onMediaStarted(String nameSong,String singer,String totalTime, int totalTimeInt);
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement