make mp3 player in android studio source code
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:orientation="vertical" tools:context=".Mp3MediaPlayer"> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:gravity="center" android:text="PLAY/STOP SONG.\nSCRUB WITH SEEKBAR" android:textStyle="bold" app:layout_constraintBottom_toTopOf="@+id/seekbar" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <SeekBar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="16dp" app:layout_constraintBottom_toTopOf="@+id/button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView5" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" app:layout_constraintBottom_toTopOf="@+id/seekbar" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <android.support.design.widget.FloatingActionButton android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="16dp" android:layout_marginBottom="32dp" android:src="@android:drawable/ic_media_play" android:text="PLAY SOUND" app:layout_constraintBottom_toTopOf="@+id/adView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/seekbar" /> </android.support.constraint.ConstraintLayout> |
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
import android.content.res.AssetFileDescriptor; import android.media.MediaPlayer; import android.support.design.widget.FloatingActionButton; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.SeekBar; import android.widget.TextView; public class Mp3MediaPlayer extends AppCompatActivity implements Runnable { MediaPlayer mp3player = new MediaPlayer(); SeekBar seekBar; boolean play = false; FloatingActionButton fab; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mp3_media_player); fab = findViewById(R.id.button); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { playSong(); } }); final TextView seekBarHint = findViewById(R.id.textView); seekBar = findViewById(R.id.seekbar); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onStartTrackingTouch(SeekBar seekBar) { seekBarHint.setVisibility(View.VISIBLE); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { seekBarHint.setVisibility(View.VISIBLE); int x = (int) Math.ceil(progress / 1000f); if (x < 10) seekBarHint.setText("0:0" + x); else seekBarHint.setText("0:" + x); double percent = progress / (double) seekBar.getMax(); int offset = seekBar.getThumbOffset(); int seekWidth = seekBar.getWidth(); int val = (int) Math.round(percent * (seekWidth - 2 * offset)); int labelWidth = seekBarHint.getWidth(); seekBarHint.setX(offset + seekBar.getX() + val - Math.round(percent * offset) - Math.round(percent * labelWidth / 2)); if (progress > 0 && mp3player != null && !mp3player.isPlaying()) { clearMediaPlayer(); fab.setImageDrawable(ContextCompat.getDrawable(Mp3MediaPlayer.this, android.R.drawable.ic_media_play)); Mp3MediaPlayer.this.seekBar.setProgress(0); } } @Override public void onStopTrackingTouch(SeekBar seekBar) { if (mp3player != null && mp3player.isPlaying()) { mp3player.seekTo(seekBar.getProgress()); } } }); } public void playSong() { try { if (mp3player != null && mp3player.isPlaying()) { clearMediaPlayer(); seekBar.setProgress(0); play = true; fab.setImageDrawable(ContextCompat.getDrawable(Mp3MediaPlayer.this, android.R.drawable.ic_media_play)); } if (!play) { if (mp3player == null) { mp3player = new MediaPlayer(); } fab.setImageDrawable(ContextCompat.getDrawable(Mp3MediaPlayer.this, android.R.drawable.ic_media_pause)); AssetFileDescriptor descriptor = getAssets().openFd("50_Cal_Shells_Drop.mp3"); mp3player.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength()); descriptor.close(); mp3player.prepare(); mp3player.setVolume(0.5f, 0.5f); mp3player.setLooping(false); seekBar.setMax(mp3player.getDuration()); mp3player.start(); new Thread(this).start(); } play = false; } catch (Exception e) { e.printStackTrace(); } } public void run() { int currentPosition = mp3player.getCurrentPosition(); int total = mp3player.getDuration(); while (mp3player != null && mp3player.isPlaying() && currentPosition < total) { try { Thread.sleep(1000); currentPosition = mp3player.getCurrentPosition(); } catch (InterruptedException e) { return; } catch (Exception e) { return; } seekBar.setProgress(currentPosition); } } @Override protected void onDestroy() { super.onDestroy(); clearMediaPlayer(); } private void clearMediaPlayer() { mp3player.stop(); mp3player.release(); mp3player = null; } } |