2012-12-14 12 views
6

AVAudioRecorderを使用して音声を録音しています。録音した音声の正確な時間を取得したいのですが、どうすれば得ることができますか?録音したオーディオの時間をiphoneでどのように取得できますか?

私はこれ試してみました:

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:avAudioRecorder.url options:nil]; 
CMTime time = asset.duration; 
double durationInSeconds = CMTimeGetSeconds(time); 

しかし、私のtime変数リターンNULLとdurationInSecondsリターン「ナン」、それはナンで何を意味しないし。

UPDATE

user1903074答えは私の問題を解決したが、ただ好奇心のために、thairはAVAudioplayerせずにそれを行うにはどのような方法です。

+1

nanは数字ではありません。資産もNULLですか? – geraldWilliam

+0

私はそのことを知らない。 –

答えて

13

AVAudioPlayerAVAudioRecorderを使用している場合は、audioPlayer.durationを取得して時間を得ることができます。

このようです。

NSError *playerError; 

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:yoururl error:&playerError]; 

NSlog(@"%@",audioPlayer.duration); 

しかし、あなたがAVAudioRecorderAVAudioPlayerを使用している場合にのみ。

UPDATE

それとも、このように行うことができます。

//put this where you start recording 
    myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; 

// a method for update 
- (void)updateTime { 
    if([recorder isRecording]) 
    { 

     float minutes = floor(recorder.currentTime/60); 
     float seconds = recorder.currentTime - (minutes * 60); 

     NSString *time = [[NSString alloc] 
            initWithFormat:@"%0.0f.%0.0f", 
            minutes, seconds]; 
    } 
} 

そのいくつかのマイクロ秒の値であり、私はすべてのthats .butそれをクリップする方法を知らないbecouseあなたには、いくつかの遅延を得ることができます鋼。

+0

はい、AVAudioRecorderでAVAudioPlayerを使用しています。 –

+0

this work great –

+2

@脂質:しかし、録音されたサウンドの持続時間は、AVAudioPlayerを使って再生されたサウンドと必ずしも同じではないと思います。 (私が間違っていれば私を訂正してください) – iShwar

1

私の解決方法は、単に開始日を追跡し、最後に経過時間を計算することです。 ユーザーがレコーダーを起動して停止しているので、私にとってはうまくいった。

レコーダークラスで

NSDate* startTime; 
NSTimeInterval duration; 

-(void) startRecording 
{ 
    //start the recorder 
    ... 
    duration = 0; 
    startTime = [NSDate date]; 

} 

-(void) stopRecording 
{ 
    //stop the recorder 
    ... 
    duration = [[NSDate date] timeIntervalSinceDate:startRecording]; 
} 
0

私は元の質問は、Objective-Cの中に答えを探していたけど、スウィフトにそうするお探しの方のために、これはスウィフト4.0で動作します:

let recorder = // Your instance of an AVAudioRecorder 
let player = try? AVAudioPlayer(contentsOf: recorder.url) 
let duration = player?.duration ?? 0.0 

あなたが有効なレコーダーを持っていると仮定すると、継続時間の値はTimeIntervalとして与えられます。

関連する問題