如何使用 AVAudioPlayer 在 iPhone sdk 中暂停和恢复同一首歌曲

发布于 2024-10-30 13:13:44 字数 622 浏览 0 评论 0原文

我想暂停这首歌,然后在 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.
}

谢谢你, 马丹·莫汉。

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

Thank you,
Madan Mohan.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[浮城] 2024-11-06 13:13:44
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];
}
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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文