我正在倾向于ios dev,并正在开发一个音频播放器应用程序。所以我需要创建一个可由应用程序的任何视图控制器控制的音频播放器。
到目前为止,我所做的是创建一个表格视图并播放所点击单元格的歌曲;还创建了AVfoundation音频播放器所需的控件,但所有这些都在同一个选项卡视图控制器中。 最后,我创建了一个标签栏控制器,其中包含5个表格视图,我需要播放音频列表。
任何人都可以帮我找到从标签栏中所有viewcontrollers控制播放器的最佳方法吗?
由于
上面是我在播放器所在的View控制器中的代码,这个视图控制器有一个表格视图和播放器,工作正常。
@implementation ViewController {
NSDictionary *Source01Details;
NSArray *Source01Names;
NSDictionary *Source02Details;
NSArray *Source02Names;
NSString *audioPath;
NSURL *soundUrl;
NSString *filename;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Source 01
NSURL *url1 = [[NSBundle mainBundle] URLForResource:@"data_group_01" withExtension:@"plist"];
Source01Details = [NSDictionary dictionaryWithContentsOfURL:url1];
Source01Names = Source01Details.allKeys;
//Source 02
NSURL *url2 = [[NSBundle mainBundle] URLForResource:@"data_group_02" withExtension:@"plist"];
Source02Details = [NSDictionary dictionaryWithContentsOfURL:url2];
Source02Names = Source02Details.allKeys;
//START AUDIO
// Construct URL to sound file
filename = @"audioteste.wav";
audioPath = [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath],filename];
soundUrl = [NSURL fileURLWithPath:audioPath];
//Create audio player object and initialize with URL to sound
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
self.isPlaying = NO;
[self.audioProgressBar setProgress:0];
//END AUDIO
}
- (void)updateProgressBar:(NSTimer *)timer {
if ([_audioPlayer currentTime]==0) {
[self.audioProgressBar setProgress:([_audioPlayer currentTime]/[_audioPlayer duration])];
[self.topPlayButton setBackgroundImage:[UIImage imageNamed:@"PLAY"] forState:UIControlStateNormal];
self.isPlaying = NO;
//[[self rowDisplay] setText:@"Trocar esta linha por update de checkbox!"];
} else {
[self.audioProgressBar setProgress:([_audioPlayer currentTime]/[_audioPlayer duration])];
}
}
- (IBAction)playButton:(id)sender {
//[_audioPlayer play];
if (self.isPlaying) // Music is currently playing
{ [_audioPlayer pause];
[self.topPlayButton setBackgroundImage:[UIImage imageNamed:@"PLAY"] forState:UIControlStateNormal];
self.isPlaying = NO;
}
else // Music is currenty paused/stopped
{ [_audioPlayer play];
[self.topPlayButton setBackgroundImage:[UIImage imageNamed:@"PAUSE"] forState:UIControlStateNormal];
self.isPlaying = YES;}
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.23 target:self selector:@selector(updateProgressBar:) userInfo:nil repeats:YES];
}
- (IBAction)pauseButton:(id)sender {
[_audioPlayer stop];
_audioPlayer.currentTime = 0;
self.isPlaying = NO;
[self.topPlayButton setBackgroundImage:[UIImage imageNamed:@"PLAY"] forState:UIControlStateNormal];
[[self sectionDisplay] setText:@""];
[self.audioProgressBar setProgress:0];
[[self rowDisplay] setText:@"Escolha uma dica para ouvir"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0){
return @"Base para Emagrecimento";
} else {
return @"Supermercado";
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0){
return Source01Details.count;
} else {
return Source02Details.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//create cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//Faz as células terem bordas redondas
CALayer *cellimagelayer = cell.layer;
[cellimagelayer setCornerRadius:9 ];
[cellimagelayer setMasksToBounds:YES];
//Insere uma imagem
//UIImage *playImage = [UIImage imageNamed:@"PLAY"];
//[cell.imageView setImage:playImage];
if (indexPath.section == 0) {
cell.textLabel.text = Source01Names[indexPath.row] ;
cell.detailTextLabel.text = Source01Details[Source01Names[indexPath.row]] ;
} else {
cell.textLabel.text = Source02Names[indexPath.row] ;
cell.detailTextLabel.text = Source02Details[Source02Names[indexPath.row]] ;
}
//fill cell
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
filename = Source01Details[Source01Names[indexPath.row]] ;
[[self sectionDisplay] setText: @"Base para Emagrecimento"];
[[self rowDisplay] setText:Source01Names[indexPath.row]];
} else {
filename = Source02Details[Source02Names[indexPath.row]] ;
[[self sectionDisplay] setText: @"Supermercado"];
[[self rowDisplay] setText:Source02Names[indexPath.row]];
}
audioPath = [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath],filename];
soundUrl = [NSURL fileURLWithPath:audioPath];
// Create audio player object and initialize with URL to sound
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[_audioPlayer play];
[self.topPlayButton setBackgroundImage:[UIImage imageNamed:@"PAUSE"] forState:UIControlStateNormal];
self.isPlaying = YES;
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.23 target:self selector:@selector(updateProgressBar:) userInfo:nil repeats:YES];
}