Hello iam making a simple mp3 player before this i have implemented the media player in one activity but now i have two activity in the first activity i have a listview and in the second activity i have seekbar and also play and pause buttons with an image for the song but i dont have any idea that how i can implement this when a person click the song name in the listview from first activity it goes to second activity and play the song in the second activity with play, pause and seekbar is there any source code or idea that i can check that, if i use putExtra so how i can send the index of songs from listview to second activity my mp3 files are in the raw folder, i have this code but this is for reading songs from external storage i want from raw folder
MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView listView;
Boolean mExternalStorageAvailable;
String[] items;//to read all files
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 99;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkStoragePermission();
}
}
public ArrayList<File> findSong(File root){
ArrayList<File> at = new ArrayList<File>();
File[] files = root.listFiles();
for(File singleFile : files){
if(singleFile.isDirectory() && !singleFile.isHidden()){
at.addAll(findSong(singleFile));
}
else{
if(singleFile.getName().endsWith(".mp3") ||
singleFile.getName().endsWith(".wav")){
at.add(singleFile);
}
}
}
return at;
}
void display(){
final ArrayList<File> mySongs =
findSong(Environment.getExternalStorageDirectory());
items = new String[ mySongs.size() ];
for(int i=0;i<mySongs.size();i++){
//toast(mySongs.get(i).getName().toString());
items[i] = mySongs.get(i).getName().toString().replace(".mp3","").replace(".wav","");
}
ArrayAdapter<String> adp = new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
listView.setAdapter(adp);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int
position, long l) {
startActivity(new
Intent(getApplicationContext(),PlayerActivity.class).putExtra("pos",position).putExtra("songs",mySongs));
}
});
}
void checkExternalStorage(){
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
} else {
mExternalStorageAvailable= false;
}
handleExternalStorageState();
}
void handleExternalStorageState() {
if(mExternalStorageAvailable){
display();
}
else{
Toast.makeText(getApplicationContext(),"Please insert an SDcard",Toast.LENGTH_LONG).show();
}
}
public boolean checkStoragePermission() {
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(this,
new
String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
} else {
ActivityCompat.requestPermissions(this,
new
String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
return false;
} else {
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[]
grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
if (grantResults.length > 0
&& grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
checkExternalStorage();
}
} else {
Toast.makeText(this, "permission denied",
Toast.LENGTH_LONG).show();
}
return;
}
}
}
}
Second activity for playing songs
public class PlayerActivity extends AppCompatActivity {
static MediaPlayer mp;//assigning memory loc once or else multiple songs will play at once
int position;
SeekBar sb;
ArrayList<File> mySongs;
Thread updateSeekBar;
Button pause,forward,reverse,next,previous;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
pause = (Button)findViewById(R.id.pause);
forward = (Button)findViewById(R.id.forward);
previous = (Button)findViewById(R.id.previous);
next = (Button)findViewById(R.id.next);
reverse = (Button)findViewById(R.id.reverse);
sb=(SeekBar)findViewById(R.id.seekBar);
updateSeekBar=new Thread(){
@Override
public void run(){
int totalDuration = mp.getDuration();
int currentPosition = 0;
while(currentPosition < totalDuration){
try{
sleep(500);
currentPosition=mp.getCurrentPosition();
sb.setProgress(currentPosition);
}
catch (InterruptedException e){
e.printStackTrace();
}
}
}
};
if(mp != null){
mp.stop();
mp.release();
}
Intent i = getIntent();
Bundle b = i.getExtras();
mySongs = (ArrayList) b.getParcelableArrayList("songs");
position = b.getInt("pos",0);
Uri u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(),u);
mp.start();
sb.setMax(mp.getDuration());
updateSeekBar.start();
sb.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) {
mp.seekTo(seekBar.getProgress());
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sb.setMax(mp.getDuration());
if(mp.isPlaying()){
pause.setText(">");
mp.pause();
}
else {
pause.setText("||");
mp.start();
}
}
});
forward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sb.setMax(mp.getDuration());
mp.seekTo(mp.getCurrentPosition()+5000);
}
});
reverse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sb.setMax(mp.getDuration());
mp.seekTo(mp.getCurrentPosition()-5000);
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.stop();
mp.release();
position=((position+1)%mySongs.size());
Uri u = Uri.parse(mySongs.get( position).toString());
mp = MediaPlayer.create(getApplicationContext(),u);
mp.start();
}
});
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.stop();
mp.release();
position=((position-1)<0)?(mySongs.size()-1):(position-1);
Uri u = Uri.parse(mySongs.get(
position).toString());//%mysongs so that it do not go to invalid position
mp = MediaPlayer.create(getApplicationContext(),u);
mp.start();
}
});
}
}
答案 0 :(得分:0)
这是开始要遵循的一些步骤。