Как приостановить и возобновить ту же песню в iPhone SDK с помощью AVAudioPlayer - PullRequest
3 голосов
/ 05 апреля 2011

Я хочу приостановить песню и продолжить с этого момента времени в iphone, используя программно.Когда я пытался приостановить песню и снова хочу начать играть песню, где я сделал паузу.Как это можно закодировать.Есть ли какие-либо прямые свойства или предложить какой-либо код, который может решить мою проблему.

-(void)playMusic
{
    path=[[NSBundle mainBundle]pathForResource:@"01my song here" ofType:@"mp3"];
    AVAudioPlayer *myAudio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path ] error:nil];
    self.audioPlayer=myAudio;
    [myAudio release];
    //audioPlayer.playing ;
    [audioPlayer play];
}

-(void)stopMusic
{
    [audioPlayer stop];
}
-(void)pauseMusic
{
    [audioPlayer pause];// here it is just stopping, how can I start where I sttoped.
}

Спасибо, Мадан Мохан.

1 Ответ

4 голосов
/ 05 апреля 2011
if(!player){


    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    resourcePath = [resourcePath stringByAppendingString:@"/grabbag.m4a"];
    NSLog(@"Path to play: %@", resourcePath);
    NSError* err;

    //Initialize our player pointing to the path to our resource
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:resourcePath] error:&err];

    if( err ){
        //bail!
        NSLog(@"Failed with reason: %@", [err localizedDescription]);
    }
    else{
        //set our delegate, flip the button and let the magic happen
        player.delegate = self;
        [self flipButton:YES];
        [player play];
    }
}
else{
    //If the player exists here, then we're already playing.
    NSLog(@"Resuming playback!");
    [player play];
    [self flipButton:YES];
}
...