Start a view-based project in xcode, and use interface builder, editing MyProjectViewController.xib, to put together a series of buttons in shapes and colors that resemble an on-screen keyboard.
Edit MyProjectViewController.h and .m to add a bunch of methods with signatures like this:
Now you can go back to interface builder, and right-click-drag each button to the File's Owner object (of type MyProjectViewController). When you do this, a menu pops up asking for which method to attach the button to - choose the appropriate method for that key.
Now add a sound file for each of the keys you want to play. For example, let's say you have a file called midC.caf (encoded in, say, linear PCM format, as one of the valid sound file formats). Copy that file into your project directory, and add it to the project by right-clicking (in xcode) on your Resources folder, and choosing Add > Existing Files.. and then choosing your sound file.
Next you need to add the AudioToolbox library as a dependency. Do this by right-clicking on the Frameworks folder, and choosing Add > Existing Frameworks.. and then browsing for the AudioToolbox library and choosing it.
Meanwhile, back in MyProjectViewController.m, add code like this:
Hit Command-Enter to compile and run, and make some music!
Version 2 Slightly more elegant.
Programmatically construct a keyboard UI with one array for white keys, and another for black (since they'll have slightly different coordinates). I recommend using custom UIButton types with images for pressed and nonpressed versions of each key type (black, C/F, B/E, and A/D/G, which are middle white keys - I'm account for key indents here).
This would allow you to store pointers to these buttons in an array, and have a single method that could play the sound for any key, by also storing sound ID's in a corresponding array.
You might think you'd want to create the sounds programmatically, as they are somewhat mathematical in nature. But the AudioQueue services, which you would use to do so, are significantly more work to deal with than the above code, so I'd actually recommend just using sound bytes, even in an "elegant" version. It might even sound better that way, anyway.
By the way, your question makes it sound like you're thinking of this as a quicky and easy starter app, but I'm not so sure. Learning to develop iPhone apps is fun, but takes some serious time and effort to do well. If you're interested in starter app ideas, I'd suggest looking for things that exercise one new programming skill at a time, such as learning to work with the ubiquitous UITableView, or a quick app that lets you take a picture and attach it to an email, for example (that's easy because the iPhone SDK has great support for photo-taking and email-composing).
Same way for black keys using different coordinates.
Also I am able to play sound on key pressed Using AVPlayer.(on selector touch down)
So far so good but when user slides across the keys, piano should play respected sound.
To achive this is tried with
1) Used UIControl events for UIButton. DragEnter, drag exit, drag inside. But it will not give me which button is pressed exactly, as drag exit location is way outside the drawn button
2) Used touchesBegan, touchesEnded, touchesMoved of main view(in which black and white keys are placed).
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
currentView = [self.view hitTest:touchLocation withEvent:nil]; //get top view
if(currentView != self.view){ //check current view is main view, do nothing
if(currentView == prevView){ //check current view is prev view, keep highlight image
currentView.backgroundColor = [UIColor colorWithPatternImage:whiteImageHighlighed];
}else{ //view changed, change image and play sound
prevView.backgroundColor = [UIColor colorWithPatternImage:whiteImage];
currentView.backgroundColor = [UIColor colorWithPatternImage:whiteImageHighlighed];
[self pressButtonWhiteDown1:currentView.tag];
prevView = currentView;
}
}
}
In this approach it gives wrong view when user slides on white keys and comes across X position of black keys (as black key is on top of white key in main view):
One way would be to take picture(s) of a piano keyboard and use the images as the starting point.
Another way would be to draw the keys in Photoshop or other app, in both the default and the key-pressed positions, and use those as the base.
Do a search on the App store and look at the different ways piano keyboards have been implemented. There are a few, and some look more realistic than others.
Doing a good representation of a piano keyboard isn't going to be easy, especially if you try to get the glossy look of the keys and create realistic movement via animation, but it is doable. At the very least, you should study Quartz 2D and possibly some CAAnimation.
发布评论
评论(5)
版本 1 更容易编码,但不太优雅。
在 xcode 中启动一个基于视图的项目,并使用界面生成器编辑
MyProjectViewController.xib
,将一系列形状和颜色类似于屏幕键盘的按钮组合在一起。编辑
MyProjectViewController.h
和.m
添加一堆具有如下签名的方法:现在您可以返回界面生成器,然后右键单击并拖动每个按钮到文件的所有者对象(
MyProjectViewController
类型)。当您执行此操作时,会弹出一个菜单,询问将按钮附加到哪种方法 - 为该键选择适当的方法。现在为您要演奏的每个键添加一个声音文件。例如,假设您有一个名为
midC.caf
的文件(以线性 PCM 格式编码,作为 有效的声音文件格式)。将该文件复制到您的项目目录中,然后通过右键单击(在 xcode 中)您的 Resources 文件夹,然后选择“添加”>“将其添加到项目中”。现有文件..然后选择您的声音文件。接下来,您需要添加 AudioToolbox 库作为依赖项。通过右键单击 Frameworks 文件夹并选择“添加”>“框架”来执行此操作。现有框架..然后浏览 AudioToolbox 库并选择它。
同时,回到
MyProjectViewController.m
中,添加如下代码:按 Command-Enter 编译并运行,并制作一些音乐!
版本 2 稍微优雅一些。
以编程方式构建一个键盘 UI,其中一个数组用于白键,另一个数组用于黑键(因为它们的坐标略有不同)。我建议使用自定义
UIButton
类型,其中包含每种按键类型的按下和未按下版本的图像(黑色、C/F、B/E 和 A/D/G,它们是中间的白键 - I' m 说明此处的关键缩进)。这将允许您将指向这些按钮的指针存储在一个数组中,并且通过将声音 ID 存储在相应的数组中,可以使用一个方法来播放任何键的声音。
您可能认为您想要以编程方式创建声音,因为它们本质上有些数学性质。但是
AudioQueue
< /a> 服务(您将用来执行此操作)比上述代码要处理的工作要多得多,因此我实际上建议仅使用声音字节,即使在“优雅”版本中也是如此。无论如何,这样听起来可能会更好。顺便说一句,你的问题听起来好像你认为这是一个快速而简单的入门应用程序,但我不太确定。学习开发 iPhone 应用程序很有趣,但需要花费一些时间和精力才能做好。如果您对入门应用程序创意感兴趣,我建议您寻找能够一次锻炼一项新编程技能的东西,例如学习使用无处不在的
UITableView
,或者一个快速应用程序,让例如,您拍摄一张照片并将其附加到电子邮件中(这很容易,因为 iPhone SDK 对拍照和撰写电子邮件提供了强大的支持)。玩得开心!
Version 1 Easier to code, not so elegant.
Start a view-based project in xcode, and use interface builder, editing
MyProjectViewController.xib
, to put together a series of buttons in shapes and colors that resemble an on-screen keyboard.Edit
MyProjectViewController.h
and.m
to add a bunch of methods with signatures like this:Now you can go back to interface builder, and right-click-drag each button to the File's Owner object (of type
MyProjectViewController
). When you do this, a menu pops up asking for which method to attach the button to - choose the appropriate method for that key.Now add a sound file for each of the keys you want to play. For example, let's say you have a file called
midC.caf
(encoded in, say, linear PCM format, as one of the valid sound file formats). Copy that file into your project directory, and add it to the project by right-clicking (in xcode) on your Resources folder, and choosing Add > Existing Files.. and then choosing your sound file.Next you need to add the AudioToolbox library as a dependency. Do this by right-clicking on the Frameworks folder, and choosing Add > Existing Frameworks.. and then browsing for the AudioToolbox library and choosing it.
Meanwhile, back in
MyProjectViewController.m
, add code like this:Hit Command-Enter to compile and run, and make some music!
Version 2 Slightly more elegant.
Programmatically construct a keyboard UI with one array for white keys, and another for black (since they'll have slightly different coordinates). I recommend using custom
UIButton
types with images for pressed and nonpressed versions of each key type (black, C/F, B/E, and A/D/G, which are middle white keys - I'm account for key indents here).This would allow you to store pointers to these buttons in an array, and have a single method that could play the sound for any key, by also storing sound ID's in a corresponding array.
You might think you'd want to create the sounds programmatically, as they are somewhat mathematical in nature. But the
AudioQueue
services, which you would use to do so, are significantly more work to deal with than the above code, so I'd actually recommend just using sound bytes, even in an "elegant" version. It might even sound better that way, anyway.By the way, your question makes it sound like you're thinking of this as a quicky and easy starter app, but I'm not so sure. Learning to develop iPhone apps is fun, but takes some serious time and effort to do well. If you're interested in starter app ideas, I'd suggest looking for things that exercise one new programming skill at a time, such as learning to work with the ubiquitous
UITableView
, or a quick app that lets you take a picture and attach it to an email, for example (that's easy because the iPhone SDK has great support for photo-taking and email-composing).Have fun!
感谢您的回复...
我几乎完成了钢琴用户界面的开发。
我使用编程方法来创建黑白键。
使用不同坐标的黑键也是如此。
我还可以使用 AVPlayer 在按下的按键上播放声音。(在选择器触地时)
到目前为止一切都很好,但是当用户在按键上滑动时,钢琴应该播放受人尊敬的声音。
要实现此目的,请尝试
使用 1) UIButton 的 UIControl 事件。拖入、拖出、拖入。
但它不会告诉我到底按下了哪个按钮,因为
拖动退出位置远离绘制按钮
2) 使用主视图的touchesBegan、touchesEnded、touchesMoved(其中放置了黑白键)。
在这种方法中,当用户在白键上滑动并遇到黑键的 X 位置时,它会给出错误的视图(因为黑键位于主视图中白键的顶部):
alt text http://img215.yfrog.com/img215/1720/pianohelp.png
还有其他方法或我正在做的事情吗错在其中。请指导我。
是否可以从触摸点获得准确的视图?
谢谢。
Thanks for replying...
I am almost done with developing UI of piano.
I have used programmatic approach to create black and white keys.
Same way for black keys using different coordinates.
Also I am able to play sound on key pressed Using AVPlayer.(on selector touch down)
So far so good but when user slides across the keys, piano should play respected sound.
To achive this is tried with
1) Used UIControl events for UIButton. DragEnter, drag exit, drag inside.
But it will not give me which button is pressed exactly, as
drag exit location is way outside the drawn button
2) Used touchesBegan, touchesEnded, touchesMoved of main view(in which black and white keys are placed).
In this approach it gives wrong view when user slides on white keys and comes across X position of black keys (as black key is on top of white key in main view):
alt text http://img215.yfrog.com/img215/1720/pianohelp.png
Is there any other approach or something I/m doing wrong in it. please guide me.
Is it possible to get exact view from touch point?
Thanks.
一种方法是拍摄钢琴键盘的照片并使用这些图像作为起点。
另一种方法是在 Photoshop 或其他应用程序中在默认位置和按键位置绘制按键,并使用它们作为基础。
在应用商店中搜索一下,看看钢琴键盘的不同实现方式。有一些,有些看起来比其他的更现实。
很好地表现钢琴键盘并不容易,特别是如果您尝试获得琴键的光泽外观并通过动画创建逼真的动作,但这是可行的。至少,你应该学习 Quartz 2D 可能还有一些 CAAnimation。
One way would be to take picture(s) of a piano keyboard and use the images as the starting point.
Another way would be to draw the keys in Photoshop or other app, in both the default and the key-pressed positions, and use those as the base.
Do a search on the App store and look at the different ways piano keyboards have been implemented. There are a few, and some look more realistic than others.
Doing a good representation of a piano keyboard isn't going to be easy, especially if you try to get the glossy look of the keys and create realistic movement via animation, but it is doable. At the very least, you should study Quartz 2D and possibly some CAAnimation.
我做了一些非常相似的事情。我使用了普通的圆角按钮,并让每个按钮都播放 WAV 声音。效果很好!
I did something very similar. I used normal buttons with the round corners and made each of them play an WAV sound. It works nicely!
您可以从这里获取钢琴键声音:http://www.sonicspot。 com/news/free-musical-instrument-samples.html。文件很大,您可以使用 Audacity 剪切它们并导出为 .wav
You can get piano key sounds from here: http://www.sonicspot.com/news/free-musical-instrument-samples.html . The files are quite big, you can cut them with Audacity and export to .wav