2015-03-25 102 views
2

我是创建AVAudio播放器,它的播放完美,点击播放按钮歌曲正在播放和它的播放暂停按钮,点击暂停按钮歌曲已暂停,并显示播放按钮,但点击播放按钮正在播放从开始onwards.i要播放简历歌曲。如何控制这个问题。这是我的代码,请检查一次。如何从开始播放AVAudio播放器?

-(void)playAudio 
{ 
    // [progreetimer invalidate]; 
    // if([audioPlayer isPlaying]==YES)`enter code here` 
    if(self.isPlaying) 
    { 
     [self.play setBackgroundImage:[UIImage imageNamed:@"audioplayer_play.png"] 
     forState:UIControlStateNormal]; 
     NSLog(@"Pausing Music"); 
     if (self.progreetimer) { 
      [self.progreetimer invalidate]; 
      progreetimer =nil; 
     } 
     [audioPlayer pause]; 
     self.isPlaying = NO; 
    } 

    else { 

     // Init audio with playback capability 
    [self.play setBackgroundImage:[UIImage imageNamed:@"audioplayer_pause.png"] forState:UIControlStateNormal]; 

    // NSString *urlstr = @"http://jesusredeems.in/media/Media Advt/mp3_player/Songs/viduthalaiyin geethangal_vol1/93.Ellame Koodum.mp3"; 

    NSString *urlstr [email protected]"http://www.abstractpath.com/files/audiosamples/sample.mp3"; 
    urlstr = [urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSURL *url = [NSURL URLWithString:urlstr]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 
    audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil]; 
    audioPlayer.volume = 1.9f; 
    // [audioPlayer prepareToPlay]; 
    SongProgressBar.maximumValue = [audioPlayer duration]; 
    SongProgressBar.value = 0.0; 
     NSLog(@"Playing music"); 

    //start a timer to update the time label display 
    self.progreetimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self 
    selector:@selector(updateTime:) userInfo:nil repeats:YES]; 

    self.progreetimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self 
    selector:@selector(updateSlider) userInfo:nil repeats:YES]; 
    [progreetimer fire]; 
    audioPlayer.delegate=self; 
    [audioPlayer play]; 
     self.isPlaying = YES; 

    } 
} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    self.SongProgressBar.minimumValue = 30; 

    self.SongProgressBar.maximumValue = 30; 

    [self.SongProgressBar setThumbImage:[UIImage imageNamed:@"thumbslider.png"] 

         forState:UIControlStateNormal]; 

    // [self.SongProgressBar setThumbImage:[UIImage imageNamed:@"slider_icon.png"] 

         // forState:UIControlStateHighlighted]; 

    [self.SongProgressBar setMinimumTrackImage:[UIImage imageNamed:@"slider_max.png"] 

           forState:UIControlStateNormal]; 

    [self.SongProgressBar setMaximumTrackImage:[UIImage imageNamed:@"slider_icon.png"] 

           forState:UIControlStateNormal]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
    [audioPlayer pause]; // Or pause 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    [audioPlayer play]; 
} 



- (IBAction)songprogressbar:(id)sender { 
    // Fast skip the music when user scroll the UISlider 
    [audioPlayer stop]; 
    [audioPlayer setCurrentTime:SongProgressBar.value]; 
    [audioPlayer prepareToPlay]; 
    [audioPlayer play]; 
} 

- (IBAction)backword:(id)sender { 
    if ([self.audioPlayer isPlaying]) { 
     NSTimeInterval desiredTime = audioPlayer.currentTime -15.0f; 
     if (desiredTime < 0) { 
      audioPlayer.currentTime =0.0f; 
     } else { 
      audioPlayer.currentTime = desiredTime; 
     } 
    } 
} 

- (IBAction)play:(id)sender { 

    [self playAudio]; 
} 
- (void)updateSlider { 
    // Update the slider about the music time 
    SongProgressBar.value = audioPlayer.currentTime; 
} 

- (void)setCurrentAudioTime:(float)value { 
    [self.audioPlayer setCurrentTime:value]; 
} 
//Stops the timer when the music is finished 
- (void)audioPlayerDidFinishPlaying : (AVAudioPlayer *)player successfully : (BOOL)flag { 
    // Music completed 
    if (flag) { 
     [progreetimer invalidate]; 

     NSLog(@"Finished playing the song"); 
    } 
} 

- (IBAction)forword:(id)sender { 

    if ([audioPlayer isPlaying]) { 
     NSTimeInterval desiredTime = audioPlayer.currentTime +15.0f; 
     if (desiredTime < audioPlayer.duration) { 
      audioPlayer.currentTime = desiredTime; 
     } 
    } 
} 

- (IBAction)volume:(id)sender { 
    audioPlayer.volume=volume.value; 
} 

-(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)updateTime:(NSTimer *)timer { 

     self.starttimer.text = [NSString stringWithFormat:@"%@", 
     [self timeFormat: ceilf(audioPlayer.currentTime)]]; 


     self.endtimer.text = [NSString stringWithFormat:@"-%@", [self timeFormat: (audioPlayer.duration - ceilf(audioPlayer.currentTime))]]; 
    } 
+0

其他部分当你的AVAudioPlayer'的'实例被暂停,''playAudio叫你创建AVAudioPlayer'的'丢弃前一个被暂停的新实例。您应该检测AVAudioPlayer的播放状态,并在暂停时再次开始播放。 – rckoenes 2015-03-25 10:24:16

+0

如何改变那个? – Phanindra 2015-03-25 10:26:03

回答

-2

使用下面的代码

AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil]; 
[player prepareToPlay]; 
player.currentTime = 0; 
[player play]; 
+1

问题不在于如何创建“AVAudioPlayer”并播放音乐,而是在稍后暂停播放音乐。 – rckoenes 2015-03-25 10:42:33

+0

需要将玩家目前的时间添加到零 – 2015-03-25 11:55:51

1

你应该试试这个。参见方法

-(void)playAudio 
{ 
    // [progreetimer invalidate]; 
    // if([audioPlayer isPlaying]==YES)`enter code here` 
    if(self.isPlaying) 
    { 
     //Code to pause audioPlayer 
     [audioPlayer pause]; 
     self.isPlaying = NO; 
    } 

    else 
    { 
      if(audioPlayer.url != nil && audioPlayer.duration != 0) 
      { 
       [audioPlayer play]; 
      } 
      else 
      { 
       //Code to add new audioPlayer 
      } 
    } 
} 
+0

其不工作的兄弟。如果声明没有调用。你可以改变代码吗? – Phanindra 2015-03-25 10:46:19

+0

您是否将audioPlayer定义为全局常量?如果不是,将其定义为全局!导致此代码正在为我工​​作 – 2015-03-25 10:47:46