Build MyMusic App Using Android Studio

Hello Guys ,

   In this tutorial , we are going to create offline MyMusic App. In this App we are fetching song from device and play song in MyMusic App. All Song  Display on MainScreen of the app. Whenever you touch the any song you redirect the another screen and play song. In this App we are using listview for display song in MainActivty. And we are using seekbar , pause , next and previous button. seekbar is used to move position of the song time. Next button is used to play next song. Previous button is used to playing previous song.  Pause button is used to stop thev playing song. 


MyMusic App MainScreen and another Screen looking like below picture :

  MainScreen :  



Another Screen :

Let's get started.

Step 1 : Create a new Project

Step 2 : Edit two Empty Activity xml file

  • Here we edit two xml file for making attractive layout for MyMusic App. Firstly we edit MainScreen for app. In this first screen we are using listview under ConstraintLayout.
  • Listview is used to display song.
  • Edit activity_main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:background="@drawable/bg"
tools:context=".MainActivity">


<ListView
android:id="@+id/listview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:padding="25dp"
android:textColor="#090909"/>

</androidx.constraintlayout.widget.ConstraintLayout>


  • Whenever user touch on any song. user redirect to another activity for playing song. 
  • Here we edit another screen for making more attractive layout. here we used TextView , seekbar , next , previous and pause button. TextView is used to display song name. seekbar is used to moving the time of song.
  • Edit activity_playsongs.xml file 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".Playsongs"
android:background="@drawable/bg">


<ImageView
android:id="@+id/logo"
android:layout_width="221dp"
android:layout_height="393dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />

<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="314dp"
android:text="TextView"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:scrollHorizontally="true"
android:gravity="center"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#090909"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/logo" />

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="314dp"
android:layout_height="198dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent">

<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/previous"
android:src="@drawable/previous"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/pause"
android:src="@drawable/pause"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/next"
android:src="@drawable/next"/>

</LinearLayout>

<SeekBar
android:id="@+id/seekBar"
android:layout_width="371dp"
android:layout_height="21dp"
android:layout_marginBottom="28dp"
app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.775"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>



Step 3 : Add the library in Gradle File.
  • we are added library in gradle files for creating deny and allow permission box for MyMusic App. 
implementation 'com.karumi:dexter:6.2.2'


Step 4 : Implement  two Empty activity class file.
  • Here we first implement the MainScreen of the app. In this class we implement display song and access from devices.
  • Edit MainActivity.java file.
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = findViewById(R.id.listview);
Dexter.withContext(this).withPermission(Manifest.permission.
READ_EXTERNAL_STORAGE).
withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse
permissionGrantedResponse) {

ArrayList<File> mySongs =
fetchSongs(Environment.getExternalStorageDirectory());
String [] items = new String[mySongs.size()];
for(int i = 0; i < mySongs.size(); i++){
items[i] = mySongs.get(i).getName().
replace(".mp3" , " ");
}
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,items);
listview.setAdapter(arrayAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView,
View view, int i, long l) {
Intent intent = new Intent(MainActivity.this ,
Playsongs.class);
String currentsong = listview.getItemAtPosition(i).toString();
intent.putExtra("SongList" , mySongs);
intent.putExtra("CurrentSong" , currentsong);
intent.putExtra("Positon" , i);
startActivity(intent);
}
});
}

@Override
public void onPermissionDenied(PermissionDeniedResponse
permissionDeniedResponse){

}

@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest,
PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
}
}).check();



}

public ArrayList<File> fetchSongs(File file){
// Fetch Song from External Storage Directory
ArrayList arrayList = new ArrayList();
File [] songs = file.listFiles();
if(songs != null){

for (File mysongs : songs){

if(!mysongs.isHidden() && mysongs.isDirectory()){

arrayList.addAll(fetchSongs(mysongs));
}

else{

if (mysongs.getName().endsWith(".mp3") &&
!mysongs.getName().endsWith(".")){

arrayList.add(mysongs);
}
}
}
}

return arrayList;

}

}

  • Here we implement the another activity for playing song.
  • Edit Playsongs.java file. 
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;

public class Playsongs extends AppCompatActivity {


@Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer.stop();
mediaPlayer.release();
updateseekbar.interrupt();
}

TextView textView;
ImageView pause , previous , next;
MediaPlayer mediaPlayer;
ArrayList<File> songs;
String textContext;
int position;
SeekBar seekBar;
Thread updateseekbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playsongs);

textView = findViewById(R.id.textView);
pause = findViewById(R.id.pause);
previous = findViewById(R.id.previous);
next = findViewById(R.id.next);
seekBar = findViewById(R.id.seekBar);

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
songs = (ArrayList) bundle.getParcelableArrayList("SongList");
textContext = intent.getStringExtra("CurrentSong");
textView.setText(textContext);
textView.setSelected(true);
position = intent.getIntExtra("Position" , 0);

Uri uri = Uri.parse(songs.get(position).toString());
mediaPlayer = MediaPlayer.create(this, uri);
mediaPlayer.start();

seekBar.setMax(mediaPlayer.getDuration());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mediaPlayer.seekTo(seekBar.getProgress());
}
});

updateseekbar = new Thread(){
@Override
public void run() {
int currentposition = 0;
try{
while (currentposition < mediaPlayer.getDuration()){
currentposition = mediaPlayer.getCurrentPosition();
seekBar.setProgress(currentposition);
sleep(800);
}

}catch (Exception e){
e.printStackTrace();
}
}
};
updateseekbar.start();

pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mediaPlayer.isPlaying()){
pause.setImageResource(R.drawable.play);
mediaPlayer.pause();
}
else{
pause.setImageResource(R.drawable.pause);
mediaPlayer.start();
}
}
});

previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaPlayer.stop();
mediaPlayer.release();

if (position != 0){
position = position - 1;
}
else{
position = songs.size() - 1;
}

Uri uri = Uri.parse(songs.get(position).toString());
mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
mediaPlayer.start();

pause.setImageResource(R.drawable.pause);
seekBar.setMax(mediaPlayer.getDuration());
textContext = songs.get(position).getName().toString();
textView.setText(textContext);
}
});


next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaPlayer.stop();
mediaPlayer.release();

if (position != songs.size() - 1){
position = position + 1;
}
else{
position = 0;
}

Uri uri = Uri.parse(songs.get(position).toString());
mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
mediaPlayer.start();

pause.setImageResource(R.drawable.pause);
seekBar.setMax(mediaPlayer.getDuration());
textContext = songs.get(position).getName().toString();
textView.setText(textContext);
}
});
}
}


Step 5 : Edit AndroidManifest.xml file.
  • Here we are add user permission on the AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mymusic">

<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/Theme.MyMusic">
<activity android:name=".Playsongs"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

</manifest>


Now your MyMusic App is ready. 


Output : 

               



I hope you like post. If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support.  🙏   

Post a Comment

0 Comments