⦁ Create an MP3 player application by using services and design
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""
xmlns:app=""
xmlns:tools=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/button"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="650dp"
android:text="Mp3 PLayer!"
android:textAlignment="center"
android:textColor="@color/Lightpink"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginLeft="90dp"
android:layout_marginBottom="650dp"
android:src="@drawable/ic_baseline_playlist_play_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginRight="340dp"
android:layout_marginBottom="650dp"
android:src="@drawable/ic_baseline_reorder_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.cardview.widget.CardView
android:id="@+id/cardView2"
android:layout_width="390dp"
android:layout_height="120dp"
android:layout_marginTop="170dp"
app:cardCornerRadius="28dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="390dp"
android:layout_height="120dp"
android:src="@color/Blackshade"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cardView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginTop="55dp"
android:text="KING"
android:textColor="@color/grey"
android:textSize="17dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginTop="80dp"
android:text="The carnival"
android:textColor="@color/black" />
<ImageView
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_marginLeft="320dp"
android:layout_marginTop="60dp"
android:src="@drawable/like" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="350dp"
android:layout_height="350dp"
android:layout_marginBottom="220dp"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="350dp"
android:layout_height="350dp"
android:layout_marginBottom="230dp"
android:src="@drawable/king"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.cardview.widget.CardView>
<ImageView
android:layout_width="300dp"
android:layout_height="100dp"
android:src="@drawable/group"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cardView2" />
<!-- <androidx.appcompat.widget.AppCompatSeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintTop_toBottomOf="@+id/cardView2" />
<TextView
android:id="@+id/tv_pass"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginLeft="20dp"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/seekBar" />
<TextView
android:id="@+id/tv_due"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginRight="20dp"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/seekBar" />-->
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="650dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/repeat"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:src="@drawable/repeat" />
<ImageView
android:id="@+id/backwrad"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginTop="20dp"
android:layout_marginRight="25dp"
android:src="@drawable/backward" />
<ImageView
android:id="@+id/play"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginBottom="15dp"
android:background="@drawable/circle"
android:src="@drawable/ic_baseline_play_arrow_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/forward"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginLeft="34dp"
android:layout_marginTop="17dp"
android:src="@drawable/forward" />
<ImageView
android:id="@+id/stop"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="8dp"
android:src="@drawable/stop" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Mainactivity.kt
package com.example.musicplayer
import android.annotation.SuppressLint
import android.content.Intent
import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.widget.*
//import kotlinx.android.synthetic.main.activity_main.*
private lateinit var mediaPlayer: MediaPlayer
private lateinit var runnable:Runnable
private var handler: Handler = Handler()
private var pause:Boolean = false
class MainActivity : AppCompatActivity() {
@SuppressLint("WrongViewCast")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var playBtn = findViewById<ImageView>(R.id.play)
var pauseBtn = findViewById<ImageView>(R.id.repeat)
var stopBtn = findViewById<ImageView>(R.id.stop)
playBtn.setOnClickListener {
Toast.makeText(this, "play", Toast.LENGTH_LONG).show()
var intent: Intent = Intent(applicationContext, Mediaservice::class.java)
intent.putExtra("service1", "play")
startService(intent)
}
pauseBtn.setOnClickListener {
Toast.makeText(this, "pause", Toast.LENGTH_LONG).show()
var intent: Intent = Intent(applicationContext, Mediaservice::class.java)
intent.putExtra("service1", "pause")
startService(intent)
}
stopBtn.setOnClickListener {
Toast.makeText(this, "stop", Toast.LENGTH_LONG).show()
var intent: Intent = Intent(applicationContext, Mediaservice::class.java)
intent.putExtra("service1", "stop")
stopService(intent)
}
}
}
Mediaservice.kt
package com.example.musicplayer
import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.IBinder
class Mediaservice : Service() {
lateinit var mediaplayer: MediaPlayer
override fun onBind(intent: Intent): IBinder {
TODO("Return the communication channel to the service.")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (!this::mediaplayer.isInitialized) {
mediaplayer = MediaPlayer.create(this, R.raw.song)
}
var string1: String? = intent!!.getStringExtra("service1")
if (string1 == "play")
mediaplayer.start()
else if (string1 == "pause") {
mediaplayer.pause()
}
return START_STICKY
}
override fun onDestroy() {
mediaplayer.stop()
super.onDestroy()
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
xmlns:tools=""
package="com.example.musicplayer">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".Mediaservice"
android:enabled="true"
android:exported="true"></service>
<activity
android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="">
<item>
<shape>
<gradient
android:startColor="#232323"
android:endColor="@color/Brown"
android:type="linear"/>
</shape>
</item>
</selector>
Circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=""
android:shape="oval">
<solid android:color="@color/Blackshade"></solid>
</shape>
Image.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="">
<solid android:color="@color/Blackshade" />
<corners android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:topRightRadius="20dp"
android:topLeftRadius="20dp"/>
</shape>
Color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
<color name="Brown">#563429</color>
<color name="Lightpink">#f4e3dd</color>
<color name="Blackshade">#191919</color>
<color name="grey">#d1d1d1</color>
<color name="black">#4d4d4d</color>
</resources>
String.xml
<resources>
<string name="app_name">MusicPlayer</string>
</resources>
Style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
In also add one directory for music raw file.
Output:-
Comments