loop for array of images and audio - android

I am making an app in which there are many images and audio. All I want to do is apply a loop for these images and audio so that when an image gets displayed the audio will get played, i.e. image1=audio1, image2=audio2. There are next and previous buttons in my app. Next will display the next image and audio, previous will display previous one. Should I use a for loop? or switch case? I have tried this code. But when the images and audio array comes to an end I want to start all over again. Please help.
public class MainActivity extends Activity {
int i=1;
MediaPlayer mediaplayer;
int image = 0;
int sound ;
int [] sounds;
int [] audio;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sounds = new int [] {
R.raw.audio_one, R.raw.audio_two, R.raw.audio_three, R.raw.audio_four
};
mediaplayer = MediaPlayer.create(this, sounds[0]);
mediaplayer.start();
final int [] images = {
R.drawable.num1, R.drawable.num2, R.drawable.num3, R.drawable.num4
};
ImageButton next = (ImageButton)findViewById(R.id.imageButton1);
ImageButton prev = (ImageButton)findViewById(R.id.imageButton2);
final ImageView img1 = (ImageView)findViewById(R.id.imageView2);
final ImageView img2 = (ImageView)findViewById(R.id.imageView3);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(image==3){
}
else {
image++;
}
i=image;
audio(i);
mediaplayer.start();
img1.setImageResource(images[image]);
}
});
prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (image<=0){
img1.setImageResource(R.drawable.num1);
}
else
{
image--;
}
i=image;
audio(i);
mediaplayer.start();
img1.setImageResource(images[image]);
}
});
}
public void audio(int a)
{
mediaplayer = MediaPlayer.create(this, sounds[a]);
return;
}
}

use the following code...
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(image== images.lenght()-1){
image=0;
}
else {
image++;
}
i=image;
audio(i);
mediaplayer.start();
img1.setImageResource(images[image]);
}
});
prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (image==0){
img1.setImageResource(R.drawable.num1);
}
else
{
image--;
}
i=image;
audio(i);
mediaplayer.start();
img1.setImageResource(images[image]);
}
});
}

Related

Trying to restrict other sound when other sound is playing

I am trying to make a sound wait to play if another sound is currently playing. But, the code I wrote doesn't execute.
public class Transportation extends AppCompatActivity {
private MediaPlayer sound,sound1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transportation);
sound = MediaPlayer.create(this,R.raw.vatunabus);
final ImageView imageView= (ImageView)findViewById(R.id.vatunabussound);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sound1.isPlaying()){
sound.stop();
sound.release();
}
else if(sound.isPlaying()){
sound.pause();
}
else {
sound.start();
}
}
});
sound1 = MediaPlayer.create(this,R.raw.varowlsanabus);
final ImageView imageView1 = (ImageView)findViewById(R.id.firstbussound);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sound.isPlaying()){
sound1.stop();
sound1.release();
}
else if(sound1.isPlaying()){
sound1.pause();
}
else {
sound1.start();
}
}
});
}
}

How to play only one sound at a time?

I have two ImageViews with different sounds which plays sound when clicked on one of them. When click on the first ImageView I want the sound to play and before the first ImageView finish playing the sound, if the second ImageView is click, I want to restrict the second ImageView to play a sound because the first ImageView is still playing the sound. In fact, I want to restrict a sound to play when another sound is playing. But, I didn't get the desired result.
public class Transportation extends AppCompatActivity {
private MediaPlayer sound,sound1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transportation);
sound = MediaPlayer.create(this,R.raw.vatunabus);
final ImageView imageView= (ImageView)findViewById(R.id.vatunabussound);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sound1.isPlaying()){
sound.stop();
sound.release();
}
else if(sound.isPlaying()){
sound.pause();
imageView.setBackgroundResource(R.mipmap.playicon);
}
else {
sound.start();
imageView.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
imageView.setBackgroundResource(R.mipmap.playicon);
}
});
sound1 = MediaPlayer.create(this,R.raw.varowlsanabus);
final ImageView imageView1 = (ImageView)findViewById(R.id.firstbussound);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sound.isPlaying()){
sound1.stop();
sound1.release();
}
else if(sound1.isPlaying()){
sound1.pause();
imageView1.setBackgroundResource(R.mipmap.playicon);
}
else {
sound1.start();
imageView1.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
imageView1.setBackgroundResource(R.mipmap.playicon);
}
});
}
}
Here's an idea, let's create a boolean value to tell us if a sound is currently playing.
private MediaPlayer sound,sound1;
private boolean soundPlaying = false; //new boolean variable
Next, before playing a sound, we'll check to make sure it's still false:
public void onClick(View v) {
//Add a check that exits the function if a sound is playing
//Add this check in both onClick methods
if(soundPlaying){
return;
}
...
}
Also, after sound.start() and sound1.start(), let's set the boolean to true:
soundPlaying = true;
Finally, in both instances of the onCompletion methods, let's reset the boolean to false:
public void onCompletion(MediaPlayer mp) {
soundPlaying = false;
...
}
The final code will look like this:
public class Transportation extends AppCompatActivity {
private MediaPlayer sound,sound1;
private boolean soundPlaying = false; //new boolean variable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transportation);
sound = MediaPlayer.create(this,R.raw.vatunabus);
final ImageView imageView= (ImageView)findViewById(R.id.vatunabussound);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Add a check that exits the function if a sound is playing
if(soundPlaying){
return;
}
if(sound1.isPlaying()){
sound.stop();
sound.release();
}
else if(sound.isPlaying()){
sound.pause();
imageView.setBackgroundResource(R.mipmap.playicon);
}
else {
sound.start();
imageView.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
soundPlaying = false;
imageView.setBackgroundResource(R.mipmap.playicon);
}
});
sound1 = MediaPlayer.create(this,R.raw.varowlsanabus);
final ImageView imageView1 = (ImageView)findViewById(R.id.firstbussound);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Add a check that exits the function if a sound is playing
if(soundPlaying){
return;
}
if(sound.isPlaying()){
sound1.stop();
sound1.release();
}
else if(sound1.isPlaying()){
sound1.pause();
imageView1.setBackgroundResource(R.mipmap.playicon);
}
else {
sound1.start();
imageView1.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
soundPlaying = false;
imageView1.setBackgroundResource(R.mipmap.playicon);
}
});
}
}
I hope this helps. Couldn't try it in live code though, so let me know how it goes.

setOnCompletionListener is detecting the completion only for first time

setOnCompletionListener is detecting the completion of a song the first time only. In the code below song1 and song2 are played one after the other but the remaining songs are not being played.
I want to play the songs one by one and add some silence between songs.
MediaPlayer song0=new MediaPlayer();
int track = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
song0=MediaPlayer.create(this,R.raw.song1);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
function();
}
});
song0.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer song0) {
track++;
loadsong();
function();
}
});
}
void loadsong()
{
if(track==1) song0=MediaPlayer.create(this,R.raw.song2);
if(track==2) song0=MediaPlayer.create(this,R.raw.song3);
if(track==3) song0=MediaPlayer.create(this,R.raw.song4);
}**strong text**
void function(){
if(track<4) song0.start();
else
song0.stop();
}
The problem is that you create MediaPlayer object every time you need to play songs. So you need to set OnCompletionListener every time after creating MediaPlayer object for another song.
So you can change a few lines in your code to fix the issue.
MediaPlayer song0=new MediaPlayer();
int track = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
song0=MediaPlayer.create(this,R.raw.song1);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
function();
}
});
song0.setOnCompletionListener(m_CompletionListener);
}
void loadsong()
{
if(track==1) {
song0=MediaPlayer.create(this,R.raw.song2);
song0.setOnCompletionListener(m_CompletionListener);
}
if(track==2) {
song0=MediaPlayer.create(this,R.raw.song3);
song0.setOnCompletionListener(m_CompletionListener);
}
if(track==3) {
song0=MediaPlayer.create(this,R.raw.song4);
song0.setOnCompletionListener(m_CompletionListener);
}
}**strong text**
void function(){
if(track<4) song0.start();
else
song0.stop();
}
MediaPlayer.OnCompletionListener m_CompletionListener = new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer song0) {
track++;
loadsong();
function();
}
};
Another way to implement is to create only one MediaPlayer object and instead of creating MediaPlayer object everytime, call setDataSource function for playing other songs.
If you need this way more detail, i can make sample code also.
I hope it will help you!
Your onclickListener event only starting for once. If you want to play song one by one you have to create a loop or have to do it recursively. Here's a snippet where I used song0.setOnCompletionListener inside loadsong() and in the event recursively called loadsong() every time. Changed your loadsong() method a little bit. Here is the code:
public class MainActivity extends AppCompatActivity {
private Button play;
MediaPlayer song0 = new MediaPlayer();
int track = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
loadsong();
}
});
}
void loadsong() {
track++;
if (track == 1) {
song0 = MediaPlayer.create(this, R.raw.track1);
song0.start();
}else if (track == 2) {
song0 = MediaPlayer.create(this, R.raw.track2);
song0.start();
}else if (track == 3) {
song0 = MediaPlayer.create(this, R.raw.track3);
song0.start();
}else if (track > 3) {
song0.stop();
}
song0.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer song) {
song.stop();
loadsong();
}
});
}
}

Multiple audio tracks in the same activity

I'm creating an audio playback application. I've currently just got two tracks added but eventually I need to add many more... I've currently created two different mediaplayers to run both the tracks, however as the number of tracks will increase using this procedure will lead to problems and lack of performance!
Is there some way I can code this better and more generically?
Here's the java code
public class ChantsFragment extends Fragment {
TextView text1, text2
ImageButton play1, play2, pause1, pause2, repeatoff1, repeatoff2, repeaton1, repeaton2, stop1, stop2;
MediaPlayer mediaPlayer1, mediaPlayer2;
Toast on, off;
public ChantsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_chants, container, false);
play1 = (ImageButton) view.findViewById(R.id.play1);
play2 = (ImageButton) view.findViewById(R.id.play2);
pause1 = (ImageButton) view.findViewById(R.id.pause1);
pause2 = (ImageButton) view.findViewById(R.id.pause2);
repeatoff1 = (ImageButton) view.findViewById(R.id.repeatoff1);
repeatoff2 = (ImageButton) view.findViewById(R.id.repeatoff2);
repeaton1 = (ImageButton) view.findViewById(R.id.repeaton1);
repeaton2 = (ImageButton) view.findViewById(R.id.repeaton2);
stop1 = (ImageButton) view.findViewById(R.id.stop1);
stop2 = (ImageButton) view.findViewById(R.id.stop2);
mediaPlayer1 = MediaPlayer.create(getActivity(), R.raw.audio1);
mediaPlayer2 = MediaPlayer.create(getActivity(), R.raw.audio2);
on = Toast.makeText(getActivity(), "The chant will repeat itself", Toast.LENGTH_LONG);
off = Toast.makeText(getActivity(), "The chant will no longer repeat itself", Toast.LENGTH_LONG);
//Track 1
text1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer1 != null && mediaPlayer1.isPlaying()) {
text1.setClickable(false);
}
if (mediaPlayer2 != null && mediaPlayer2.isPlaying()) {
mediaPlayer2.stop();
mediaPlayer2.release();
mediaPlayer2 = null;
play2.setVisibility(View.GONE);
pause2.setVisibility(View.GONE);
repeatoff2.setVisibility(View.GONE);
repeaton2.setVisibility(View.GONE);
stop2.setVisibility(View.GONE);
}
if (mediaPlayer1 == null) {
mediaPlayer1 = MediaPlayer.create(getActivity(), R.raw.audio1);
} else {
text1.setClickable(true);
mediaPlayer1.start();
play1.setVisibility(View.GONE);
pause1.setVisibility(View.VISIBLE);
repeatoff1.setVisibility(View.VISIBLE);
stop1.setVisibility(View.VISIBLE);
}
mediaPlayer1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
play1.setVisibility(View.GONE);
pause1.setVisibility(View.GONE);
repeatoff1.setVisibility(View.GONE);
repeaton1.setVisibility(View.GONE);
stop1.setVisibility(View.GONE);
}
});
}
});
play1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play1.setVisibility(View.GONE);
pause1.setVisibility(View.VISIBLE);
mediaPlayer1.start();
}
});
pause1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play1.setVisibility(View.VISIBLE);
pause1.setVisibility(View.GONE);
mediaPlayer1.pause();
}
});
repeatoff1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer1.setLooping(true);
repeatoff1.setVisibility(View.GONE);
repeaton1.setVisibility(View.VISIBLE);
on.show();
off.cancel();
}
});
repeaton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer1.setLooping(false);
repeaton1.setVisibility(View.GONE);
repeatoff1.setVisibility(View.VISIBLE);
off.show();
on.cancel();
}
});
stop1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer1.stop();
mediaPlayer1 = null;
text1.setClickable(true);
play1.setVisibility(View.GONE);
pause1.setVisibility(View.GONE);
repeatoff1.setVisibility(View.GONE);
repeaton1.setVisibility(View.GONE);
stop1.setVisibility(View.GONE);
}
});
//Track 2
text2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer2 != null && mediaPlayer2.isPlaying()) {
text2.setClickable(false);
}
if (mediaPlayer1 != null && mediaPlayer1.isPlaying()) {
mediaPlayer1.stop();
mediaPlayer1.release();
mediaPlayer1 = null;
play1.setVisibility(View.GONE);
pause1.setVisibility(View.GONE);
repeatoff1.setVisibility(View.GONE);
repeaton1.setVisibility(View.GONE);
stop1.setVisibility(View.GONE);
}
if (mediaPlayer2 == null) {
mediaPlayer2 = MediaPlayer.create(getActivity(), R.raw.audio2);
} else {
text2.setClickable(true);
mediaPlayer2.start();
play2.setVisibility(View.GONE);
pause2.setVisibility(View.VISIBLE);
repeatoff2.setVisibility(View.VISIBLE);
stop2.setVisibility(View.VISIBLE);
}
mediaPlayer2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
play2.setVisibility(View.GONE);
pause2.setVisibility(View.GONE);
repeatoff2.setVisibility(View.GONE);
repeaton2.setVisibility(View.GONE);
stop2.setVisibility(View.GONE);
}
});
}
});
play2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play2.setVisibility(View.GONE);
pause2.setVisibility(View.VISIBLE);
mediaPlayer2.start();
}
});
pause2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play2.setVisibility(View.VISIBLE);
pause2.setVisibility(View.GONE);
mediaPlayer2.pause();
}
});
repeatoff2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer2.setLooping(true);
repeatoff2.setVisibility(View.GONE);
repeaton2.setVisibility(View.VISIBLE);
on.show();
off.cancel();
}
});
repeaton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer2.setLooping(false);
repeaton2.setVisibility(View.GONE);
repeatoff2.setVisibility(View.VISIBLE);
off.show();
on.cancel();
}
});
stop2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer2.stop();
mediaPlayer2 = null;
text2.setClickable(true);
play2.setVisibility(View.GONE);
pause2.setVisibility(View.GONE);
repeatoff2.setVisibility(View.GONE);
repeaton2.setVisibility(View.GONE);
stop2.setVisibility(View.GONE);
}
});
return view;
}
Edit
I've currently zeroed down on adding around 10 tracks to the application. I want playback to stop on the current track (if playing) the moment another track is selected and using media player to do the same would mean checking separately if 9 media players are playing! (Unless of course there's another way to do this which I've completely missed)
Well, for playing multiple audio files at once, SoundPool seems to be your best choice mainly because its much more lightweight and you can control the number of streams. You can create a new SoundPool instance as:
SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 100); // 10 = maxStreams, 100 = quality
You can then start loading different sounds into it as:
int soundId1 = soundPool.load(context, R.raw.audio1, 1);
int soundId2 = soundPool.load(context, R.raw.audio2, 1);
...
You can control playback as:
int idBeingPlayed = soundPool.play (soundID, 1, 1, 1, -1, 1); // volume params, returns the streamId
Other controls:
soundPool.pause(streamID);
soundPool.resume(streamID);
For more info, visit the offfical docs. Hope this helps!

Can't pass an int from button if / else case to media player

how are you supposed to let the mediaplayer know what it is supposed to play?
wholeTextPlayer = MediaPlayer.create(Lesson1Reading.this, engAu);
It works fine if I declare the file in the top:
MediaPlayer wholeTextPlayer;
private int engAu = R.raw.l1r_en_l10;
Button btn_default_acc_whole;
It doesn't work from within a button click if / else statement wherever I try to put it with the following combination:
MediaPlayer wholeTextPlayer;
private int engAu;
Button btn_default_acc_whole;
The button click:
final Button btn_default_acc_whole = (Button) findViewById(R.id.btn_default_acc_whole);
btn_default_acc_whole.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (wholeTextPlayer.isPlaying()) {
wholeTextPlayer.pause();
} else {
wholeTextPlayer.start();
startPlayProgressUpdater();
}
setEngAu(R.raw.l1r_en_l10); //this line doesn't want to fit anywhere
}
});
The setter:
public void setEngAu(int engAu) {
this.engAu = engAu;
}
Of course they are separately placed in the activity, I just copied and pasted the relevant bits from it.
Thanks guys.
Here is the whole code:
'public class Lesson1Grammar extends Activity {
private SeekBar seekBar;
MediaPlayer wholeTextPlayer;
private int engAu;
private final Handler handler = new Handler();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lesson1grammar);
WholeDefAccPlayer();
final RelativeLayout playerScreen = (RelativeLayout)findViewById(R.id.playerScreen);
final ImageButton btn_player_screen = (ImageButton) findViewById(R.id.btn_player_screen);
btn_player_screen.setOnClickListener(new View.OnClickListener() {
//this hides/unhides the part of the layout in which the player is
#Override
public void onClick(View arg0) {
if (playerScreen.isShown()) {
playerScreen.setVisibility(View.GONE);
} else {
playerScreen.setVisibility(View.VISIBLE);
}
}
});
final Button btn_default_acc_whole = (Button) findViewById(R.id.btn_default_acc_whole);
btn_default_acc_whole.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (wholeTextPlayer.isPlaying()) {
wholeTextPlayer.pause();
} else {
setEngAu(R.raw.default_acc_audio);
wholeTextPlayer.start();
startPlayProgressUpdater();
}
}
});
}
public void setEngAu(int engAu) {
this.engAu = engAu;
}
private void WholeDefAccPlayer() {
wholeTextPlayer = MediaPlayer.create(Lesson1Grammar.this, engAu);
((TextView) findViewById(R.id.getTitleOfAccent)).setText(R.string.btn_lesson1reading);
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setMax(wholeTextPlayer.getDuration());
seekBar.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
seekChange(v);
return false;
}
});
}
public void startPlayProgressUpdater() {
seekBar.setProgress(wholeTextPlayer.getCurrentPosition());
if (wholeTextPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
}
else wholeTextPlayer.pause();
}
// This is event handler thumb moving event
private void seekChange(View v){
if(wholeTextPlayer.isPlaying()){
SeekBar sb = (SeekBar)v;
wholeTextPlayer.seekTo(sb.getProgress());
}
}
}
'
if your plan is to make the method setEngAu() is the controller as you mentioned in the comment. then you you need to use that method like this
public void setEngAu(int enAu)
{
this.EngAu = enAu;
wholeTextPlayer.setDataSource(engAu);
wholeTextPlayer.prepare();
}
you need to implement onPrepared listener from the media player
I do not know your classes but I assume where you say something like;
wholeTextPlayer = new MediaPlayer();
wholeTextPlayer.setOnPreparedListener(this);
here you start the player after it became prepared
public void onPrepared(MediaPlayer player)
{
player.start();
}
Remember to you will need to call wholeTextPlayer.release() if you do not want the player to hold on that resource anymore( think of it as memory issues - recommended if you check the documentation)
EDIT :
I adjusted your code a little, please take a look and let me know.
private SeekBar seekBar;
MediaPlayer wholeTextPlayer;
private int engAu;
final Handler handler = new Handler();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.lesson1grammar);
WholeDefAccPlayer();
final RelativeLayout playerScreen = (RelativeLayout) findViewById(R.id.playerScreen);
final ImageButton btn_player_screen = (ImageButton) findViewById(R.id.btn_player_screen);
btn_player_screen.setOnClickListener(new View.OnClickListener()
{
//this hides/unhides the part of the layout in which the player is
#Override
public void onClick(View arg0)
{
if(playerScreen.isShown())
{
playerScreen.setVisibility(View.GONE);
}
else
{
playerScreen.setVisibility(View.VISIBLE);
}
}
});
final Button btn_default_acc_whole = (Button) findViewById(R.id.btn_default_acc_whole);
btn_default_acc_whole.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{//problem here is
if(playerState == PlayerState_Playing)
{
wholeTextPlayer.pause();
setPlayerState(PlayerState_Paused);
}
else if(playerState == PlayerState_Paused)
{
wholeTextPlayer.start();
setPlayerState(PlayerState_Playing);
}
else
{
setEngAu(R.raw.default_acc_audio);
wholeTextPlayer.start();
startPlayProgressUpdater();
setPlayerState(PlayerState_Playing);
}
}
});
}
public void setEngAu(int engAu)
{
this.engAu = engAu;
if(wholeTextPlayer !=null)
{//just in case you call this method and player was not initialized yet
wholeTextPlayer.release();
}
setPlayerState(PlayerState_Preparing);
wholeTextPlayer = MediaPlayer.create(this, engAu);
}
private void WholeDefAccPlayer()
{
//this line probably will fail because engAu is not really initialized yet, unless you have default value for it
wholeTextPlayer = MediaPlayer.create(this, engAu);
((TextView) findViewById(R.id.getTitleOfAccent)).setText(R.string.btn_lesson1reading);
seekBar = (SeekBar) findViewById(R.id.seekBar);
//you can not call getDuration unless the player is prepared, so this might crash you
// seekBar.setMax(wholeTextPlayer.getDuration());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b)
{
if(playerState != PlayerState_Preparing)
{//if player state is preparing it means we can not seek yet, player.start() must be called first
wholeTextPlayer.seekTo(i);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
});
}
public void startPlayProgressUpdater()
{
if(seekBar.getMax() != wholeTextPlayer.getDuration())
{//in case you change track later, this will check if the seek bar max value with track duration.
// if they are not the same, then it will adjust it for you
seekBar.setMax(wholeTextPlayer.getDuration());
}
seekBar.setProgress(wholeTextPlayer.getCurrentPosition());
if(wholeTextPlayer.isPlaying())
{
Runnable notification = new Runnable()
{
public void run()
{
startPlayProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
}
else wholeTextPlayer.pause();
}
// This is event handler thumb moving event
private void seekChange(View v)
{
if(wholeTextPlayer.isPlaying())
{
SeekBar sb = (SeekBar) v;
wholeTextPlayer.seekTo(sb.getProgress());
}
}
private final int PlayerState_Preparing = 0;
private final int PlayerState_Playing = 1;
private final int PlayerState_Paused = 2;
private int playerState;
private void setPlayerState(int state)
{//this is used to track the player state, because wholeTextPlayer.isPlaying() can return false in many conditions ( too general )
//you can check in the media player documentation to know more details about this
playerState = state;
}

Categories

Resources