如何使用AVAudioPlayer在iPhone sdk中暂停和恢复相同的歌曲 [英] How to pause and resume same song in iPhone sdk using AVAudioPlayer

查看:119
本文介绍了如何使用AVAudioPlayer在iPhone sdk中暂停和恢复相同的歌曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想暂停一首歌并继续使用程序化的盟友在iphone中保持这段时间。当我试图暂停这首歌时,我又想开始播放我暂停的歌曲。它是如何编码的。是否有任何直接属性或建议任何代码可以解决我的问题。

I want to pause the song and continue from that point of duration in iphone using programmatic-ally. When I tried to pause the song and again I want to start to play the song where I paused. How it can be coded. Is there any direct properties or suggest any code to that may solve my problem.

-(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.
}

谢谢你,
Madan Mohan。

Thank you, Madan Mohan.

推荐答案

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];
}

这篇关于如何使用AVAudioPlayer在iPhone sdk中暂停和恢复相同的歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆