无法在iOS中听到声音

时间:2014-03-18 14:02:41

标签: ios iphone audio

在我的应用中,我听不到声音。特别是这是我的代码:

#import <QuartzCore/QuartzCore.h>
#import <AudioToolbox/AudioToolbox.h>

#import "ViewController.h"
#import "RIOInterface.h"
#import "KeyHelper.h"
#import "Toast+UIView.h"

@interface ViewController () {
    BOOL watermarkReceived;
    float frequencyRecived;
    CABasicAnimation *theAnimation;
    BOOL water1, water2, water3, water4, noWater;
}

@property(nonatomic)NSTimer *timer, *timer2;
@property(nonatomic,strong)AVAudioPlayer *player;
@property(nonatomic,strong)NSURL *url;

@end

@implementation ViewController

@synthesize isListening;
@synthesize rioRef;
@synthesize currentFrequency;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //self.labelPosition.font=[UIFont fontWithName:@"DBLCDTempBlack" size:20.0];

    NSError *error;

    self.url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"sms_alert_circles" ofType:@"mp3"]];
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.url error:&error];

    AVAudioSession *session = [AVAudioSession sharedInstance];

    NSError *setCategoryError = nil;
    [session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&setCategoryError];

    [self.imageLed setImage:[UIImage imageNamed:@"image_led_red.png"]];
    self.rioRef = [RIOInterface sharedInstance];
    [rioRef setSampleRate:44100];
    [rioRef setFrequency:394];//294
    [rioRef initializeAudioSession];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)startListenWatermark:(UIButton *)sender {
    if ([sender isSelected]) {
        [self stopListener];
        [UIApplication sharedApplication].idleTimerDisabled = NO;
        [sender setSelected:NO];
        [self.imageListening.layer removeAllAnimations];
        [self.imageLed setImage:[UIImage imageNamed:@"image_led_red.png"]];
        //self.labelPosition.font=[UIFont fontWithName:@"DBLCDTempBlack" size:20.0];
        self.labelPosition.text = @"Nessuna postazione";
    } else {
        water1 = water2 = water3 = water4 = NO;
        [self startListener];
        [UIApplication sharedApplication].idleTimerDisabled = YES;
        [sender setSelected:YES];
        [self.imageLed setImage:[UIImage imageNamed:@"image_led_red.png"]];
        self.labelPosition.text = @"Nessuna postazione";
        theAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        theAnimation.duration = 0.4;
        theAnimation.repeatDuration = 10000;
        theAnimation.autoreverses = YES;
        theAnimation.delegate = self;
        theAnimation.fromValue = [NSNumber numberWithFloat:1.0];
        theAnimation.toValue = [NSNumber numberWithFloat:0.1];
        [self.imageListening.layer addAnimation:theAnimation forKey:@"animateOpacity"];
    }
}

#pragma mark Listener methods
- (void)startListener {
    [self.rioRef startListening:self];
}

- (void)stopListener {
    [self.rioRef stopListening];
}

- (void)frequencyChangedWithValue:(float)newFrequency {
    frequencyRecived = newFrequency;
    watermarkReceived = YES;

    if (frequencyRecived > 18000) {
        if (frequencyRecived >= 18000 && frequencyRecived <= 18110 && !water1) {
            [self.timer invalidate];
            self.timer = nil;
            [self performSelectorOnMainThread:@selector(setTextInLabel:) withObject:@"1" waitUntilDone:YES];
            water2 = water3 = water4 = NO;
            water1 = YES;
            noWater = YES;
        }
        if (frequencyRecived >= 18115 && frequencyRecived <= 18250 && !water2) {
            [self.timer invalidate];
            self.timer = nil;
            [self performSelectorOnMainThread:@selector(setTextInLabel:) withObject:@"2" waitUntilDone:YES];
            water1 = water3 = water4 = NO;
            water2 = YES;
            noWater = YES;
        }
        if (frequencyRecived >= 18255 && frequencyRecived <= 18440 && !water3) {
            [self.timer invalidate];
            self.timer = nil;
            [self performSelectorOnMainThread:@selector(setTextInLabel:) withObject:@"3" waitUntilDone:YES];
            water1 = water2 = water4 = NO;
            water3 = YES;
            noWater = YES;
        }
        if (frequencyRecived >= 18445 && !water4) {
            [self.timer invalidate];
            self.timer = nil;
            [self performSelectorOnMainThread:@selector(setTextInLabel:) withObject:@"4" waitUntilDone:YES];
            water1 = water2 = water3 = NO;
            water4 = YES;
            noWater = YES;
        }
    } else {
        if (noWater) {
            [self performSelectorOnMainThread:@selector(noWatermark) withObject:nil waitUntilDone:YES];
            noWater = NO;
        }
    }
}

- (void)noWatermark {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(noPosition:) userInfo:nil repeats:NO];
}

- (void)noPosition:(NSTimer*)aTimer {
    [self performSelectorOnMainThread:@selector(setTextInLabel:) withObject:@"Nessuna postazione" waitUntilDone:YES];
    [self performSelectorInBackground:@selector(redLed) withObject:nil];
    self.timer2 = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(resetFlags) userInfo:nil repeats:NO];
}

- (void)resetFlags {
    water1 = water2 = water3 = water4 = NO;
}

- (void)redLed {
    [self.imageLed setImage:[UIImage imageNamed:@"image_led_red.png"]];
}

- (void)setTextInLabel:(NSString*)position {

    [self.timer invalidate];
    self.timer = nil;



    if ([position isEqualToString:@"Nessuna postazione"]) {
        self.labelPosition.text = position;
    }
    self.labelPosition.text = position;
    if (![position isEqualToString:@"Nessuna postazione"]) {
        [self.player setVolume:1.0];
        [self.player prepareToPlay];
        [self.player play];
        NSString *textForToast = [NSString stringWithFormat:@"Postazione %@", position];
        UIImage *image = [[UIImage alloc]init];
        if ([position isEqualToString:@"1"]) {
            image = [UIImage imageNamed:@"image_smart.png"];
        }
        if ([position isEqualToString:@"2"]) {
            image = [UIImage imageNamed:@"image_500.png"];
        }
        if ([position isEqualToString:@"3"]) {
            image = [UIImage imageNamed:@"image_mini.png"];
        }
        if ([position isEqualToString:@"4"]) {
            image = [UIImage imageNamed:@"image_aygo.png"];
        }
        [self.view makeToast:textForToast duration:5.0 position:@"bottom" title:@"Watermark ricevuto" image:image];
        [self.imageLed setImage:[UIImage imageNamed:@"image_led_green.png"]];
    }
}
@end

特别地,该类应该听到(带有麦克风)音频信号,其中存在频率> = 18000Hz的一些音调。所以我会这样做:当它识别出频率> = 18000 Hz的音调时,它应该发出声音。 当我尝试在设备上运行应用程序时,我通过使用iPhone扬声器听到音量非常低的声音,但是当我插入耳机时,我听到了高音量的声音。我尝试使用模拟器运行应用程序,当我使用模拟器时,它很好用。为什么?你能帮我解决这门课吗?

PS:使用pitch detector检测声音的频率。

1 个答案:

答案 0 :(得分:0)

我解决了,我在这里发布了代码:

#import <QuartzCore/QuartzCore.h>
#import <AudioToolbox/AudioToolbox.h>

#import "ViewController.h"
#import "RIOInterface.h"
#import "KeyHelper.h"
#import "Toast+UIView.h"

@interface ViewController () {
    BOOL watermarkReceived;
    float frequencyRecived;
    CABasicAnimation *theAnimation;
    BOOL water1, water2, water3, water4, noWater;
}

@property(nonatomic)NSTimer *timer, *timer2;
//@property(strong)AVAudioPlayer *player;
@property(nonatomic,strong)NSURL *url;

@end

@implementation ViewController

@synthesize isListening;
@synthesize rioRef;
@synthesize currentFrequency;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

//    NSError *error;
//    
//    self.url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"sms_alert_circles" ofType:@"mp3"]];
//    player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.url error:&error];
//    
//    AVAudioSession *session = [AVAudioSession sharedInstance];
//    
//    NSError *setCategoryError = nil;
//    [session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&setCategoryError];

    [self.imageLed setImage:[UIImage imageNamed:@"image_led_red.png"]];
    self.rioRef = [RIOInterface sharedInstance];
    [rioRef setSampleRate:44100];
    [rioRef setFrequency:394];//294
    [rioRef initializeAudioSession];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)startListenWatermark:(UIButton *)sender {
    if ([sender isSelected]) {
        [self stopListener];
        [UIApplication sharedApplication].idleTimerDisabled = NO;
        [sender setSelected:NO];
        [self.imageListening.layer removeAllAnimations];
        [self.imageLed setImage:[UIImage imageNamed:@"image_led_red.png"]];
        //self.labelPosition.font=[UIFont fontWithName:@"DBLCDTempBlack" size:20.0];
        self.labelPosition.text = @"Nessuna postazione";
    } else {
        water1 = water2 = water3 = water4 = NO;
        [self startListener];
        [UIApplication sharedApplication].idleTimerDisabled = YES;
        [sender setSelected:YES];
        [self.imageLed setImage:[UIImage imageNamed:@"image_led_red.png"]];
        self.labelPosition.text = @"Nessuna postazione";
        theAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        theAnimation.duration = 0.4;
        theAnimation.repeatDuration = 10000;
        theAnimation.autoreverses = YES;
        theAnimation.delegate = self;
        theAnimation.fromValue = [NSNumber numberWithFloat:1.0];
        theAnimation.toValue = [NSNumber numberWithFloat:0.1];
        [self.imageListening.layer addAnimation:theAnimation forKey:@"animateOpacity"];
    }
}

#pragma mark Listener methods
- (void)startListener {
    [self.rioRef startListening:self];
}

- (void)stopListener {
    [self.rioRef stopListening];
}

- (void)frequencyChangedWithValue:(float)newFrequency {
    frequencyRecived = newFrequency;
    watermarkReceived = YES;

    if (frequencyRecived > 18000) {
        if (frequencyRecived >= 18000 && frequencyRecived <= 18110 && !water1) {
            [self.timer invalidate];
            self.timer = nil;
            [self performSelectorOnMainThread:@selector(setTextInLabel:) withObject:@"1" waitUntilDone:YES];
            water2 = water3 = water4 = NO;
            water1 = YES;
            noWater = YES;
        }
        if (frequencyRecived >= 18115 && frequencyRecived <= 18250 && !water2) {
            [self.timer invalidate];
            self.timer = nil;
            [self performSelectorOnMainThread:@selector(setTextInLabel:) withObject:@"2" waitUntilDone:YES];
            water1 = water3 = water4 = NO;
            water2 = YES;
            noWater = YES;
        }
        if (frequencyRecived >= 18255 && frequencyRecived <= 18440 && !water3) {
            [self.timer invalidate];
            self.timer = nil;
            [self performSelectorOnMainThread:@selector(setTextInLabel:) withObject:@"3" waitUntilDone:YES];
            water1 = water2 = water4 = NO;
            water3 = YES;
            noWater = YES;
        }
        if (frequencyRecived >= 18445 && !water4) {
            [self.timer invalidate];
            self.timer = nil;
            [self performSelectorOnMainThread:@selector(setTextInLabel:) withObject:@"4" waitUntilDone:YES];
            water1 = water2 = water3 = NO;
            water4 = YES;
            noWater = YES;
        }
    } else {
        if (noWater) {
            [self performSelectorOnMainThread:@selector(noWatermark) withObject:nil waitUntilDone:YES];
            noWater = NO;
        }
    }
}

- (void)noWatermark {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(noPosition:) userInfo:nil repeats:NO];
}

- (void)noPosition:(NSTimer*)aTimer {
    [self performSelectorOnMainThread:@selector(setTextInLabel:) withObject:@"Nessuna postazione" waitUntilDone:YES];
    [self performSelectorInBackground:@selector(redLed) withObject:nil];
    self.timer2 = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(resetFlags) userInfo:nil repeats:NO];
}

- (void)resetFlags {
    water1 = water2 = water3 = water4 = NO;
}

- (void)redLed {
    [self.imageLed setImage:[UIImage imageNamed:@"image_led_red.png"]];
}

- (void)setTextInLabel:(NSString*)position {

    [self.timer invalidate];
    self.timer = nil;

    NSError *error;

    self.url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"sms_alert_circles" ofType:@"mp3"]];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.url error:&error];

    AVAudioSession *session = [AVAudioSession sharedInstance];

    NSError *setCategoryError = nil;
    [session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&setCategoryError];

    if ([position isEqualToString:@"Nessuna postazione"]) {
        self.labelPosition.text = position;
    }
    self.labelPosition.text = position;
    if (![position isEqualToString:@"Nessuna postazione"]) {
        [player setVolume:1.0];
        [player prepareToPlay];
        [player play];
        NSString *textForToast = [NSString stringWithFormat:@"Postazione %@", position];
        UIImage *image = [[UIImage alloc]init];
        if ([position isEqualToString:@"1"]) {
            image = [UIImage imageNamed:@"image_smart.png"];
        }
        if ([position isEqualToString:@"2"]) {
            image = [UIImage imageNamed:@"image_500.png"];
        }
        if ([position isEqualToString:@"3"]) {
            image = [UIImage imageNamed:@"image_mini.png"];
        }
        if ([position isEqualToString:@"4"]) {
            image = [UIImage imageNamed:@"image_aygo.png"];
        }
        [self.view makeToast:textForToast duration:5.0 position:@"bottom" title:@"Watermark ricevuto" image:image];
        [self.imageLed setImage:[UIImage imageNamed:@"image_led_green.png"]];
    }
}
@end

我只是将AVAudioPlayer的初始化程序放入方法setTextInLabel并且它可以工作。 谢谢!