0

当我单击Array. 我想要做的是当你点击不同的图片时播放不同的声音。

到目前为止,这是我的 Java 代码:

public class gorendis extends Activity implements MediaPlayer.OnCompletionListener{
    private ImageView play;
    private ImageView a2;
    private MediaPlayer mp;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.huruf);

        play=(ImageView)findViewById(R.id.a1);
        a2=(ImageView)findViewById(R.id.a2);
        //gambar=(ImageButton)findViewById(R.id.gambar);

        //gambar.setImageResource(R.drawable.a1);

        play.setOnClickListener( new View.OnClickListener() {
            public void onClick(View view) {
                play();
            }
        });

        a2.setOnClickListener( new View.OnClickListener() {
            public void onClick(View view) {
                play();
            }
        });

        setup();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void onCompletion(MediaPlayer mp) {
        stop();
    }

    private void stop() {
        // TODO Auto-generated method stub
    }

    private void play() {
        mp.start();
        play.setEnabled(true);
    }

    private void loadClip() {
        try {
            mp=MediaPlayer.create(this, R.raw.doajodoh);
            mp.setOnCompletionListener(this);
        } catch (Throwable t) {
            goBlooey(t);
        }
    }

    private void setup() {
        loadClip();
        play.setEnabled(true);
    }

    private void goBlooey(Throwable t) {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);

        builder.setTitle("Exception!")
               .setMessage(t.toString())
               .setPositiveButton("OK", null)
               .show();
    }
}

主要布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView>

    <LinearLayout>

        <ImageView
            android:id="@+id/play"
            android:layout_width="wrap_content"
            android:layout_height="53px"
            android:background="@android:color/transparent"
            android:scaleType="fitCenter"
            android:src="@drawable/a1" >
        </ImageView>

        <ImageView
            android:id="@+id/a2"
            android:layout_width="wrap_content"
            android:layout_height="53px"
            android:background="@android:color/transparent"
            android:scaleType="fitCenter"
            android:src="@drawable/a2" >
        </ImageView>
    </LinearLayout>

</ScrollView>
4

2 回答 2

2

如果您只是想在 buttonclick 上播放不同的声音,那么我建议您这样做:

  • 使用setTag()按钮的方法。喜欢button1.setTag(0);button1.setTag("guitarmusic");
  • 您会注意到该View.OnClickListener's onClick()方法获得了一个Viewas 参数。
  • 您知道,您OnClickListener仅将其设置为按钮,因此您可以ViewonClick()方法中将其转换为Button喜欢 Button clicked = (Button) view;
  • 然后您可以访问之前为按钮设置的标签,也可以投射它们,例如:Integer buttonTag = (Integer) clicked.getTag();
  • 然后你可以通过一个简单的switch来决定你想要播放的声音,比如:switch(buttonTag){case 0: play first sound... break; ...}

这样你就不需要单独的 OnClickListener 来播放你想要播放的每个声音:)

于 2012-08-04T08:42:40.050 回答
1

最后我找到了这个解决方案..这个工作,即使是长代码!

public class part1 extends Activity implements MediaPlayer.OnCompletionListener{
       /** Called when the activity is first created. */

    private ImageView a2;
    private ImageView a3;
    private ImageView a4;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        a2=(ImageView)findViewById(R.id.a2);
        a3=(ImageView)findViewById(R.id.a3);
        a4=(ImageView)findViewById(R.id.a4);

        a2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                a2();
            }               
        });
        setup();

        a3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                a3();
            }               
        });
        setup2();
        a4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                a4();
            }
        });
        setup3();

        public void onCompletion(MediaPlayer mp) {
        stop();
    }

    private void stop() {
        mp.stop();

    }

    private void a2() {
        mp.stop();
        loadClip();
        mp.start();

        a2.setEnabled(true);
    }
    private void a3() {
        mp.stop();
        loadClip2();
        mp.start();

        a3.setEnabled(true);
    }
    private void a4() {
        mp.stop();
        loadClip3();
        mp.start();

        a4.setEnabled(true);
    }

    private void loadClip() {

        try {
            mp=MediaPlayer.create(this, R.raw.a2);
            mp.setOnCompletionListener(this);

        }
        catch (Throwable t) {
            goBlooey(t);
        }
    }
    private void loadClip2() {

        try {
            mp=MediaPlayer.create(this, R.raw.a3);
            mp.setOnCompletionListener(this);

        }
        catch (Throwable t) {
            goBlooey(t);
        }
    }

    private void loadClip3() {

        try {
            mp=MediaPlayer.create(this, R.raw.a4);
            mp.setOnCompletionListener(this);

        }
        catch (Throwable t) {
            goBlooey(t);
        }
    }

    private void setup() {
        loadClip();
        a2.setEnabled(true);
    }
    private void setup2() {
        loadClip2();
        a3.setEnabled(true);
    }

    private void setup3() {
        loadClip3();
        a4.setEnabled(true);
    }

    private void goBlooey(Throwable t) {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);

        builder
            .setTitle("Exception!")
            .setMessage(t.toString())
            .setPositiveButton("OK", null)
            .show();
    }
于 2012-08-04T07:27:16.860 回答