Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 立即停止音乐_Iphone_Objective C_Ios_Xcode_Avfoundation - Fatal编程技术网

Iphone 立即停止音乐

Iphone 立即停止音乐,iphone,objective-c,ios,xcode,avfoundation,Iphone,Objective C,Ios,Xcode,Avfoundation,嗨,我有一个应用程序,你必须按住一个按钮,吹进手机,它会发出声音,下面是代码: #import "AirInstrumentViewController.h" @implementation AirInstrumentViewController @synthesize audiOfA; - (void)didReceiveMemoryWarning { / / Releases the view if it doesn't have a supe

嗨,我有一个应用程序,你必须按住一个按钮,吹进手机,它会发出声音,下面是代码:

      #import "AirInstrumentViewController.h"

@implementation AirInstrumentViewController
    @synthesize audiOfA;
       - (void)didReceiveMemoryWarning
      {
/ / Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

   #pragma mark - View lifecycle



       - (void)viewDidLoad
  {
     [super viewDidLoad];


     NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                          [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                          [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                          [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                          nil];

NSError *error;

recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];

if (recorder) {
    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;
    [recorder record];
    levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
} else
    NSLog([error description]); 
}

          -  (void)levelTimerCallback:(NSTimer *)timer {
[recorder updateMeters];

const double ALPHA = 0.05;
double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;  

if (lowPassResults > 0.55) {
    NSLog(@"Mic blow detected");

    if (aIsBeingTouched == YES) {
        NSString *musicString = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"aifc"];
        audiOfA = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:NULL];
        [audiOfA play];

    } }else {

        [audiOfA stop];
    }


}

   -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


UITouch *touch = [[event allTouches] anyObject];

CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(aImage.frame, touchLocation)) {

    aImage.image = [UIImage imageNamed:@"active.png"];
    aIsBeingTouched = YES;

          }
         }
             -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 [audiOfA stop];

aImage.image = [UIImage imageNamed:@"a.png"];

aIsBeingTouched = NO;

     }

- (void)viewDidUnload
     {

[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
   }



    @end
我希望你明白我想做什么。在这里: 如果(lowPassResults>0.55){ NSLog(“检测到话筒冲击”)

声音持续一段时间,然后停止。触碰结束时也是如此。如果我吹
当我没有按下图像时,当我最终按下图像时,它会播放,即使我没有吹,因为我在没有按下图像时吹了,它可能已经录制了它或其他东西!我不知道如何解决所有这些问题,请帮助!

NSTimer
非常不可靠。根据,引用:

计时器时间间隔的有效分辨率限制为50-100毫秒

可能是因为您调用它们的速度太快(0.03秒或30毫秒相当快),
NSTimer
没有按精确的间隔被调用,因此它正在跳过

您可能希望尝试多线程处理,以便与
UI
相关的所有操作都在主线程上完成,
AVAudio
部分在单独的线程上完成。这是一个解释
NSOperation
的链接,这是一种简单的方法,并且在您提供的代码中应该可以很好地完成


希望这能有所帮助!

我不明白这个链接如何能帮助我真正的音乐停止,就像一秒钟后的想法一样,因为你可以对
AVAudio
部分使用
NSOperation
,这样这个过程就可以在一个单独的线程上完成,从而加快主线程的速度,并改善所有
NSTimer
,以及触摸事件和其他UI元素。你能给我代码和解释吗?我看了参考资料,但我无法理解,对不起
if (aIsBeingTouched == YES) {
    NSString *musicString = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"aifc"];
    audiOfA = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:NULL];
    [audiOfA play];

} }else {

    [audiOfA stop];
}


  }