2016-03-29 19 views
-1

オーディオがセッション中に停止したときに時間を節約し、次のセッションで停止点から再生を続けるにはどうすればよいですか?オーディオファイルの位置を保存する方法は?目的 - C

私のコードは:

- (void)initPlayer:(NSString*) audioFile fileExtension:(NSString*)fileExtension 
{ 

NSURL *audioFileLocationURL = [[NSBundle mainBundle] URLForResource:audioFile withExtension:fileExtension]; 
NSError *error; 
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error]; 

if ([audioFile isEqualToString:@"2"]) { 
    _index = 1; 
} 
else if ([audioFile isEqualToString:@"3"]) { 
    _index = 2; 
} 

[self song]; 

} 
- (void)playAudio { 
[self.audioPlayer play]; 


} 

- (void)pauseAudio { 
[self.audioPlayer pause]; 

} 
- (BOOL)isPlaying { 
return [self.audioPlayer isPlaying]; 
} 
-(NSString*)timeFormat:(float)value{ 

float minutes = floor(lroundf(value)/60); 
float seconds = lroundf(value) - (minutes * 60); 

int roundedSeconds = lroundf(seconds); 
int roundedMinutes = lroundf(minutes); 

NSString *time = [[NSString alloc] 
        initWithFormat:@"%d:%02d", 
        roundedMinutes, roundedSeconds]; 
return time; 
} 
- (void)setCurrentAudioTime:(float)value { 
[self.audioPlayer setCurrentTime:value]; 
} 
- (NSTimeInterval)getCurrentAudioTime { 
return [self.audioPlayer currentTime]; 
} 
- (float)getAudioDuration { 
return [self.audioPlayer duration]; 
} 

答えて

0

AVPlayerのcurrentTimeプロパティを使用できます。現在のAVPlayerItemの再生時間を返します。 CURRENTTIMEによって返さCMTIMEを永続化する

[self.player seekToTime:storedPlaybackTime]; 

https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVPlayer_Class/index.html#//apple_ref/doc/uid/TP40009530-CH1-SW2

https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVPlayer_Class/index.html#//apple_ref/occ/instm/AVPlayer/seekToTime%3a

、あなたは:

次のセッションで再生時間を復元するには、あなたがAVPlayerのseekToTimeに保存された時間を渡すことができますNSValueが提供するAVFoundationの便利なメソッドを使用できます。

valueWithCMTime使用し、NSValueでCMTIMEをラップする:あなたはNSValueのインスタンスでCMTIME構造体を包んだ後

CMTime persistedTime = [storeValue CMTimeValue]; 

:永続値、使用からCMTIME構造体を取得するには

[NSValue valueWithCMTime:player.currentTime]; 

をキー付きアーカイバ& NSDataを使用して、時間をディスクに書き込むことができます。

NSHipsterは、そのトピックについての良い記事があります。http://nshipster.com/nscoding/

+0

私は 'CMTIME時間= _audioPlayer.currentTimeは//現在time' ' [self.playerのseekToTime:時間]を忘れないでくださいを使用します。 // transient'しかし、動作しません。どうして? – user

0

最も簡単な方法は、曲の名前のローカルDBを維持すること、それが停止した場合、DBにそのデータを追加します。その後、再生が再開したら、ローカルのデータベースが最初にエントリを持っているかどうかチェックします。開始から継続しない場合。

また、曲が終了してもエントリが作成されていないことを確認してください。

このアイデアは役に立ちます。

関連する問題