gpt4 book ai didi

ios - 按停止按钮时 AVAudioPlayer 不恢复

转载 作者:行者123 更新时间:2023-11-29 12:34:22 24 4
gpt4 key购买 nike

我正在尝试创建一个可以播放一些音频文件的应用程序。我希望从 Web 服务器流式传输音频,但到目前为止我将 mp3 直接放入项目中,因为我不知道如何链接到 http url。

我创建了一个停止按钮,我的理解是如果您按下停止然后再次播放,mp3 文件应该从它停止的地方继续播放。这不会发生在我身上。关于我做错了什么的任何线索吗?我也尝试过使用 pause 也没有改变。提前谢谢了。我的 .h 文件中的代码如下。

另外,如果我想拥有多个音频文件,我需要做什么?

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
AVAudioPlayer *player;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

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

- (IBAction)play:(id)sender {
NSURL *songURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"hustle" ofType:@"mp3"]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:songURL error:nil];
player.volume = 0.5;
[player play];

}

- (IBAction)pause:(id)sender {
[player stop];
}

- (IBAction)volumeChanged:(id)sender {
player.volume = volumeSlider.value;

}


@end

这是经过编辑的 .m 文件,其中包含数组:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

AVAudioPlayer *player;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//NSURL *songURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"hustle" ofType:@"mp3"]];
//player = [[AVAudioPlayer alloc] initWithContentsOfURL:songURL error:nil];
//player.volume = 0.5;

songsArray = [[NSMutableArray alloc] initWithObjects:@"hustle", @"hustle2", nil];
NSUInteger currentTrackNumber;
currentTrackNumber=0;

}

- (IBAction)startPlaying
{
if (player) {
[player stop];
player = nil;
}
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithString:[songsArray objectAtIndex:currentTrackNumber]] ofType:@"mp3"]] error:NULL];
player.delegate = self;
[player play];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player1 successfully:(BOOL)flag
{
if (flag) {
if (currentTrackNumber < [songsArray count] - 1) {
currentTrackNumber ++;
if (player) {
[player stop];
player = nil;
}
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithString:[songsArray objectAtIndex:currentTrackNumber]] ofType:@"mp3"]] error:NULL];
player.delegate = self;
[player play];
}
}
}

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

- (IBAction)play:(id)sender {

[player play];

}

- (IBAction)pause:(id)sender {
[player stop];
}

- (IBAction)volumeChanged:(id)sender {
player.volume = volumeSlider.value;

}

@end

最佳答案

Write the code like this, when you click on the pause button the audio stopped then click on the play button the audio will resumed where the audio stopped.     



- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSURL *songURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"hustle" ofType:@"mp3"]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:songURL error:nil];
player.volume = 0.5;
}

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

- (IBAction)play:(id)sender {

[player play];

}

- (IBAction)pause:(id)sender {
[player stop];
}

关于ios - 按停止按钮时 AVAudioPlayer 不恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26770307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com