<?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:padding="10dp"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mySongListView"
/>
</RelativeLayout>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
tools:context=".PlayerActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:orientation="vertical"
android:layout_weight="7"
>
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/cover_art"/>
<TextView
android:id="@+id/song_name_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text = "Song name"
android:textAlignment="center"
android:gravity="center_horizontal"
android:textSize="20sp"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
android:scrollHorizontally="true"
android:textColor="@color/colorPrimary"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
android:layout_alignParentBottom="true"
android:layout_marginBottom="40dp"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:id="@+id/pause"
android:background="@drawable/ic_pause"
android:layout_marginTop="5dp"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_toRightOf="@+id/pause"
android:id="@+id/next"
android:background="@drawable/ic__skip_next"
android:layout_marginTop="5dp"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_toLeftOf="@+id/pause"
android:id="@+id/prev"
android:background="@drawable/ic_skip_previous"
android:layout_marginTop="5dp"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
package com.jwlee.musicplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
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 myListViewSong;
String[] items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListViewSong = findViewById(R.id.mySongListView);
runtimePermission();
}
public void runtimePermission() {
Dexter.withActivity(this).withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
display();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
token.continuePermissionRequest();
}
}).check();
}
public ArrayList<File> findSong(File file) {
ArrayList<File> arrayList = new ArrayList<>();
File[] files = file.listFiles();
for (File singleFile : files) {
if (singleFile.isDirectory() && !singleFile.isHidden()) {
arrayList.addAll(findSong(singleFile));
} else {
if (singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")) {
arrayList.add(singleFile);
}
}
}
return arrayList;
}
void display() {
final ArrayList<File> mySong = findSong(Environment.getExternalStorageDirectory());
items = new String[mySong.size()];
for(int i=0; i<mySong.size();i++){
items[i]= mySong.get(i).getName().toString().replace(".mp3","").replace(".wav","");
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items );
myListViewSong.setAdapter(myAdapter);
myListViewSong.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
String songName = myListViewSong.getItemAtPosition(i).toString();
startActivity(new Intent(getApplicationContext(),PlayerActivity.class)
.putExtra("songs",mySong).putExtra("songname",songName).putExtra("pos",i));
}
});
}
}
}
package com.jwlee.musicplayer;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
public class PlayerActivity extends AppCompatActivity {
Button btn_next, btn_prev, btn_pause;
TextView songNameText;
SeekBar songSeekBar;
String song_name;
static MediaPlayer myMediaPlayer;
int position;
ArrayList<File> mySongs;
Thread updateseekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
btn_next= findViewById(R.id.next);
btn_prev = findViewById(R.id.prev);
btn_pause = findViewById(R.id.pause);
songNameText = findViewById(R.id.song_name_text);
songSeekBar = findViewById(R.id.seekBar);
getSupportActionBar().setTitle("Now Playing");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
updateseekBar = new Thread() {
@Override
public void run(){
int totalDuration = myMediaPlayer.getDuration();
int currentPostion = 0;
while (currentPostion < totalDuration){
try{
sleep(500);
currentPostion = myMediaPlayer.getCurrentPosition();
songSeekBar.setProgress(currentPostion);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
};
if(myMediaPlayer != null){
myMediaPlayer.stop();
myMediaPlayer.release();
}
Intent i = getIntent();
Bundle bundle = i.getExtras();
mySongs = (ArrayList)bundle.getParcelableArrayList("songs");
song_name = mySongs.get(position).getName().toString();
String songName = i.getStringExtra("songname");
songNameText.setText(songName);
songNameText.setSelected(true);
position = bundle.getInt("pos",0);
Uri u = Uri.parse(mySongs.get(position).toString());
myMediaPlayer = MediaPlayer.create(getApplicationContext(),u);
myMediaPlayer.start();
songSeekBar.setMax(myMediaPlayer.getDuration() );
updateseekBar.start();
songSeekBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.MULTIPLY);
songSeekBar.getThumb().setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
songSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
myMediaPlayer.seekTo(seekBar.getProgress());
}
});
btn_pause.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
songSeekBar.setMax(myMediaPlayer.getDuration());
if(myMediaPlayer.isPlaying()){
btn_pause.setBackgroundResource(R.drawable.ic_play);
myMediaPlayer.pause();
}else {
btn_pause.setBackgroundResource(R.drawable.ic_pause);
myMediaPlayer.start();
}
}
});
btn_next.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
myMediaPlayer.stop();
myMediaPlayer.release();
position = ((position+1)%mySongs.size());
Uri u = Uri.parse(mySongs.get(position).toString());
myMediaPlayer = MediaPlayer.create(getApplicationContext(),u);
song_name = mySongs.get(position).getName().toString();
songNameText.setText(song_name);
myMediaPlayer.start();
}
});
btn_prev.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
myMediaPlayer.stop();
myMediaPlayer.release();
position = (position-1< 0) ? (mySongs.size()-1) : (position-1);
Uri u = Uri.parse(mySongs.get(position).toString());
myMediaPlayer = MediaPlayer.create(getApplicationContext(),u);
song_name = mySongs.get(position).getName().toString();
songNameText.setText(song_name);
myMediaPlayer.start();
}
});
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(item.getItemId() == android.R.id.home){
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
}
'기타 ETC > Android Studio' 카테고리의 다른 글
Android SQLite Error: android.database.sqlite.SQLiteException: no such column: (code 1) (0) | 2020.07.15 |
---|---|
COVID19 TRACKER Android source code (0) | 2020.07.14 |
[Android] Delay handling using postDelayed method (0) | 2020.07.08 |
[Android] youtube link open (0) | 2020.07.07 |
It is simpler when you have a lot of image buttons, and you don't want to write xml-s for every button. (0) | 2020.07.06 |