嗨,我在这里做一个,我需要显示一些图像,同时我需要播放这些图像相关的声音。使用 mediaplyer 我做了一个它的 plyaing 很好,但是如果我当时点击主页按钮,我的 prblm 就在应用程序的中间音乐仍在播放。但是当用户单击主页按钮时我需要停止音乐,我尝试使用暂停()。但它没有任何帮助我做错了......
GalleryMedia .class:
public class GalleryMedia extends Activity implements OnItemSelectedListener{
public boolean Visibility=true;
Boolean loaded = false;
MediaPlayer intro1, intro2, intro3, intro4, intro5, intro6, intro7, intro8,
intro9, intro10, intro11, intro12, intro13;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intro1 = MediaPlayer.create(this, R.raw.babycooing05);
intro2 = MediaPlayer.create(this, R.raw.bokstavslottet_2);
intro3 = MediaPlayer.create(this, R.raw.bokstavslottet_3);
intro4 = MediaPlayer.create(this, R.raw.bokstavslottet_4);
intro5 = MediaPlayer.create(this, R.raw.bokstavslottet_5);
intro6 = MediaPlayer.create(this, R.raw.bokstavslottet_6);
intro7 = MediaPlayer.create(this, R.raw.bokstavslottet_7);
intro8 = MediaPlayer.create(this, R.raw.bokstavslottet_8);
intro9 = MediaPlayer.create(this, R.raw.bokstavslottet_9);
intro10 = MediaPlayer.create(this, R.raw.bokstavslottet_10);
intro11 = MediaPlayer.create(this, R.raw.bokstavslottet_11);
intro12 = MediaPlayer.create(this, R.raw.bokstavslottet_12);
intro13 = MediaPlayer.create(this, R.raw.starwars);
Gallery g=(Gallery)findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setSpacing(10);
g.setOnItemSelectedListener(this);
}
public class ImageAdapter extends BaseAdapter {
private Context myContext;
private int[] myImageIds = {
R.drawable.bokstavslottet01,
R.drawable.bokstavslottet02,
R.drawable.bokstavslottet03,
R.drawable.bokstavslottet04,
R.drawable.bokstavslottet05,
R.drawable.bokstavslottet06,
R.drawable.bokstavslottet07,
R.drawable.bokstavslottet08,
R.drawable.bokstavslottet09,
R.drawable.bokstavslottet10,
R.drawable.bokstavslottet11,
R.drawable.bokstavslottet12,
R.drawable.bokstavslottet13
};
public ImageAdapter(Context c) { this.myContext = c; }
public int getCount() {
// TODO Auto-generated method stub
return this.myImageIds.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(this.myContext);
i.setImageResource(this.myImageIds[position]);
/* Image should be scaled as width/height are set. */
i.setScaleType(ImageView.ScaleType.FIT_XY);
/* Set the Width/Height of the ImageView. */
i.setLayoutParams(new Gallery.LayoutParams(700, 400));
Integer e = myImageIds.length;
Log.i("","length-------"+e);
return i;
}
/** Returns the size (0.0f to 1.0f) of the views
* depending on the 'offset' to the center. */
public float getScale(boolean focused, int offset) {
/* Formula: 1 / (2 ^ offset) */
return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
}
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
if (position == 0) {
intro1.start();
intro1 = MediaPlayer.create(this, R.raw.babycooing05);
intro1.start();
}
if (position == 1) {
intro1.stop();
intro2.start();
intro2 = MediaPlayer.create(this, R.raw.bokstavslottet_2);
intro2.start();
}
if (position == 2) {
intro2.stop();
intro3.start();
intro3 = MediaPlayer.create(this, R.raw.bokstavslottet_3);
intro3.start();
}
if (position == 3) {
intro3.stop();
intro4.start();
intro4 = MediaPlayer.create(this, R.raw.bokstavslottet_4);
intro4.start();
}
if (position == 4) {
intro4.stop();
intro5.start();
intro5 = MediaPlayer.create(this, R.raw.bokstavslottet_5);
intro5.start();
}
if (position == 5) {
intro5.stop();
intro6.start();
intro6 = MediaPlayer.create(this, R.raw.bokstavslottet_6);
intro6.start();
}
if (position == 6) {
intro6.stop();
intro7.start();
intro7 = MediaPlayer.create(this, R.raw.bokstavslottet_7);
intro7.start();
}
if (position == 7) {
intro7.stop();
intro8.start();
intro8 = MediaPlayer.create(this, R.raw.bokstavslottet_8);
intro8.start();
}
if (position == 8) {
intro8.stop();
intro9.start();
intro9 = MediaPlayer.create(this, R.raw.bokstavslottet_9);
intro9.start();
}
if (position == 9) {
intro9.stop();
intro10.start();
intro10 = MediaPlayer.create(this, R.raw.bokstavslottet_10);
intro10.start();
}
if (position == 10) {
intro10.stop();
intro11.start();
intro11 = MediaPlayer.create(this, R.raw.bokstavslottet_11);
intro11.start();
}
if (position == 11) {
intro10.stop();
intro12.start();
intro12 = MediaPlayer.create(this, R.raw.bokstavslottet_12);
intro12.start();
}
if (position == 12) {
intro12.stop();
intro13.start();
intro13 = MediaPlayer.create(this, R.raw.starwars);
intro13.start();
}
}
@Override
protected void onPause() {
super.onPause();
if (intro1 != null){
intro1.stop();
if (isFinishing()){
intro1.stop();
intro1.release();
}
}
if (intro2 != null){
intro2.stop();
if (isFinishing()){
intro2.stop();
intro2.release();
}
}
if (intro3 != null){
intro3.stop();
if (isFinishing()){
intro3.stop();
intro3.release();
}
}
if (intro4 != null){
intro4.stop();
if (isFinishing()){
intro4.stop();
intro4.release();
}
}
if (intro5 != null){
intro5.stop();
if (isFinishing()){
intro5.stop();
intro5.release();
}
}
if (intro6 != null){
intro6.stop();
if (isFinishing()){
intro6.stop();
intro6.release();
}
}
if (intro7 != null){
intro7.stop();
if (isFinishing()){
intro7.stop();
intro7.release();
}
}
if (intro8 != null){
intro8.stop();
if (isFinishing()){
intro8.stop();
intro8.release();
}
}
if (intro9 != null){
intro9.stop();
if (isFinishing()){
intro9.stop();
intro9.release();
}
}
if (intro10 != null){
intro10.stop();
if (isFinishing()){
intro10.stop();
intro10.release();
}
}
if (intro11 != null){
intro11.stop();
if (isFinishing()){
intro11.stop();
intro11.release();
}
}
if (intro12 != null){
intro12.stop();
if (isFinishing()){
intro12.stop();
intro12.release();
}
}
if (intro13 != null){
intro13.stop();
if (isFinishing()){
intro13.stop();
intro13.release();
}
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}