我应该使用什么来创建钢琴应用程序?图像视图等?

发布于 2025-01-01 03:55:13 字数 814 浏览 2 评论 0原文

我不知道我的钢琴应用程序应该使用哪种小部件。

我尝试使用 ImageView 作为按键,并向其附加 setOnTouchListener 。 它可以工作,但不知怎的,图像应该完成它产生的声音,然后你才能再次按下它。我使用 MediaPlayer 来播放声音。我还想用按键制作“滑动”的东西,我试图使用这行代码:

final MediaPlayer whiteP1 = MediaPlayer.create(this, R.raw.white_1);
white1.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub

// to make the keys work (produce sounds) i removed this line of codes:
   int i = event.getAction(); // i removed this line and the if statement and left the whiteP1.start();
if(i == MotionEvent.ACTION_MOVE)
{ 
whiteP1.start(); 
}
return false;
}
});

问题是它在模拟器上不起作用,当我在 Android 手机上尝试它时,它不起作用。

我还使用RelativeLayout使钢琴的白键和黑键相互重叠。我不知道它是否适用于每种屏幕分辨率,我也想听听您对此的意见。

任何帮助将不胜感激。谢谢!!

I don't know what kind of widget should i use for my piano app.

I've tried to use an ImageView for the keys and attached a setOnTouchListener to it.
It works but somehow the image should finish the sound it produced before you can press it again.I used MediaPlayer for the sounds. I also want to make the "slide" thing with the keys and i was trying to use this lines of code:

final MediaPlayer whiteP1 = MediaPlayer.create(this, R.raw.white_1);
white1.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub

// to make the keys work (produce sounds) i removed this line of codes:
   int i = event.getAction(); // i removed this line and the if statement and left the whiteP1.start();
if(i == MotionEvent.ACTION_MOVE)
{ 
whiteP1.start(); 
}
return false;
}
});

problem is it doesn't work on the emulator and when i tried it on an android phone, it didn't work.

i also used RelativeLayout to make the white and black keys of the piano overlap with each other. I don't know if it works perfectly with every screen resolution and i want your opinions regarding this one too.

any help would be appreciated. thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷默言语 2025-01-08 03:55:13

您正在使用此代码阻塞 GUI 线程。创建一个线程,并在另一个线程上播放媒体。最好的方法是使用 AsynTask 类。但在这种情况下,您可以使用更加简化的可运行方法。看看吧。

final MediaPlayer whiteP1 = MediaPlayer.create(this, R.raw.white_1);
white1.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public void onTouch(View v, MotionEvent event) {
        new Thread(new Runnable() {
            public void run() {
                int i = event.getAction();
                whiteP1.start();
                if(i == MotionEvent.ACTION_MOVE)    {
                    whiteP1.start();
                }
            }
        }).start();
    }
});

我还没有测试过这段代码,但它应该可以工作。如果您有任何疑问,请随时询问。

You are blocking the GUI thread with this code. Create a thread, and play the media on the other thread. And the best way to do the same is to make use of the AsynTask class. But in this case you can make use of much simplified runnable method. Take a look.

final MediaPlayer whiteP1 = MediaPlayer.create(this, R.raw.white_1);
white1.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public void onTouch(View v, MotionEvent event) {
        new Thread(new Runnable() {
            public void run() {
                int i = event.getAction();
                whiteP1.start();
                if(i == MotionEvent.ACTION_MOVE)    {
                    whiteP1.start();
                }
            }
        }).start();
    }
});

I have not tested this code, but it should probably work. Feel free to ask if you have nay questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文