【问题标题】:Unable to hear sound in iOS在 iOS 中无法听到声音
【发布时间】:2014-03-18 14:02:41
【问题描述】:

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

#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

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

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

【问题讨论】:

    标签: ios iphone audio


    【解决方案1】:

    我解决了,我把代码贴在这里:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多