Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.99 KB | None | 0 0
  1.   btnPlay = (ImageButton) findViewById(R.id.btnPlay);
  2.     btnForward = (ImageButton) findViewById(R.id.btnForward);
  3.     btnBackward = (ImageButton) findViewById(R.id.btnBackward);
  4.     btnPlaylist = (ImageButton) findViewById(R.id.btnPlaylist);
  5.     btnRepeat = (ImageButton) findViewById(R.id.btnRepeat);
  6.     btnShuffle = (ImageButton) findViewById(R.id.btnShuffle);
  7.     songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
  8.     songTitleLabel = (TextView) findViewById(R.id.songTitle);
  9.     songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
  10.     songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);
  11.  
  12.     // Mediaplayer
  13.     mp = new MediaPlayer();
  14.     songManager = new SongsManager();
  15.     utils = new Utilities();
  16.  
  17.     // Listeners
  18.     songProgressBar.setOnSeekBarChangeListener(this); // Important
  19.     mp.setOnCompletionListener(this); // Important
  20.  
  21.     // Getting all songs list
  22.     songsList = songManager.getPlayList();
  23.  
  24.     // By default play first song
  25.     playSong(0);
  26.  
  27.     /**
  28.      * Play button click event plays a song and changes button to pause
  29.      * image pauses a song and changes button to play image
  30.      * */
  31.     btnPlay.setOnClickListener(new View.OnClickListener() {
  32.  
  33.         @Override
  34.         public void onClick(View arg0) {
  35.             // check for already playing
  36.             if (mp.isPlaying()) {
  37.                 if (mp != null) {
  38.                     mp.pause();
  39.                     // Changing button image to play button
  40.                     btnPlay.setImageResource(R.drawable.btn_play);
  41.                 }
  42.             } else {
  43.                 // Resume song
  44.                 if (mp != null) {
  45.                     mp.start();
  46.                     // Changing button image to pause button
  47.                     btnPlay.setImageResource(R.drawable.btn_pause);
  48.                 }
  49.             }
  50.  
  51.         }
  52.     });
  53.  
  54.     btnForward.setOnLongClickListener(new OnLongClickListener() {
  55.  
  56.         @Override
  57.         public boolean onLongClick(View v) {
  58.             // get current song position
  59.             int currentPosition = mp.getCurrentPosition();
  60.             // check if seekForward time is lesser than song duration
  61.             if (currentPosition + seekForwardTime <= mp.getDuration()) {
  62.                 // forward song
  63.                 mp.seekTo(currentPosition + seekForwardTime);
  64.             } else {
  65.                 // forward to end position
  66.                 mp.seekTo(mp.getDuration());
  67.             }
  68.             return false;
  69.         }
  70.     });
  71.  
  72.     /**
  73.      * Forward button click event Forwards song specified seconds
  74.      * */
  75.     btnForward.setOnClickListener(new View.OnClickListener() {
  76.  
  77.         @Override
  78.         public void onClick(View arg0) {
  79.             // get current song position
  80.             // check if next song is there or not
  81.             if (currentSongIndex < (songsList.size() - 1)) {
  82.                 playSong(currentSongIndex + 1);
  83.                 currentSongIndex = currentSongIndex + 1;
  84.             } else {
  85.                 // play first song
  86.                 playSong(0);
  87.                 currentSongIndex = 0;
  88.             }
  89.         }
  90.     });
  91.  
  92.     btnBackward.setOnLongClickListener(new OnLongClickListener() {
  93.  
  94.         @Override
  95.         public boolean onLongClick(View v) {
  96.             int currentPosition = mp.getCurrentPosition();
  97.             // check if seekBackward time is greater than 0 sec
  98.             if (currentPosition - seekBackwardTime >= 0) {
  99.                 // forward song
  100.                 mp.seekTo(currentPosition - seekBackwardTime);
  101.             } else {
  102.                 // backward to starting position
  103.                 mp.seekTo(0);
  104.             }
  105.             return false;
  106.         }
  107.     });
  108.  
  109.     /**
  110.      * Backward button click event Backward song to specified seconds
  111.      * */
  112.     btnBackward.setOnClickListener(new View.OnClickListener() {
  113.  
  114.         @Override
  115.         public void onClick(View arg0) {
  116.             if (currentSongIndex > 0) {
  117.                 playSong(currentSongIndex - 1);
  118.                 currentSongIndex = currentSongIndex - 1;
  119.             } else {
  120.                 // play last song
  121.                 playSong(songsList.size() - 1);
  122.                 currentSongIndex = songsList.size() - 1;
  123.             }
  124.  
  125.         }
  126.     });
  127.  
  128.     /**
  129.      * Button Click event for Repeat button Enables repeat flag to true
  130.      * */
  131.     btnRepeat.setOnClickListener(new View.OnClickListener() {
  132.  
  133.         @Override
  134.         public void onClick(View arg0) {
  135.             if (isRepeat) {
  136.                 isRepeat = false;
  137.                 Toast.makeText(getApplicationContext(), "Repeat is OFF",
  138.                         Toast.LENGTH_SHORT).show();
  139.                 btnRepeat.setImageResource(R.drawable.btn_repeat);
  140.             } else {
  141.                 // make repeat to true
  142.                 isRepeat = true;
  143.                 Toast.makeText(getApplicationContext(), "Repeat is ON",
  144.                         Toast.LENGTH_SHORT).show();
  145.                 // make shuffle to false
  146.                 isShuffle = false;
  147.                 btnRepeat.setImageResource(R.drawable.btn_repeat_focused);
  148.                 btnShuffle.setImageResource(R.drawable.btn_shuffle);
  149.             }
  150.         }
  151.     });
  152.  
  153.     /**
  154.      * Button Click event for Shuffle button Enables shuffle flag to true
  155.      * */
  156.     btnShuffle.setOnClickListener(new View.OnClickListener() {
  157.  
  158.         @Override
  159.         public void onClick(View arg0) {
  160.             if (isShuffle) {
  161.                 isShuffle = false;
  162.                 Toast.makeText(getApplicationContext(), "Shuffle is OFF",
  163.                         Toast.LENGTH_SHORT).show();
  164.                 btnShuffle.setImageResource(R.drawable.btn_shuffle);
  165.             } else {
  166.                 // make repeat to true
  167.                 isShuffle = true;
  168.                 Toast.makeText(getApplicationContext(), "Shuffle is ON",
  169.                         Toast.LENGTH_SHORT).show();
  170.                 // make shuffle to false
  171.                 isRepeat = false;
  172.                 btnShuffle.setImageResource(R.drawable.btn_shuffle_focused);
  173.                 btnRepeat.setImageResource(R.drawable.btn_repeat);
  174.             }
  175.         }
  176.     });
  177.  
  178.     /**
  179.      * Button Click event for Play list click event Launches list activity
  180.      * which displays list of songs
  181.      * */
  182.     btnPlaylist.setOnClickListener(new View.OnClickListener() {
  183.  
  184.         @Override
  185.         public void onClick(View arg0) {
  186.             Intent i = new Intent(getApplicationContext(),
  187.                     PlayListActivity.class);
  188.             startActivityForResult(i, 100);
  189.         }
  190.     });
  191.  
  192. }
  193.  
  194. /**
  195.  * Receiving song index from playlist view and play the song
  196.  * */
  197. @Override
  198. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  199.     super.onActivityResult(requestCode, resultCode, data);
  200.     if (resultCode == 100) {
  201.         currentSongIndex = data.getExtras().getInt("songIndex");
  202.         // play selected song
  203.         playSong(currentSongIndex);
  204.     }
  205.  
  206. }
  207.  
  208. /**
  209.  * Function to play a song
  210.  *
  211.  * @param songIndex
  212.  *            - index of song
  213.  * */
  214. public void playSong(int songIndex) {
  215.     // Play song
  216.     try {
  217.         mp.reset();
  218.         mp.setDataSource(songsList.get(songIndex).get("songPath"));
  219.         mp.prepare();
  220.         mp.start();
  221.         // Displaying Song title
  222.         String songTitle = songsList.get(songIndex).get("songTitle");
  223.         songTitleLabel.setText(songTitle);
  224.  
  225.         // Changing Button Image to pause image
  226.         btnPlay.setImageResource(R.drawable.btn_pause);
  227.  
  228.         // set Progress bar values
  229.         songProgressBar.setProgress(0);
  230.         songProgressBar.setMax(100);
  231.  
  232.         // Updating progress bar
  233.         updateProgressBar();
  234.     } catch (IllegalArgumentException e) {
  235.         e.printStackTrace();
  236.     } catch (IllegalStateException e) {
  237.         e.printStackTrace();
  238.     } catch (IOException e) {
  239.         e.printStackTrace();
  240.     }
  241. }
  242.  
  243. /**
  244.  * Update timer on seekbar
  245.  * */
  246. public void updateProgressBar() {
  247.     mHandler.postDelayed(mUpdateTimeTask, 100);
  248. }
  249.  
  250. @Override
  251. public boolean onKeyDown(int keyCode, KeyEvent event) {
  252.     // TODO Auto-generated method stub
  253.     if (keyCode == KeyEvent.KEYCODE_BACK) {
  254.         mHandler.removeCallbacks(mUpdateTimeTask);
  255.         mp.release();
  256.     }
  257.     return super.onKeyDown(keyCode, event);
  258. }
  259.  
  260. /**
  261.  * Background Runnable thread
  262.  * */
  263. private Runnable mUpdateTimeTask = new Runnable() {
  264.     public void run() {
  265.         long totalDuration = mp.getDuration();
  266.         long currentDuration = mp.getCurrentPosition();
  267.  
  268.         // Displaying Total Duration time
  269.         songTotalDurationLabel.setText(""
  270.                 + utils.milliSecondsToTimer(totalDuration));
  271.         // Displaying time completed playing
  272.         songCurrentDurationLabel.setText(""
  273.                 + utils.milliSecondsToTimer(currentDuration));
  274.  
  275.         // Updating progress bar
  276.         int progress = (int) (utils.getProgressPercentage(currentDuration,
  277.                 totalDuration));
  278.         // System.out.println("Progress : "+progress);
  279.         songProgressBar.setProgress(progress);
  280.  
  281.         // Running this thread after 100 milliseconds
  282.         mHandler.postDelayed(this, 100);
  283.     }
  284. };
  285.  
  286. /**
  287.  *
  288.  * */
  289. @Override
  290. public void onProgressChanged(SeekBar seekBar, int progress,
  291.         boolean fromTouch) {
  292.  
  293. }
  294.  
  295. /**
  296.  * When user starts moving the progress handler
  297.  * */
  298. @Override
  299. public void onStartTrackingTouch(SeekBar seekBar) {
  300.     // remove message Handler from updating progress bar
  301.     mHandler.removeCallbacks(mUpdateTimeTask);
  302. }
  303.  
  304. /**
  305.  * When user stops moving the progress hanlder
  306.  * */
  307. @Override
  308. public void onStopTrackingTouch(SeekBar seekBar) {
  309.     mHandler.removeCallbacks(mUpdateTimeTask);
  310.     int totalDuration = mp.getDuration();
  311.     int currentPosition = utils.progressToTimer(seekBar.getProgress(),
  312.             totalDuration);
  313.  
  314.     // forward or backward to certain seconds
  315.     mp.seekTo(currentPosition);
  316.  
  317.     // update timer progress again
  318.     updateProgressBar();
  319. }
  320.  
  321. /**
  322.  * On Song Playing completed if repeat is ON play same song again if shuffle
  323.  * is ON play random song
  324.  * */
  325. @Override
  326. public void onCompletion(MediaPlayer arg0) {
  327.  
  328.     // check for repeat is ON or OFF
  329.     if (isRepeat) {
  330.         // repeat is on play same song again
  331.         playSong(currentSongIndex);
  332.     } else if (isShuffle) {
  333.         // shuffle is on - play a random song
  334.         Random rand = new Random();
  335.         currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
  336.         playSong(currentSongIndex);
  337.     } else {
  338.         // no repeat or shuffle ON - play next song
  339.         if (currentSongIndex < (songsList.size() - 1)) {
  340.             playSong(currentSongIndex + 1);
  341.             currentSongIndex = currentSongIndex + 1;
  342.         } else {
  343.             // play first song
  344.             playSong(0);
  345.             currentSongIndex = 0;
  346.         }
  347.     }
  348. }
  349.  
  350. @Override
  351. public void onDestroy() {
  352.     super.onDestroy();
  353.     mp.release();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement