L
L
lonata2018-07-02 17:06:44
Java
lonata, 2018-07-02 17:06:44

How to add playlist to music player on android?

I am making a player for android as my first application. You need to add a playlist with which the song selection will be made. That is, there will be a folder icon, when you click on it, a full-screen list of the tracks that are on the sd card will appear. The folder icon should be on the main screen of the application.
What should be:
-Tracks in this folder can change, that is, I can add or remove tracks from the computer. The folder should, when opened, show the tracks inside it. As well as other folders that also contain other tracks, you can call it albums. That is, when you open this folder, all other folders nested in it should be visible
-When you click on the track name, it starts and goes to Main_Activity
How to implement this?
5b3a3167d5372803827597.jpeg
XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/background"
    tools:context=".MainActivity">

    <com.mikhaellopez.circularimageview.CircularImageView
        android:layout_width="230dp"
        android:layout_height="230dp"
        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        android:src="@drawable/album_art"
        />


    <Button
        android:id="@+id/btn_play"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="82dp"
        android:background="@drawable/play" />

    <Button
        android:id="@+id/btn_pause"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignBottom="@+id/btn_play"
        android:layout_alignParentStart="true"
        android:layout_marginStart="43dp"
        android:background="@drawable/pause" />

    <Button
        android:id="@+id/btn_stop"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignBottom="@+id/btn_play"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="43dp"
        android:background="@drawable/stop" />
</RelativeLayout>

main_activity class:
package com.kentforth.musicplayer;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button play,pause,stop;
    MediaPlayer mediaPlayer;
    int pauseCurrentPosition;

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

        play = (Button) findViewById(R.id.btn_play);
        pause = (Button) findViewById(R.id.btn_pause);
        stop = (Button) findViewById(R.id.btn_stop);

        play.setOnClickListener(this);
        pause.setOnClickListener(this);
        stop.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch(view.getId()) {

            case R.id.btn_play:
                if(mediaPlayer == null) {
                mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.city);
                mediaPlayer.start();}

                else if(!mediaPlayer.isPlaying()) {
                    mediaPlayer.seekTo(pauseCurrentPosition);
                    mediaPlayer.start();
                }
                break;

            case R.id.btn_pause:
                if (mediaPlayer !=null ) {
                    mediaPlayer.pause();
                    pauseCurrentPosition = mediaPlayer.getCurrentPosition();
                }
                break;

            case R.id.btn_stop:
                if(mediaPlayer!=null) {
                    mediaPlayer.stop();
                    mediaPlayer = null;
                }

                break;

        }

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2018-07-02
@lonata

You need to get a list of files and put them in a list (display).
Read about:

  • Working with files
  • Working with lists (RecyclerView)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question