我正在尝试播放声音,当我点击一个按钮,声音片段是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
任何想法我做错了。
答案 0 :(得分:1)
以下是简单的代码: -
- (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!");
}
}