ios - 在iOS中听不到声音

标签 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

特别是,该类(class)应该(用麦克风)听到音频信号,其中有些声音的频率大于等于18000 Hz。因此,我将这样做:当它识别出频率> = 18000 Hz的音调时,它应该播放声音。
当我尝试在设备上运行该应用程序时,我使用iPhone扬声器听到的声音非常低,但是当我插入耳机时,听到的声音却很大。我试图通过使用模拟器来运行该应用程序,当我使用模拟器时,它运行良好。为什么?您能帮我修好这门课吗?

PS:检测声音的频率我正在使用pitch detector

最佳答案

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

#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方法中,它可以工作。
谢谢!

关于ios - 在iOS中听不到声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22481712/

相关文章:

iphone - iPhone 应用程序与亚马逊 Web 服务后端的架构

ios - 对持续触摸采取行动,而不是仅在触摸开始时采取行动 :

ios - 在代码中使用以下方程式

iphone - 在哪里自定义 UINavigationController 后退按钮?

iphone - 将 iPhone 摄像头直播到媒体服务器的最佳方式是什么?

javascript - 播放音频的定义部分

iphone - 使用哪种技术在iPhone上播放音频?

ios - 新 Alamofire Swift 3 上的 GET 请求

python - 在 Python 中将波转换为分贝

iphone - GLSL ES 2.0(适用于 iPhone/iPod touch/iPad)中的 highp float 的精度是多少?