0% found this document useful (0 votes)
10 views4 pages

Music App

Uploaded by

vivek koli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Music App

Uploaded by

vivek koli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

**activity_main.

xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:srcCompat="@drawable/player" />
<SeekBar
android:id="@+id/musicbar"
android:min="0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">

<ImageButton
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="play"
app:srcCompat="@drawable/play" />

<ImageButton
android:id="@+id/btnFrwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="forword"
app:srcCompat="@drawable/frwd" />

<ImageButton
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="backward"
app:srcCompat="@drawable/back" />

<ImageButton
android:id="@+id/btnPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="pause"
app:srcCompat="@drawable/pause" />
</LinearLayout>

</LinearLayout>

**MainActivity.java

package com.example.musicapp;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,Runnable{


ImageButton btnPlay,btnFrwd,btnBack,btnPause;
SeekBar sbMusic;
MediaPlayer mPlayer;
int frwdBackTime=5000;
int currentTime=0;
int totalDuration=0;
Handler handler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPlay=findViewById(R.id.btnPlay);
btnFrwd=findViewById(R.id.btnFrwd);
btnBack=findViewById(R.id.btnBack);
btnPause=findViewById(R.id.btnPause);
sbMusic=findViewById(R.id.musicbar);
btnPause.setOnClickListener(this);
btnPlay.setOnClickListener(this);
btnBack.setOnClickListener(this);
btnFrwd.setOnClickListener(this);

mPlayer=MediaPlayer.create(MainActivity.this,R.raw.song);
totalDuration= mPlayer.getDuration();
sbMusic.setMax(totalDuration);
sbMusic.setEnabled(false);
handler.postDelayed(this,100);

@Override
public void onClick(View view) {
if(view.getId()==R.id.btnPlay)
{
mPlayer.start();
}
else if(view.getId()==R.id.btnFrwd)
{
if((currentTime+frwdBackTime)<totalDuration)
{
mPlayer.seekTo(currentTime+frwdBackTime);
sbMusic.setProgress(currentTime+frwdBackTime);
}
}
else if(view.getId()==R.id.btnBack)
{
if((currentTime-frwdBackTime)>0)
{
mPlayer.seekTo(currentTime-frwdBackTime);
sbMusic.setProgress(currentTime-frwdBackTime);
}
}
else
{
mPlayer.pause();
handler.removeCallbacks(this);
}

@Override
public void run() {
currentTime=mPlayer.getCurrentPosition();
sbMusic.setProgress(currentTime);
handler.postDelayed(this, 100);
}
}

**AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MusicApp"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>
**Output

You might also like