Reputation: 33
I am trying to create an app that will play some audio files. I was hoping to stream the audio from a web server, but so far am placing an mp3 directly into the project as I don't know how to link to http urls.
I have created a stop button and my understanding is that if you press stop and then play again, the mp3 file should resume from where it has stopped. That's not happening for me. Any clues as to what I have done wrong please? I have also tried using pause as well with no change. Many thanks in advance. The code from my .h file is below.
Also, what do I need to do if I want to have multiple audio files?
#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
Here is the edited .m file with the array:
#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
Upvotes: 1
Views: 197
Reputation: 258
@user3934210
Take a array in .h file and write protocol delegate for Player
"AVAudioPlayerDelegate>"
NSMutableArray *songsArray;
in .m files
Add all the .mp3 songs to the array in viewDidLoad
songsArray = [[NSMutableArray alloc]initWithObjects:@“A”,@“B”,@“C”,nil]; //replace your files
then take one Integer value to find the current song
NSUInteger currentTrackNumber;
and set the currentTrackNumber=0 in viewDidLoad,
then copy the below code
- (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];
}
}
}
Upvotes: 0
Reputation: 258
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];
}
Upvotes: 1