package com.twolf.stressball11; /************************************************************************************************* * * BIG THANKS TO TYLER HACKER AND DOCUMENTATION THAT LEAD TO ME FINDING * www.youtube.com/watch?v=V1ocJmXeQ28 * This class creates a simple media player where the user can choose from a list of 4 songs * The songs are stored in the raw folder. */ import android.media.MediaPlayer; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.RadioButton; public class MusicPlayer extends AppCompatActivity { private RadioButton Song1, Song2, Song3, Song4; public int temp=R.raw.storm; private MediaPlayer mPlayer; private int length=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music_player); Song1 = (RadioButton) findViewById(R.id.song1); Song2 = (RadioButton) findViewById(R.id.song2); Song3 = (RadioButton) findViewById(R.id.song3); Song4 = (RadioButton) findViewById(R.id.song4); mPlayer=MediaPlayer.create(this,temp); } /*********************************************************************************************** * On Pause() simply just pauses the song and releases the media player. When back button is pressed, * the music stops. Call this function when you want the music player to stop and go back to Main ***********************************************************************************************/ @Override protected void onPause(){ //When back button is pressed, the music stops super.onPause(); //Stops playing music when music tab is left or back button is pressed mPlayer.release(); //Releases the media player...essentially never was here } private void SongChange (View v) { if(Song1.isChecked()) temp=R.raw.storm; else if(Song2.isChecked()) temp=R.raw.med; else if(Song3.isChecked()) temp=R.raw.chill; else temp=R.raw.be; mPlayer.stop(); length=0; } /*********************************************************************************************** * PlayMusic(View v)Plays the selected song from beginning unless song has been paused * if the song was paused it will pick up right where it left off ***********************************************************************************************/ public void PlayMusic(View v) { if(length == 0) { SongChange(v); //Calls function to determine which song is selected mPlayer = MediaPlayer.create(this, temp); //creates new media player mPlayer.setLooping(true); //Looping the track until user presses stop or pause } else { mPlayer.seekTo(length); //moves the song to the correct location in the track length = 0; } mPlayer.start(); //starts media player playing the correct music } /*********************************************************************************************** * PauseMusic(View v) Pauses the music and records where the song was paused. ***********************************************************************************************/ public void PauseMusic(View v){ mPlayer.pause(); length = mPlayer.getCurrentPosition(); //stores track location for when user presses play again } }