image
package com.example.viraj.musicapp; import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.Switch; import android.widget.TextView; import java.text.SimpleDateFormat; import java.util.Date; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private MediaPlayer mediaPlayer; private ImageView artistimage; private TextView lefttime, righttime, artistname, songname; private SeekBar seekBar; private Button previousbuttion, playpausebutton, nextbutton; private Thread thread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //---------------------------- setUpUI(); //------------------------------ seekBar.setMax(mediaPlayer.getDuration()); //------------------------------ seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser) { mediaPlayer.seekTo(progress); } //------inside start time formate class set min sdk to 24 on gradle build.gradle modul app SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss"); int currentpos = mediaPlayer.getCurrentPosition(); int duration = mediaPlayer.getDuration(); lefttime.setText(String.valueOf(dateFormat.format(new Date(currentpos)))); righttime.setText(String.valueOf(dateFormat.format(new Date(duration - currentpos)))); //------inside start time formate class } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); //----------------------------------------- } public void setUpUI() { mediaPlayer = new MediaPlayer(); mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.tola); artistimage = (ImageView) findViewById(R.id.id_artistimage); lefttime = (TextView) findViewById(R.id.id_lefttime); righttime = (TextView) findViewById(R.id.id_righttime); seekBar = (SeekBar) findViewById(R.id.id_seekBar); previousbuttion = (Button) findViewById(R.id.id_previousbutton); playpausebutton = (Button) findViewById(R.id.id_playbutton); nextbutton = (Button) findViewById(R.id.id_nextbutton); artistname = (TextView) findViewById(R.id.id_artistname); songname = (TextView) findViewById(R.id.id_songname); previousbuttion.setOnClickListener(this); playpausebutton.setOnClickListener(this); nextbutton.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.id_previousbutton: //code backMusic(); break; case R.id.id_playbutton: //code if (mediaPlayer.isPlaying()) { pauseMusic(); } else { startMusic(); } break; case R.id.id_nextbutton: //code nextMusic(); break; } } //method he public void pauseMusic() { if (mediaPlayer != null) { mediaPlayer.pause(); playpausebutton.setBackgroundResource(android.R.drawable.ic_media_play); } } public void startMusic() { if (mediaPlayer != null) { mediaPlayer.start(); updateThread(); playpausebutton.setBackgroundResource(android.R.drawable.ic_media_pause); } } public void backMusic() { if (mediaPlayer.isPlaying()) { //for no mediaPlayer.seekTo(0); } } public void nextMusic() { if (mediaPlayer.isPlaying()) { mediaPlayer.seekTo(mediaPlayer.getDuration()-100); } } public void updateThread() { thread = new Thread() { @Override public void run() { try { while (mediaPlayer != null && mediaPlayer.isPlaying()) { Thread.sleep(50); runOnUiThread(new Runnable() { @Override public void run() { int newPositio = mediaPlayer.getCurrentPosition(); int newMax = mediaPlayer.getDuration(); seekBar.setMax(newMax); seekBar.setProgress(newPositio); //update the text lefttime.setText(String.valueOf(new java.text.SimpleDateFormat("mm:ss") .format(new Date(mediaPlayer.getCurrentPosition())))); righttime.setText(String.valueOf(new java.text.SimpleDateFormat("mm:ss") .format(new Date(mediaPlayer.getCurrentPosition())))); } }); } } catch (InterruptedException e) { e.printStackTrace(); } } }; thread.start(); } @Override protected void onDestroy() { if (mediaPlayer!=null && mediaPlayer.isPlaying()) { mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer=null; } thread.interrupt(); thread=null; super.onDestroy(); } }
xml file
<?xml version="1.0" encoding="utf-8"?><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:orientation="vertical" tools:context=".MainActivity"> <ImageView android:id="@+id/id_artistimage" android:layout_width="150dp" android:layout_height="150dp" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="28dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/oval" /> <ImageView android:id="@+id/imageView2" android:layout_width="342dp" android:layout_height="7dp" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/id_artistimage" app:layout_constraintVertical_bias="0.077" app:srcCompat="@drawable/divider" /> <TextView android:id="@+id/id_songname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="20dp" android:text="@string/songname" android:textColor="@color/pink" android:textSize="24sp" android:textStyle="bold" app:fontFamily="monospace" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.425" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView2" /> <TextView android:id="@+id/id_artistname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="@string/Artist" android:textColor="@color/pink" android:textSize="24sp" android:textStyle="bold" app:fontFamily="monospace" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.502" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/id_songname" app:layout_constraintVertical_bias="0.03" /> <SeekBar android:id="@+id/id_seekBar" android:layout_width="360dp" android:layout_height="28dp" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/id_artistname" app:layout_constraintVertical_bias="0.026" /> <TextView android:id="@+id/id_lefttime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginLeft="16dp" android:layout_marginStart="16dp" android:layout_marginTop="8dp" android:text="@string/lefttime" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/id_seekBar" app:layout_constraintVertical_bias="0.0" /> <TextView android:id="@+id/id_righttime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginEnd="12dp" android:layout_marginLeft="8dp" android:layout_marginRight="12dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="@string/righttime" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.968" app:layout_constraintStart_toEndOf="@+id/id_lefttime" app:layout_constraintTop_toBottomOf="@+id/id_seekBar" app:layout_constraintVertical_bias="0.0" /> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp"android:background="@color/colorPrimaryDark" android:gravity="center" android:padding="14dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/id_lefttime" app:layout_constraintVertical_bias="0.0"> <Button android:id="@+id/id_previousbutton" android:layout_width="40dp" android:layout_height="40dp" android:background="@android:drawable/ic_media_previous" /> <Button android:id="@+id/id_playbutton" android:layout_width="40dp" android:layout_height="40dp" android:background="@android:drawable/ic_media_play" /> <Button android:id="@+id/id_nextbutton" android:layout_width="40dp" android:layout_height="40dp" android:background="@android:drawable/ic_media_next" /> </TableRow> </android.support.constraint.ConstraintLayout>
0 comments:
Post a Comment