8

xCode を使用して iOS アプリのコーディングを始めたばかりで、物事がどのように機能するかを見つけるのは簡単でも直観的でもありません。私はこれに非常に慣れておらず、私のアプリは非常にゆっくりと進みます^^. とにかく、少なくともiOS7で試しています。

カスタムセルと動的な高さを使用して動的なテーブルを作成できましたが、問題の解決策が見つかりません...おそらく正しい場所で検索しなかったのでしょう... とにかく。

次の行のおかげで、オーディオを再生しています。

NSString *path = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"mp3"];
AVAudioPlayer *audio = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];

[audio play];
[audio updateMeters];

オーディオが再生されます。しかし、私にはコントロールがありません。再生/一時停止ボタンの追加に成功しましたが、オーディオ内を移動するにはどうすればよいですか? すべてのインターフェイスをコーディングする必要がありますか? ボタンと応答性の高い進行状況バーを備えたシンプルなインターフェイスはありませんか?

コードを書かなければならない場合は、うーん... どこから始めればよいでしょうか?

どうもありがとう!

4

2 に答える 2

17

AVAudioPlayer の playAtTime: メソッドで UISlider を使用します。AVAudioPlayer には組み込みのシーク バーはありません。

このサンプル コードを確認してください。クラス avTouchController に必要なものが実装されています。

https://developer.apple.com/library/ios/samplecode/avTouch/Introduction/Intro.html

インターフェイスに UISLider を追加し、valueChanged: を seekTime: メソッドにリンクします。

.h で

@property (nonatomic, weak) IBOutlet UISlider *seekbar;

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

@property (nonatomic, strong) NSTimer *updateTimer;

AVAudioPlayerをロードした後、viewDidLoadの.mで、

NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"There's A Long, Long Trail A-Winding" ofType:@"mp3"]];

AVAudioPlayer *audio = [[AVAudioPlayer alloc]
                        initWithContentsOfURL:fileURL error:nil];

self.audioPlayer = audio;

self.seekbar.minimumValue = 0;

self.seekbar.maximumValue = self.audioPlayer.duration;

[[self audioPlayer] play];

self.updateTimer =     [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateSeekBar) userInfo:nil repeats:YES]

次のメソッドを追加します

- (void)updateSeekBar{
float progress = self.audioPlayer.currentTime;
[self.seekbar setValue:progress];
}

- (IBAction)seekTime:(id)sender {

self.audioPlayer.currentTime = self.seekbar.value;

}
于 2013-10-28T15:21:11.967 に答える
2
Play audio and update UISlider with it.

-(void)viewWillAppear:(BOOL)animated
{    
    NSString *soundFile=[[NSBundle mainBundle] pathForResource:@"name of your audio file." ofType:@".mp3"];

    NSURL *soundFileUrl=[NSURL fileURLWithPath:soundFile];
    soundPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:soundFileUrl error:nil];

    [soundPlayer prepareToPlay];
    soundPlayer.delegate=self;


    slider.maximumValue=[soundPlayer duration];
    slider.minimumValue=0.0;
    soundPlayer.currentTime=slider.value;

    [soundPlayer play];

    [self startTimer];
}

#pragma mark Timer Methods

- (void)startTimer
{

    timerForPlaying= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
}

- (void)stopTimer
{
    [timerForPlaying invalidate];
    timerForPlaying = nil;
}

// This method for updating UISlider when sound start to play.
- (void)updateSlider
{
    [slider setValue:soundPlayer.currentTime animated:NO];
    startTime=slider.value;
}

// When we adjust sound according to slider value connect this method to your slider value change event.

-(void)valueChange:(id)sender
{
   soundPlayer.currentTime=slider.value;

}
于 2014-01-16T09:14:15.770 に答える