Я пытаюсь создать приложение, которое будет воспроизводить некоторые аудиофайлы. Я надеялся передать аудио с веб-сервера, но до сих пор я помещаю mp3 прямо в проект, так как не знаю, как установить ссылку на http urls.AVAudioPlayer не возобновляется при нажатии кнопки остановки
Я создал кнопку остановки, и я понимаю, что если вы нажмете стоп, а затем снова запустите, файл mp3 должен возобновиться с того места, где он остановился. Это не для меня. Какие-нибудь подсказки относительно того, что я сделал неправильно, пожалуйста? Я также попытался использовать паузу, а также без изменений. Спасибо заранее. Код из моего файла .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
вы должны сделать паузу вместо очистных, '[игрок пауза],', то вы должны играть снова, чтобы возобновить его. – zaheer