Reputation: 27
in this program we can play 15 mp3 files with buttons. i simulate the program in "youwave for android" and its works good. when i test it in lenovo ideatab a3000 tablet and xperia z1 phone somtimes when we switch from one button to another it doesn't play sound and it wont play if we push the button again. and we should push other buttons and get back to the button that didn't play sound to play sound. and i checked programming of those buttons. and there is another problem that sometimes when i exit the program the sound wont stop playing . and i added delays recently and it was not diffident before that.
and this is the program :
package fifa1.com.w3;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Fifa1Activity2 extends Activity {
Button btn0;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
Button btn6;
Button btn7;
Button btn8;
Button btn9;
Button btn10;
Button btn11;
Button btn12;
Button btn13;
Button btn14;
Button btn15;
Button btn16;
Button btn17;
ImageView imageView1;
MediaPlayer md = new MediaPlayer();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
btn0 = (Button) findViewById(R.id.btn0);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn5 = (Button) findViewById(R.id.btn5);
btn6 = (Button) findViewById(R.id.btn6);
btn7 = (Button) findViewById(R.id.btn7);
btn8 = (Button) findViewById(R.id.btn8);
btn9 = (Button) findViewById(R.id.btn9);
btn10 = (Button) findViewById(R.id.btn10);
btn11 = (Button) findViewById(R.id.btn11);
btn12 = (Button) findViewById(R.id.btn12);
btn13 = (Button) findViewById(R.id.btn13);
btn14 = (Button) findViewById(R.id.btn14);
btn15 = (Button) findViewById(R.id.btn15);
btn16 = (Button) findViewById(R.id.btn16);
btn17 = (Button) findViewById(R.id.btn17);
imageView1 = (ImageView) findViewById(R.id.imageView1);
btn0.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
imageView1.setImageResource(R.drawable.fifa2000);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2000);
md.start();
}
});
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2001);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2001);
md.start();
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2002);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2002);
md.start();
}
});
btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2003);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2003);
md.start();
}
});
btn4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2004);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2004);
md.start();
}
});
btn5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2005);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2005);
md.start();
}
});
btn6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2006);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2006);
md.start();
}
});
btn7.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2007);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2007);
md.start();
}
});
btn8.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2008);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2008);
md.start();
}
});
btn9.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2009);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2009);
md.start();
}
});
btn10.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2010);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2010);
md.start();
}
});
btn11.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2011);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2011);
md.start();
}
});
btn12.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2012);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2012);
md.start();
}
});
btn13.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2013);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2013);
md.start();
}
});
btn14.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2014);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2014);
md.start();
}
});
btn15.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
imageView1.setImageResource(R.drawable.fifa2015);
md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2015);
md.start();
}
});
btn16.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
md.stop();
SystemClock.sleep(100);
finish();
}
});
btn17.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Fifa1Activity2.this, about_us.class);
startActivity(intent);
finish();
}
});
}
}
xml file:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="20dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2000"/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2001"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2002"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2003"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2004" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2005" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2006" />
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2007" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2008" />
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2009" />
<Button
android:id="@+id/btn10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2010" />
<Button
android:id="@+id/btn11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2011" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="@+id/btn12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2012" />
<Button
android:id="@+id/btn13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2013" />
<Button
android:id="@+id/btn14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2014" />
<Button
android:id="@+id/btn15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIFA2015" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:gravity="center">
<Button
android:id="@+id/btn17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="دربــــاره مــا" />
<Button
android:id="@+id/btn16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="خــــــــــــروج" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center" android:paddingTop="20dp"/>
</LinearLayout>
Upvotes: 0
Views: 25
Reputation: 7571
A couple things you can do
release()
stop()
player.prepare()
before player.start()
Take a look at the life cycle to understand the methods better as well:
Upvotes: 0