Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi Please Help... When we press the next song button, the screen turns white, and takes me to the previous screen . When we press

Hi Please Help... When we press the next song button, the screen turns white, and takes me to the previous screen . When we press the previous song button, it makes us quit the app. This is our MusicActivity.java file in android studio
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import androidx.activity.ComponentActivity;
public class MusicActivity extends ComponentActivity {
ImageView img;
ImageView skipButton;
ImageView backButton;
boolean isPlaying = false;
MediaPlayer musicPlayer;
private int currentSongIndex =0;
int[] songCovers ={R.drawable.md, R.drawable.whatevershewants, R.drawable.snooze};
int[] songResources ={R.raw.million, R.raw.whatever, R.raw.snooze};
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.musicplayer);
setupButtons();
img = findViewById(R.id.playpause);
skipButton = findViewById(R.id.skip);
backButton = findViewById(R.id.goback);
musicPlayer = MediaPlayer.create(this, R.raw.million);
img.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
togglePlayPause();
}
});
skipButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
playNextSong();
}
});
backButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
playPreviousSong();
}
});
}
private void togglePlayPause(){
if (isPlaying){
musicPlayer.pause();
isPlaying = false;
} else {
musicPlayer.start();
isPlaying = true;
}
}
private void playNextSong(){
currentSongIndex =(currentSongIndex +1)% songCovers.length;
musicPlayer.stop();
musicPlayer = MediaPlayer.create(this, getSongResource(currentSongIndex));
//musicPlayer = MediaPlayer.create(this, R.raw.whatever);
musicPlayer.start();
updateSongCover();
isPlaying = true;
}
private void updateSongCover(){
int currentIndex = getCurrentSongIndex();
img.setImageResource(songCovers[currentIndex]);
}
private int getCurrentSongIndex(){
return currentSongIndex;
}
private void playPreviousSong(){
currentSongIndex =(currentSongIndex -1+ songCovers.length)% songCovers.length;
musicPlayer.stop();
musicPlayer = MediaPlayer.create(this, getSongResource(currentSongIndex));
musicPlayer.start();
updateSongCover();
isPlaying = true;
}
private int getSongResource(int index){
return songResources[index];
}
@Override
protected void onDestroy(){
super.onDestroy();
if (musicPlayer != null){
musicPlayer.release();
musicPlayer = null;
}
}
private void setupButtons(){
ImageButton button1=(ImageButton) findViewById(R.id.musichome);
ImageButton button2=(ImageButton) findViewById(R.id.playlist);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(MusicActivity.this, HomeActivity.class);
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(MusicActivity.this, PlaylistManagement.class);
startActivity(intent);
}
});
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions