![](/img/trans.png)
[英]Button in UICollectionViewCell Makes Keyboard Sound When Clicked
[英]No sound when button clicked
当我单击按钮时,我尝试播放声音,声音剪辑为10秒,应用程序运行良好,当我单击按钮时,什么也没有发生,没有错误或什么都没有,这是我的代码
.m file
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
- (IBAction)Play:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle]pathForResource:@"Blesgaes" ofType:@"mp3"];
NSURL *audioURL =[NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
}
.h file
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController
- (IBAction)Play:(id)sender;
我添加了AVFoundation框架,并且按照以下步骤操作
http://looksok.wordpress.com/2013/01/19/ios-tutorial-play-sound/ http://www.techotopia.com/index.php/Playing_Audio_on_an_iPhone_using_AVAudioPlayer_(iOS_6)http:// code-and- coffee.blogspot.in/2012/09/how-to-play-audio-in-ios.html
任何想法我在做什么错。
下面是简单的代码:
- (IBAction)playAudio:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle
mainBundle] pathForResource:@"Woof"
ofType:@"mp3"];
NSURL *audioURL = [NSURL
fileURLWithPath:audioPath];
NSError *audioError = [[NSError alloc] init];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:audioURL
error:&audioError];
if (!audioError) {
[audioPlayer play];
NSLog(@"Woof!");
}
else {
NSLog(@"Error!");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.