Created
November 8, 2015 05:22
-
-
Save ar-android/e98180de80c777872b40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.ocit.musicandvideostream.activity; | |
| import android.media.AudioManager; | |
| import android.media.MediaPlayer; | |
| import android.os.Bundle; | |
| import android.os.Handler; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.util.Log; | |
| import android.view.MotionEvent; | |
| import android.view.View; | |
| import android.widget.Button; | |
| import android.widget.ImageButton; | |
| import android.widget.RelativeLayout; | |
| import android.widget.SeekBar; | |
| import com.ocit.musicandvideostream.R; | |
| import com.ocit.musicandvideostream.model.Song; | |
| import java.util.ArrayList; | |
| public class MusicActivity extends AppCompatActivity implements | |
| View.OnTouchListener, View.OnClickListener, MediaPlayer.OnBufferingUpdateListener { | |
| private Button btnPlay; | |
| private SeekBar seekBarProgress; | |
| private MediaPlayer player; | |
| private int mediaFileLengthInMilliseconds; | |
| private final Handler handler = new Handler(); | |
| private RelativeLayout playPauseButtonBackground; | |
| private ImageButton playPauseButton; | |
| private boolean isPlaying = false; | |
| private SeekBar nowPlayingSeekBar; | |
| private int position; | |
| private ArrayList<Song> mSongs; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_music); | |
| position = getIntent().getIntExtra("position", 0); | |
| mSongs = new ArrayList<>(); | |
| seekBarProgress = (SeekBar) findViewById(R.id.seekBar); | |
| seekBarProgress.setMax(99); | |
| seekBarProgress.setOnTouchListener(this); | |
| playPauseButtonBackground = (RelativeLayout) findViewById(R.id.playPauseButtonBackground); | |
| playPauseButtonBackground.setOnClickListener(this); | |
| playPauseButton = (ImageButton) findViewById(R.id.playPauseButton); | |
| playPauseButton.setOnClickListener(this); | |
| // | |
| // nowPlayingSeekBar = (SeekBar) findViewById(R.id.nowPlayingSeekBar); | |
| // nowPlayingSeekBar.setMax(99); | |
| // nowPlayingSeekBar.setOnTouchListener(this); | |
| } | |
| /** | |
| * Method which updates the SeekBar primary progress by current song playing position | |
| */ | |
| private void primarySeekBarProgressUpdater() { | |
| nowPlayingSeekBar.setProgress((int) (((float) player.getCurrentPosition() / mediaFileLengthInMilliseconds) * 100)); | |
| if (player.isPlaying()) { | |
| Runnable notification = new Runnable() { | |
| public void run() { | |
| primarySeekBarProgressUpdater(); | |
| } | |
| }; | |
| handler.postDelayed(notification, 1000); | |
| } | |
| } | |
| @Override | |
| public boolean onTouch(View v, MotionEvent event) { | |
| if (v.getId() == R.id.nowPlayingSeekBar) { | |
| /** | |
| * Seekbar onTouch event handler. | |
| * Method which seeks MediaPlayer to seekBar primary progress position | |
| */ | |
| if (player.isPlaying()) { | |
| SeekBar sb = (SeekBar) v; | |
| int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress(); | |
| player.seekTo(playPositionInMillisecconds); | |
| } | |
| } | |
| return false; | |
| } | |
| @Override | |
| public void onBufferingUpdate(MediaPlayer mp, int percent) { | |
| /** | |
| * Method which updates the SeekBar secondary progress by current song loading from URL position | |
| */ | |
| nowPlayingSeekBar.setSecondaryProgress(percent); | |
| } | |
| @Override | |
| public void onClick(View v) { | |
| if (v.getId() == R.id.btnPlay) { | |
| mediaFileLengthInMilliseconds = player.getDuration(); // gets the song length in milliseconds from URL | |
| primarySeekBarProgressUpdater(); | |
| } else if (v.getId() == R.id.playPauseButton) { | |
| playController(); | |
| } | |
| } | |
| void playController() { | |
| try { | |
| player = new MediaPlayer(); | |
| player.setAudioStreamType(AudioManager.STREAM_MUSIC); | |
| player.setDataSource(mSongs.get(position).getTitle()); | |
| player.prepare(); | |
| player.start(); | |
| } catch (Exception e) { | |
| Log.d("eror play music", e.toString()); | |
| } | |
| if (isPlaying == false) { | |
| isPlaying = true; | |
| playPauseButton.setImageResource(R.drawable.pause_light); | |
| mediaFileLengthInMilliseconds = player.getDuration(); | |
| primarySeekBarProgressUpdater(); | |
| } else { | |
| playPauseButton.setImageResource(R.drawable.play_light); | |
| player.pause(); | |
| isPlaying = false; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment