bikashthapa01 / android-studio-music-player

Music Player With Android TabLayout ( Android Studio 3.4 )
11 stars 5 forks source link

App: Forcing To Close #1

Open codermotion opened 4 years ago

codermotion commented 4 years ago

Android Studio 3.6.1

package com.example.tab;

import android.Manifest; import android.os.Bundle; import android.os.Environment; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; 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;

/**

// I added this line "ArrayAdapter" this also resolved the app not responding issue.

            musicArrayAdapter = new ArrayAdapter<String(getActivity(),android.R.layout.simple_list_item_1,songs);
            allMusicList.setAdapter(musicArrayAdapter);

        }

        @Override
        public void onPermissionDenied(PermissionDeniedResponse response) {

        }

        @Override
        public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
                //keep asking permission
            token.continuePermissionRequest();
        }
    }).check();

    return view;
}

private ArrayList<File> findMusicFiles(File file){

    ArrayList<File> allMusicFilesObject = new ArrayList<>();
    File [] files = file.listFiles();

    for (File currentFile: files ){
    if(currentFile.isDirectory() && !currentFile.isHidden()){

        allMusicFilesObject.addAll(findMusicFiles(currentFile));

    } else{

        allMusicFilesObject.add(currentFile);
    }

    }

    return allMusicFilesObject;
}

}

abhayvikramnayak98 commented 4 years ago

package com.abhaypnayak.gramoplay; import androidx.appcompat.app.; import android.annotation.; import android.content.; import android.media.; import android.net.; import android.os.; import android.view.; import android.widget.; import java.io.; import java.util.; public class Player extends AppCompatActivity { int position; Bundle songExtraData; ArrayList songFileList; SeekBar musicSeekBar; TextView songTitleView; ImageView playBtn, nextBtn, prevBtn; static MediaPlayer mediaPlayer; Intent songData;

@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);

    //Setting resources.

    musicSeekBar = findViewById(R.id.musicSeekBar);
    songTitleView = findViewById(R.id.songTitleView);
    playBtn = findViewById(R.id.play_btn);
    nextBtn = findViewById(R.id.next_btn);
    prevBtn = findViewById(R.id.prev_btn);

    if(mediaPlayer != null)
    {
        mediaPlayer.stop();
    }

    songData = getIntent();
    songExtraData = songData.getExtras();

    songFileList = (ArrayList)songExtraData.getParcelableArrayList("songFileList");
    position = songExtraData.getInt("position", 0);
    initMusicPlayer(position); //Starting the media player.

    playBtn.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //When the play/pause button is clicked.
            playMusic();
        }
    });

    nextBtn.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if(position < songFileList.size() - 1)
            {
                position++;
            }
            else
            {
                position = 0;
            }
            initMusicPlayer(position);
        }
    });

    prevBtn.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if(position <= 0)
            {
                position = songFileList.size();
            }
            else
            {
                position++;
            }
            initMusicPlayer(position);
        }
    });

}
private void initMusicPlayer(int position)
{
    if(mediaPlayer != null && mediaPlayer.isPlaying())
    {
        mediaPlayer.reset();
    }
    String name = songFileList.get(position).getName();
    songTitleView.setText(name);

    //Getting song path on external storage.
    Uri songResourceUri = Uri.parse(songFileList.get(position).toString());

    //Creating a media player class.
    mediaPlayer = MediaPlayer.create(getApplicationContext(), songResourceUri);
    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
    {
        @Override
        public void onPrepared(MediaPlayer mp)
        {
            //Setting the seek bar at maximum duration.
            musicSeekBar.setMax(mediaPlayer.getDuration());
            //Starting the music player.
            mediaPlayer.start();
            //Setting the icon to pause.
            playBtn.setImageResource(R.drawable.pause_btn);
        }
    });
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            playBtn.setImageResource(R.drawable.play_btn);
        }
    });

    musicSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
        {
            if(fromUser)
            {
                mediaPlayer.seekTo(progress); //Seeking the song
                musicSeekBar.setProgress(progress); //Setting the seek bar progress
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            while(mediaPlayer != null)
            {
                try {
                    if (mediaPlayer.isPlaying())
                    {
                        Message message = new Message();
                        message.what = mediaPlayer.getCurrentPosition();
                        handler.sendMessage(message);
                        Thread.sleep(1000);
                    }
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }).run();
}

//Creating handler to set progress.
@SuppressLint("HandlerLeak")
private Handler handler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        musicSeekBar.setProgress(msg.what);
    }
};

private void playMusic()
{
    if(mediaPlayer.isPlaying())
    {

        mediaPlayer.pause();
        playBtn.setImageResource(R.drawable.play_btn);

    }
    else
    {

        mediaPlayer.start();
        playBtn.setImageResource(R.drawable.pause_btn);

    }
}

} When it comes to external storage, it shows the output. Couldn't open /storage/emulated/0/Music/01 - Dil Bechara - Dil Bechara (2020).mp3: java.io.FileNotFoundException: No content provider: /storage/emulated/0/Music/01 - Dil Bechara - Dil Bechara (2020).mp3 And then the app got crashed. Please sort out this problem ASAP.

bikashthapa01 commented 4 years ago

Actually the app is not designed to use external storage. I need time to do research and implement it.