diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 778858b..7e680c2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -11,7 +11,7 @@ android:supportsRtl="true" android:theme="@style/AppTheme" tools:ignore="GoogleAppIndexingWarning"> - + @@ -19,7 +19,7 @@ diff --git a/app/src/main/java/com/example/quangtran/listenmusic/Model/SongModel.java b/app/src/main/java/com/example/quangtran/listenmusic/Model/SongModel.java deleted file mode 100644 index cd6ec93..0000000 --- a/app/src/main/java/com/example/quangtran/listenmusic/Model/SongModel.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.example.quangtran.listenmusic.Model; - -public class SongModel { - public String getPath() { - return path; - } - - public void setPath(String path) { - this.path = path; - } - - public String path; -} diff --git a/app/src/main/java/com/example/quangtran/listenmusic/View/MainActivity.java b/app/src/main/java/com/example/quangtran/listenmusic/View/MainActivity.java deleted file mode 100644 index 4d68f44..0000000 --- a/app/src/main/java/com/example/quangtran/listenmusic/View/MainActivity.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.example.quangtran.listenmusic.View; - -import android.content.Intent; -import android.media.MediaPlayer; -import android.support.v7.app.AppCompatActivity; -import android.os.Bundle; -import android.view.View; -import android.widget.ImageButton; -import android.widget.Toast; - -import com.example.quangtran.listenmusic.R; - -import java.io.IOException; - -public class MainActivity extends AppCompatActivity implements View.OnClickListener { - private static int[] mRawID = {R.raw.songone, R.raw.songtwo, R.raw.songthree}; - private static int mcurrentSongIndex = 0; - MediaPlayer mediaPlayer; - ImageButton mImagePlay, mImageNext, mImageBack; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - mImageBack = findViewById(R.id.imageBack); - mImagePlay = findViewById(R.id.imagePlay); - mImageNext = findViewById(R.id.imageNext); - mImagePlay.setOnClickListener(this); - mImageBack.setOnClickListener(this); - mImageNext.setOnClickListener(this); - mediaPlayer = MediaPlayer.create(this, mRawID[0]); - - } - - @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.imagePlay: - if (mediaPlayer.isPlaying()) { - if (mediaPlayer != null) { - mediaPlayer.pause(); - break; - } - - } else { - if (mediaPlayer != null) { - mediaPlayer.start(); - break; - } - } - break; - - case R.id.imageBack: - if (mcurrentSongIndex > 0) { - mcurrentSongIndex = mcurrentSongIndex - 1; - PlaySong(mcurrentSongIndex); - } else { - mcurrentSongIndex = mRawID.length - 1; - PlaySong(mcurrentSongIndex); - } - break; - - case R.id.imageNext: - if (mcurrentSongIndex < mRawID.length - 1) { - mcurrentSongIndex = mcurrentSongIndex + 1; - PlaySong(mcurrentSongIndex); - } else { - mcurrentSongIndex = 0; - PlaySong(mcurrentSongIndex); - - } - break; - } - } - - private void PlaySong(int IDTrack) { - mediaPlayer.reset(); - mediaPlayer = MediaPlayer.create(this, mRawID[IDTrack]); - mediaPlayer.start(); - } - - @Override - protected void onResume() { - super.onResume(); - StopService(); - } - - private void StopService() { - Intent startService = new Intent(this, ServicePlayMusic.class); - stopService(startService); - } - - @Override - protected void onStop() { - super.onStop(); - //mediaPlayer.pause(); - StartService(); - } - - private void StartService() { - Intent startService = new Intent(this, ServicePlayMusic.class); - startService.putExtra("currentIndexSong", mcurrentSongIndex); - startService(startService); - } -} diff --git a/app/src/main/java/com/example/quangtran/listenmusic/View/ServicePlayMusic.java b/app/src/main/java/com/example/quangtran/listenmusic/View/ServicePlayMusic.java deleted file mode 100644 index ac0b5cd..0000000 --- a/app/src/main/java/com/example/quangtran/listenmusic/View/ServicePlayMusic.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.example.quangtran.listenmusic.View; - -import android.app.IntentService; -import android.app.Service; -import android.content.Intent; -import android.media.MediaPlayer; -import android.os.Bundle; -import android.os.IBinder; -import android.support.annotation.Nullable; -import android.widget.Toast; - -import com.example.quangtran.listenmusic.R; - -public class ServicePlayMusic extends Service{ - MediaPlayer mediaPlayer; - private static int[] mRawID = {R.raw.songone, R.raw.songtwo, R.raw.songthree}; - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - Bundle bundle = intent.getExtras(); - int current = bundle.getInt("currentIndexSong"); - PlaySong(current); - Toast.makeText(this, current + " ", Toast.LENGTH_SHORT).show(); - return flags; - } - - @Nullable - @Override - public IBinder onBind(Intent intent) { - return null; - } - - private void PlaySong(int IDTrack) { - //mediaPlayer.reset(); - mediaPlayer = MediaPlayer.create(this, mRawID[IDTrack]); - mediaPlayer.start(); - } - - -} diff --git a/app/src/main/java/com/example/quangtran/listenmusic/util/Util.java b/app/src/main/java/com/example/quangtran/listenmusic/util/Util.java new file mode 100644 index 0000000..da12ce5 --- /dev/null +++ b/app/src/main/java/com/example/quangtran/listenmusic/util/Util.java @@ -0,0 +1,36 @@ +package com.example.quangtran.listenmusic.util; + +import java.text.SimpleDateFormat; +import java.util.Locale; + +public class Util { + private static final int MILLITIME = 1000; + private static final int HOURS_TO_SECONDS = 3600; + private static final int HUNDRED = 100; + private static final int ONE_HOUR = 60; + private static final String HOUR_DEFAULT = "00:00"; + private static final String TIME_FORMAT = "mm:ss"; + + public String TotalTime(long milliseconds) { + SimpleDateFormat dateFormat = new SimpleDateFormat(TIME_FORMAT, Locale.US); + return dateFormat.format(milliseconds); + } + + //update progressbar + public static int getProgressPercentage(long currentDuration, long totalDuration) { + Double percentage = (double) 0; + long currentSeconds = (int) (currentDuration / MILLITIME); + long totalSeconds = (int) (totalDuration / MILLITIME); + double phantram = (double) (currentSeconds * HUNDRED / totalSeconds); + percentage = ((double) (currentSeconds * HUNDRED / totalSeconds)); + return percentage.intValue(); + } + + //progress tracking then song stop here + public static int progressToTimer(int progress, int totalDuration) { + int currentDuration = 0; + totalDuration = totalDuration / MILLITIME; + currentDuration = (int) ((((double) progress) / HUNDRED) * totalDuration); + return currentDuration * MILLITIME; + } +} diff --git a/app/src/main/java/com/example/quangtran/listenmusic/view/MainActivity.java b/app/src/main/java/com/example/quangtran/listenmusic/view/MainActivity.java new file mode 100644 index 0000000..5125a4f --- /dev/null +++ b/app/src/main/java/com/example/quangtran/listenmusic/view/MainActivity.java @@ -0,0 +1,180 @@ +package com.example.quangtran.listenmusic.view; + +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.Bundle; +import android.os.IBinder; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.ImageButton; +import android.widget.SeekBar; +import android.widget.TextView; + +import com.example.quangtran.listenmusic.R; +import com.example.quangtran.listenmusic.util.Util; + +public class MainActivity extends AppCompatActivity implements View.OnClickListener + , SeekBar.OnSeekBarChangeListener { + private static final String TITLE_NOTIFICATION = "Media Artist"; + private static final String CONTENT_NOTIFICATION = "thuong thuc am nhac"; + private static final int REQUEST_CODE = 1; + private static final int NOTIFICATION_ID = 1; + private static final int PROGRESS_START = 0; + private static final int PROGRESS_MAX = 100; + private SeekBar mProgressMusic; + private ImageButton mImagePlay, mImageNext, mImageBack; + private TextView mCurrentTime, mTotalTime; + private Util mUtil; + private android.os.Handler mHandler = new android.os.Handler(); + private ServicePlayMusic mServicePlayMusic; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + mImageBack = findViewById(R.id.image_back); + mImagePlay = findViewById(R.id.image_play); + mImageNext = findViewById(R.id.image_next); + mImagePlay.setOnClickListener(this); + mImageBack.setOnClickListener(this); + mImageNext.setOnClickListener(this); + mCurrentTime = findViewById(R.id.text_current_time); + mTotalTime = findViewById(R.id.text_total_time); + mProgressMusic = findViewById(R.id.seek_bar_music); + mProgressMusic.setProgress(PROGRESS_START); + mProgressMusic.setMax(PROGRESS_MAX); + mProgressMusic.setOnSeekBarChangeListener(this); + startService(); + buildNotification(); + } + + private ServiceConnection mConnection = new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName componentName, IBinder iBinder) { + ServicePlayMusic.LocalBinder binder = (ServicePlayMusic.LocalBinder) iBinder; + mServicePlayMusic = binder.getService(); + updateUI(); + } + + @Override + public void onServiceDisconnected(ComponentName componentName) { + unbindService(mConnection); + } + }; + + private void startService() { + Intent startService = new Intent(this, ServicePlayMusic.class); + bindService(startService, mConnection, BIND_AUTO_CREATE); + startService(startService); + } + + private void buildNotification() { + Intent intentNotification = new Intent(getApplicationContext(), MainActivity.class); + PendingIntent pendingIntent = PendingIntent + .getService(getApplicationContext(), REQUEST_CODE, intentNotification, 0); + Notification.Builder builder = new Notification.Builder(this) + .setSmallIcon(R.drawable.music_icon) + .setContentTitle(TITLE_NOTIFICATION) + .setContentText(CONTENT_NOTIFICATION) + .setDeleteIntent(pendingIntent); + NotificationManager notificationManager = (NotificationManager) + getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.notify(NOTIFICATION_ID, builder.build()); + } + + private void updateUI() { + if (mServicePlayMusic.mediaPlayer != null) { + updateProgressBar(); + if (mServicePlayMusic.mediaPlayer.isPlaying()) { + mImagePlay.setImageResource(R.drawable.ic_pause); + } else { + mImagePlay.setImageResource(R.drawable.ic_play_button); + } + } + } + + private Runnable mUpdateTimes = (new Runnable() { + @Override + public void run() { + mUtil = new Util(); + long currentTime = mServicePlayMusic.mediaPlayer.getCurrentPosition(); + long totalTime = mServicePlayMusic.mediaPlayer.getDuration(); + mCurrentTime.setText(mUtil.TotalTime(currentTime)); + mTotalTime.setText(mUtil.TotalTime(totalTime)); + //update progressbar + int progress = (int) (Util.getProgressPercentage(currentTime, totalTime)); + mProgressMusic.setProgress(progress); + if (mProgressMusic.getProgress() == mProgressMusic.getMax()) { + mServicePlayMusic.nextSong(); + } + mHandler.postDelayed(this, PROGRESS_MAX);//de quy + } + }); + + @Override + public void onClick(View view) { + switch (view.getId()) { + case R.id.image_play: + updatePlay(); + break; + case R.id.image_back: + mServicePlayMusic.backSong(); + mImagePlay.setImageResource(R.drawable.ic_pause); + break; + + case R.id.image_next: + mServicePlayMusic.nextSong(); + mImagePlay.setImageResource(R.drawable.ic_pause); + break; + default: + break; + } + } + + private void updatePlay() { + if (mServicePlayMusic.mediaPlayer.isPlaying()) { + mServicePlayMusic.play(); + mImagePlay.setImageResource(R.drawable.ic_play_button); + } else { + mServicePlayMusic.play(); + mImagePlay.setImageResource(R.drawable.ic_pause); + } + } + + private void updateProgressBar() { + mHandler.postDelayed(mUpdateTimes, PROGRESS_MAX); + } + + @Override + public void onProgressChanged(SeekBar seekBar, int i, boolean b) { + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + mHandler.removeCallbacks(mUpdateTimes); + if (mServicePlayMusic.mediaPlayer != null) { + int totalDuration = mServicePlayMusic.mediaPlayer.getDuration(); + int progress = mProgressMusic.getProgress(); + int current = Util.progressToTimer(progress, totalDuration); + mServicePlayMusic.mediaPlayer.seekTo(current); + updateProgressBar(); + } + } + + @Override + protected void onDestroy() { + super.onDestroy(); + NotificationManager notificationManager = (NotificationManager) + getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.cancel(NOTIFICATION_ID); + } +} diff --git a/app/src/main/java/com/example/quangtran/listenmusic/view/ServicePlayMusic.java b/app/src/main/java/com/example/quangtran/listenmusic/view/ServicePlayMusic.java new file mode 100644 index 0000000..101c4d4 --- /dev/null +++ b/app/src/main/java/com/example/quangtran/listenmusic/view/ServicePlayMusic.java @@ -0,0 +1,108 @@ +package com.example.quangtran.listenmusic.view; + +import android.app.Service; +import android.content.Intent; +import android.media.MediaPlayer; +import android.os.Binder; +import android.os.IBinder; +import android.support.annotation.Nullable; + +import com.example.quangtran.listenmusic.R; + +public class ServicePlayMusic extends Service { + private static int mCurrentSongIndex = 0; + private static final int START_ID = 1; + private final IBinder mIBinder = new LocalBinder(); + public MediaPlayer mediaPlayer; + private static int[] mRawID = {R.raw.songone + , R.raw.songtwo + , R.raw.songthree + , R.raw.songfour}; + + public ServicePlayMusic() { + } + + + @Nullable + @Override + public IBinder onBind(Intent intent) { + return mIBinder; + } + + public class LocalBinder extends Binder { + public ServicePlayMusic getService() { + return ServicePlayMusic.this; + } + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + //kiem tra so lan service khoi tao lai + if (startId == START_ID) { + mediaPlayer = MediaPlayer.create(this, mRawID[0]); + + } + return START_STICKY; + } + + public void playSong(int id) { + if (mediaPlayer != null) { + mediaPlayer.reset(); + } + mediaPlayer = MediaPlayer.create(this, mRawID[id]); + mediaPlayer.start(); + } + + public void play() { + if (mediaPlayer.isPlaying()) { + if (mediaPlayer != null) { + mediaPlayer.pause(); + } + } else { + if (mediaPlayer != null) { + mediaPlayer.start(); + } + } + } + + public void nextSong() { + if (mCurrentSongIndex < (mRawID.length - 1)) { + playSong(mCurrentSongIndex + 1); + mCurrentSongIndex = mCurrentSongIndex + 1; + } else { + playSong(0); + mCurrentSongIndex = 0; + } + } + + public void backSong() { + if (mCurrentSongIndex > 0) { + playSong(mCurrentSongIndex - 1); + mCurrentSongIndex = mCurrentSongIndex - 1; + } else { + //play last song + playSong(mRawID.length - 1); + mCurrentSongIndex = mRawID.length - 1; + } + } + + + public static void setCurrentSongIndex(int mcurrentSongIndex) { + ServicePlayMusic.mCurrentSongIndex = mcurrentSongIndex; + } + + public static int getCurrentSongIndex() { + return mCurrentSongIndex; + } + + @Override + public void onDestroy() { + super.onDestroy(); + stopSelf(); + } + + @Override + public boolean onUnbind(Intent intent) { + return true; + } +} diff --git a/app/src/main/res/drawable/ic_fast_forward.xml b/app/src/main/res/drawable/ic_fast_forward.xml new file mode 100644 index 0000000..b96be26 --- /dev/null +++ b/app/src/main/res/drawable/ic_fast_forward.xml @@ -0,0 +1,5 @@ + + + + diff --git a/app/src/main/res/drawable/ic_pause.xml b/app/src/main/res/drawable/ic_pause.xml new file mode 100644 index 0000000..fa0abda --- /dev/null +++ b/app/src/main/res/drawable/ic_pause.xml @@ -0,0 +1,6 @@ + + + + + diff --git a/app/src/main/res/drawable/ic_play_button.xml b/app/src/main/res/drawable/ic_play_button.xml new file mode 100644 index 0000000..4b809a6 --- /dev/null +++ b/app/src/main/res/drawable/ic_play_button.xml @@ -0,0 +1,5 @@ + + + + diff --git a/app/src/main/res/drawable/ic_rewind.xml b/app/src/main/res/drawable/ic_rewind.xml new file mode 100644 index 0000000..7bf5603 --- /dev/null +++ b/app/src/main/res/drawable/ic_rewind.xml @@ -0,0 +1,5 @@ + + + + diff --git a/app/src/main/res/drawable/music_icon.png b/app/src/main/res/drawable/music_icon.png new file mode 100644 index 0000000..aa9a8ed Binary files /dev/null and b/app/src/main/res/drawable/music_icon.png differ diff --git a/app/src/main/res/drawable/play.png b/app/src/main/res/drawable/play.png deleted file mode 100644 index 1b6d64b..0000000 Binary files a/app/src/main/res/drawable/play.png and /dev/null differ diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index d5eee04..1643ce6 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -4,20 +4,19 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".View.MainActivity" - tools:layout_editor_absoluteY="81dp"> - + tools:context=".view.MainActivity"> + app:layout_constraintTop_toBottomOf="@+id/image_play" + app:layout_constraintVertical_bias="0.355" + tools:ignore="MissingConstraints" /> diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml new file mode 100644 index 0000000..7db224a --- /dev/null +++ b/app/src/main/res/values/dimens.xml @@ -0,0 +1,11 @@ + + + 80dp + 8dp + 220dp + 388dp + 289dp + 16dp + 64dp + 15dp + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0f6291e..d29a46a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,4 @@ ListenMusic + 00:00