我已經構建了一些代碼,當按鈕被點擊時應該播放音頻。代碼中沒有錯誤,但是當我打開iOS模擬器並按下按鈕時,沒有聲音播放。該按鈕執行輕擊動畫,但沒有任何反應。 我有AVFoundation和AudioToolbox框架。 我也有我的音頻文件的資源。 我沒有使用斷點。 該按鈕設置爲在Touch Up Inside時使用'playAudio:'第一響應者。聲音沒有在iOS模擬器中播放。沒有錯誤
這裏是我的 'ViewController.h' 文件:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
- (IBAction)playAudio:(id)sender;
@end
這裏是我的 'ViewController.m' 文件:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
@interface ViewController()
@end
@implementation ViewController
- (IBAction)playAudio:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"AudioFile" ofType:@"wav"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
sleep(5);
};
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
的音頻段大約爲3秒。
任何幫助非常感謝! 謝謝!
您的計算機是否靜音並且音量設置得足夠高以至於聽不到? – Kevin
你試過這個http://stackoverflow.com/questions/12260120/no-sound-in-simulator-of-xcode-4-4-1-in-iphone? –