0

私が取り組んでいるプロジェクトでは、一時停止して続行するストップウォッチが必要です。これまでのところ、基本的な機能はすべて機能していますが、タイマーを一時停止して再開する方法を見つけることができませんでした。参考までに、他の投稿を既に確認しましたが、機能しませんでした。コード:

.h:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface Timer : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate>
{
    AVAudioRecorder *recorder;
    AVAudioPlayer *player;
}

@property (weak, nonatomic) IBOutlet UIButton *recordPauseButton;

@property (weak, nonatomic) IBOutlet UIButton *stopButton;

@property (weak, nonatomic) IBOutlet UILabel *stopwatchLabel;


-(IBAction)recordPauseTapped:(id)sender;

-(IBAction)stopTapped:(id)sender;

 @end

.m:

#import "Timer.h"

 @interface SongIdeasRecording ()

 @property (strong, nonatomic) NSTimer *stopWatchTimer; // Store the timer that fires     after a certain time
 @property (strong, nonatomic) NSDate *startDate; // Stores the date of the click on the start button

 @end


 @implementation Timer

 @synthesize stopButton, playButton, recordPauseButton, stopwatchLabel;

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self) {
     // Custom initialization
 }
     return self;
 }

 - (void)updateTimer
 {
     // Timer is 1/10 of a second so thats what we add to stopwatch
NSTimeInterval timeInterval = 0.1;

// Create a date formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];

// Take the time currently displayed on the stopwatch and add the time interval to it
NSDate *oldDate = [dateFormatter dateFromString:self.stopwatchLabel.text];
NSDate *newDate = [oldDate dateByAddingTimeInterval:timeInterval];
//Get a string representation of the new date
NSString *timeString = [dateFormatter stringFromDate:newDate];
self.stopwatchLabel.text = timeString;
 }

 - (IBAction)recordPauseTapped:(id)sender {
     self.startDate = [NSDate date];

    // Create the stop watch timer that fires every 100 ms
    self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                       target:self
                                                     selector:@selector(updateTimer)
                                                     userInfo:nil
                                                      repeats:YES];



    // Stop the audio player before recording
    if (player.playing) {
       [player stop];
    }

    if (!recorder.recording) {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];

    // Start recording
    [recorder record];
    [recordPauseButton setTitle:@"Pause" forState:UIControlStateNormal];

    } else {

       // Pause recording
       [self.stopWatchTimer invalidate];
       self.stopWatchTimer = nil;
       [self updateTimer];
       [recorder pause];
       [recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];

    }

      [stopButton setEnabled:YES];
      [playButton setEnabled:NO];
 }
 - (IBAction)stopTapped:(id)sender {
     [recorder stop];

     AVAudioSession *audioSession = [AVAudioSession sharedInstance];
     [audioSession setActive:NO error:nil];

     [self.stopWatchTimer invalidate];
     self.stopWatchTimer = nil;
     [self updateTimer];
 }

 - (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully: (BOOL)flag{
     [recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];

     [stopButton setEnabled:NO];
     [playButton setEnabled:YES];
 }

 - (IBAction)playTapped:(id)sender {
     if (!recorder.recording){
     player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
     [player setDelegate:self];
     [player play];
     self.startDate = [NSDate date];
     stopwatchLabel.text = @"00:00:00.000";
     self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                           target:self
                                                         selector:@selector(updateTimer)
                                                         userInfo:nil
                                                          repeats:YES];

     }
  }

 - (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
 {
     [self.stopWatchTimer invalidate];
     self.stopWatchTimer = nil;
     [self updateTimer];
  }


 @end
4

3 に答える 3

0

内のコードにコメントを付けてみてください- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player

私の推測では、新しいタイマーを無効にするトリガーを- (IBAction)recordPauseTapped:(id)sender呼び出していると思います。[player stop]- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

于 2013-08-20T01:49:59.643 に答える
0
- (IBAction)recordPauseTapped:(id)sender {

     if ([stopwatchLabel.text  isEqual: @"00:00:00.000"]) {


     self.startDate = [NSDate date];

     // Create the stop watch timer that fires every 100 ms
     self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                       target:self
                                                     selector:@selector(updateTimer)
                                                     userInfo:nil
                                                      repeats:YES];



     // Stop the audio player before recording
     if (player.playing) {
     [player stop];
    }

if (!recorder.recording) {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];

    // Start recording
    [recorder record];
    [recordPauseButton setTitle:@"Pause" forState:UIControlStateNormal];

}
}else {

    // Pause recording
    [self.stopWatchTimer invalidate];
    self.stopWatchTimer = nil;
    [self updateTimer];
    [recorder pause];
    [recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];

}

[stopButton setEnabled:YES];
[playButton setEnabled:NO];


}
于 2013-08-20T14:47:42.063 に答える
0

あなたの場合、NSDate記録ボタンが最初に押されたストップウォッチラベルの値を計算しています。ストップウォッチ ラベルの値を再計算するたびに、記録ボタンが押された元の日付が反映されるため、この方法でタイマーを一時停止する方法はありません。このメソッドを次のように変更することをお勧めします。

 - (void)updateTimer
 {
     // Timer is 1/10 of a second so thats what we add to stopwatch
     NSTimeInterval timeInterval = 0.1;

    // Create a date formatter
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];

    // Take the time currently displayed on the stopwatch and add the time interval to it
    NSDate *oldDate = [dateFormatter dateFromString:self.stopwatchLabel.text];
    NSDate *newDate = [oldDate dateByAddingTimeInterval:timeInterval];
    //Get a string representation of the new date and BOOM POW.
    NSString *timeString = [dateFormatter stringFromDate:newDate];
    self.stopwatchLabel.text = timeString;
 }

これをテストしていませんが、うまくいくことを願っています。構文の問題もあったとしても、私は驚かないでしょう。また、文字列がself.stopwatchLabel.text開始する形式 (例: 00:00:00.000) に従っていることを確認してください。

于 2013-08-20T01:26:50.570 に答える