ios - 如何增加声音文件(如uiimagefile)的值

标签 ios objective-c xcode image audio

我有以下代码:

。H

 #import <UIKit/UIKit.h>


-(id) initWithFrame: (CGRect)frame andPosition: (NSInteger)pos andValue: (NSInteger)value {
self = [super initWithFrame: frame];
if (self) {
    self.frontImage = [UIImage imageNamed:[NSString stringWithFormat:      @"front%ld.png", (long) (value+1)]];
    self.backImage = [UIImage imageNamed:@"back"];


  }
  return self;
 }


@end

好的,看起来很简单。只是一个带有图像的框架。图片的值是+1。
但是我不知道如何用声音文件来做到这一点。我想“链接”图像和相关的声音文件。文件中有数字,例如front1,front2 ...和无声的有sound1,sound2 ...

感谢帮助!

编辑:类似的东西
self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL   alloc] initFileURLWithPath: [[NSBundle mainBundle]     pathForResource:@"sfx%ld.wav", (long) (value+1)] error:NULL];

不起作用

最佳答案

尝试以下

NSString * strName = [NSString stringWithFormat:@"sfx%ld", (long) (value+1)];
NSString * strPath = [[NSBundle mainBundle] pathForResource:strName ofType:@"wav"];
NSURL * urlPath = [NSURL fileURLWithPath:strPath];
self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:urlPath error:NULL];

关于ios - 如何增加声音文件(如uiimagefile)的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29743146/

相关文章:

ios - Sprite-Kit:移动 SKSpriteNode

ios - AVPlayerViewController - 无自动布局 - 无法同时满足约束条件。

objective-c - 在 Objective C 中,为什么我的核心数据对象永远不会被释放?

ios - “@property(非原子,强)”和“@property”之间的区别

ios - iOS iPhone 中宽度的自动布局问题

cocoa - converter-sample.c 包含重复的 main

ios - MVVM 并推送 ViewController : where to initialize the next ViewController and ViewModel, 并推送新 View ?

c++ - 从 Vector3f 访问数据

iphone - 如何在标签中增加 NSTimer 的时间?

ios - 提交到 App Store 问题 : Unsupported Architecture x86