2012-03-13 9 views
1

私のプロジェクトでは、私は歌を再生するAVAudioPlayerを持っています。私はそれを一時停止してからもう一度ヒットした後、一時停止した場所ではなく上からやり直します。なぜそれが起こっているのですか? また、最初に再生を押すと、曲が3〜4秒ほど読み込まれます。私はそれを他のスレッドで読み込んで解決したと思ったが、それでもまだ読み込みが遅すぎる(少なくともビューはもう凍結しない)。どうすれば修正できますか?コードは次のとおりです。AVAudioPlayer一時停止の問題

- (IBAction)play:(id)sender { 
    songIsCurrentlyPaused = NO; 
    if(songIsCurrentlyPaused==YES){ 
    [self.background play]; 
    } else { 
    playQueue = dispatch_queue_create("volume_change", NULL); 
    dispatch_async(playQueue, ^{ NSString *filePath = 
     [[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"]; 
     NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath]; 
     self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; 
     self.background.delegate = self; 
     [self.background setNumberOfLoops:1]; 
     [self.background setVolume:0.5]; 
     [self.background play]; }); 

     [trackNameLabel setText:@"Currently playing :\n some_song"]; 
     self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES]; 
    } 
} 

- (IBAction)pause:(id)sender { 
    songIsCurrentlyPaused = YES; 
    [self.background pause]; 
    [trackNameLabel setText:@"Currently playing : some_song (paused)"]; 
    [self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES]; 
} 

ありがとうございます!

答えて

1

あなたはplay:

の先頭にNOsongIsCurrentlyPausedを設定しているが、それをコメントアウトしてみてください。

- (IBAction)play:(id)sender { 
    //songIsCurrentlyPaused = NO; 
    if(songIsCurrentlyPaused==YES){ 
    [self.background play]; 
    } else { 
    playQueue = dispatch_queue_create("volume_change", NULL); 
    dispatch_async(playQueue, ^{ NSString *filePath = 
     [[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"]; 
     NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath]; 
     self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; 
     self.background.delegate = self; 
     [self.background setNumberOfLoops:1]; 
     [self.background setVolume:0.5]; 
     [self.background play]; }); 

     [trackNameLabel setText:@"Currently playing :\n some_song"]; 
     self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES]; 
    } 
} 

- (IBAction)pause:(id)sender { 
    songIsCurrentlyPaused = YES; 
    [self.background pause]; 
    [trackNameLabel setText:@"Currently playing : some_song (paused)"]; 
    [self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES]; 
} 

あなたがその最初の一時停止を取り除きたい場合は、つもりだ完全に再編成する必要がありあなたのプレーヤーの設定。ユーザーが再生ボタンを押すとも呼び出すことができます前に、それを初期化します。

[self.background prepareToPlay]; 

曲をプリロードなります。また、songIsCurrentlyPaused = NO;をコード内のある場所に移動してください。

EDIT:

はあなたがloadViewまたはviweDidLoadのようなどこかにあなたの初期化コードを移動する必要があり、最初の遅延を取り除くために。

//initialization code 
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"]; 
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath]; 
    self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; 
    self.background.delegate = self; 
    [self.background setNumberOfLoops:1]; 
    [self.background setVolume:0.5]; 
    [self.background prepareToPlay]; 

あなたがバックグラウンドスレッドでデータ をプリロードを検討するためにwan'tかもしれないので、今これはUI表示の遅れが発生することがあります。

あなたIBAction方法は、変更する必要があります。

- (IBAction)play:(id)sender 
{ 

    if (songIsCurrentlyPaused==YES) 
    { 
     [self.background play]; 
    } 
    else 
    { 
     playQueue = dispatch_queue_create("volume_change", NULL); 
     dispatch_async(playQueue, ^{ 
     [self.background setCurrentTime: 0.0]; 
     [self.background play]; 
     }); 

     [self.progressBar setProgress:0.0 animated:YES]; 
     [trackNameLabel setText:@"Currently playing :\n some_song"]; 
     self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES]; 

    } 
    songIsCurrentlyPaused = NO; 
} 

- (IBAction)pause:(id)sender 
{ 
    songIsCurrentlyPaused = YES; 
    [self.background pause]; 
    [trackNameLabel setText:@"Currently playing : some_song (paused)"]; 
    [self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES]; 
} 
+0

問題解決ポーズで!ありがとう、非常に感謝!しかし、ローディングはまだ遅いです:( – nemesis

+0

@ネメシス:私は自分の答えを編集しました。私は現時点では私のマックではないので、コードは不可能です。 –

+0

あなたが再生ボタンを押すと、ビューが読み込まれた後にうまくいきます。完全に削除する方法はありませんか?遅れ?ありがとう!! – nemesis

関連する問題