Playing different audiofiles using AVaudio based on where I am in a property list. Objective-C











up vote
0
down vote

favorite
1












I have copy pasted my tabbarviewcontroller.m file and my tab4VC.h files below.
my question is this. My app works fine. I click a recipe and it goes into a tab bar view controller that has 4 tabs at the bottom, my audio that I want to play are in tab 4. when i first open by app and go to row 0 and section 0 the audio plays fine when I first click the play button. but when i go to access row 1 section 0 my audio file doesn't play, Also when i try to return to my row 0 section 0 tab 4, the audio doesn't play again. I hope I'm not being confusing with the wording. Any feedback is greatly appreciated. Please let me know if I also need to upload any other files, however I am confident that the problem is arising somewhere in the 2 files listed below, maybe my if statements are the problem?



#import "tab4VC.h"

@interface tab4VC ()

@end

@implementation tab4VC
@synthesize inRow, inLabelText, inSection;
@synthesize outLabel, showRow, showSection;
@synthesize player, path;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// prep Audio Player local variable
NSError *myError;

//pathforresource is where your mp3 file name would go


if (!player){
NSLog(@"viewDidLoad:error %@", [myError localizedDescription]);
NSLog(@"viewDidLoad:failure reason %@", [myError localizedFailureReason]);
NSLog(@"viewDidLoad:recovery options %@", [myError localizedRecoveryOptions]);
NSLog(@"viewDidLoad:recovery suggestions %@",[myError localizedRecoverySuggestion]);
}

[player prepareToPlay];
player.meteringEnabled = YES;
avgVolume.progress = 0.0f;
peakVolume.progress = 0.0f;

timeScale.minimumValue = 0;
timeScale.maximumValue = player.duration;

//BEGINING OF THE BREAKFAST SECTION
if(inRow == 0 && inSection == 0)
{

NSError *myError;
path = [[NSBundle mainBundle] pathForResource:@"voice1" ofType:@"m4a"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&myError];
outLabel.text = self.inLabelText;
showSection.text = [NSString stringWithFormat:@"Simmer uncovered for 3 to 5 minutes until thickened, stirring occasionally. Remove from heat and let cool slightly.Divide equally between two bowls. Drizzle each serving with 1/2 teaspoon honey.%lu", inSection];
}

if(inRow == 1 && inSection == 0)
{

NSError *myError;
path = [[NSBundle mainBundle] pathForResource:@"voice2" ofType:@"m4a"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&myError];
outLabel.text = self.inLabelText;
outLabel.text = self.inLabelText;
showSection.text = [NSString stringWithFormat:@" Spray or lightly grease a large skillet and heat over medium heat.Crack 3 eggs into a glass measuring cup and beat with a fork or whisk until scrambled. Pour eggs into heated skillet, swirling the pan to coat the bottom. Cover and let cook until eggs are firm, about 2 minutes. %lu", inSection];
}


//BEGINING OF THE DESERT SECTION
if(inRow == 0 && inSection == 1)
{
NSError *myError;
path = [[NSBundle mainBundle] pathForResource:@"voice3" ofType:@"m4a"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&myError];
outLabel.text = self.inLabelText;
showRow.text = [NSString stringWithFormat:@"1 cup of Greek Yogurt, 1 Scoop of your favorite protein powder %lu", inRow];
showSection.text = [NSString stringWithFormat:@"Add 1 cup of Greek Yogurt and 1 Scoop of your favorite protein powder into a bowl and mix, you can add whatever favorite toppings you like and enjoy! %lu", inSection];
}

if(inRow == 1 && inSection == 1)
{
outLabel.text = self.inLabelText;
showRow.text = [NSString stringWithFormat:@"1 Scoop of your favorite protein powder, Frozen Bannana, Milk"];
showSection.text = [NSString stringWithFormat:@"Add all of the ingrediants above into a Blender, Blend until no chunks are visible, pour into a glass ane enjoy! %lu", inSection];
}

//BEGINING OF THE DINNER SECTION
if(inRow == 0 && inSection == 2)
{
outLabel.text = self.inLabelText;
showRow.text = [NSString stringWithFormat:@"1 cup of Rice, 1 Can of Black Beans %lu", inRow];
showSection.text = [NSString stringWithFormat:@"Add 1 cup of Rice and 1 Can of Black Beans into a cooking pot, place the pot over a stove. Add 2 cups of water. Let the water come to a boil, once that happens, place a lid on top and turn off the heat. wait 15, 20 minutes. Add your favorite seasonings and enjoy! %lu", inSection];
}

if(inRow == 1 && inSection == 2)
{
outLabel.text = self.inLabelText;
showRow.text = [NSString stringWithFormat:@"romaine, tomatoes, Swiss cheese, almonds if desired, Parmesan cheese"];
showSection.text = [NSString stringWithFormat:@"In a large serving bowl, toss the romaine, tomatoes, Swiss cheese, almonds if desired, Parmesan cheese and bacon. Shake dressing; pour over salad and toss. Add croutons and serve immediately. %lu", inSection];
}

//BEGINING OF LUNCH SECTION
if(inRow == 0 && inSection == 3)
{
outLabel.text = self.inLabelText;
showRow.text = [NSString stringWithFormat:@"1 egg, 1 can of Tuna, 3 tablespoons sweet pickle relish, celery, 1 pinch ground black pepper. %lu", inRow];
showSection.text = [NSString stringWithFormat:@"Place egg in a saucepan and cover with cold water. Bring water to a boil and immediately remove from heat. Cover and let egg stand in hot water for 10 to 12 minutes. Remove from hot water; cool for 5 minutes. Peel and chop into bite-sized pieces.In a medium bowl, mix together tuna and mayonnaise. Mix in egg, celery, relish, and black pepper. %lu", inSection];
}

if(inRow == 1 && inSection == 3)
{
outLabel.text = self.inLabelText;
showRow.text = [NSString stringWithFormat:@"Bread of any kind, Mozeralla Cheese"];
showSection.text = [NSString stringWithFormat:@"Slice the break in half, put mozeralla cheese strips between the bread, put on a stove top with medium heat, cook until bread crust is brown and the cheese is melted, Servce and enjoy! %lu", inSection];
}
}

-(IBAction)play:(id)sender{
static BOOL isFirstPush = YES; //we will set up timer only on the first push of the button

if (isFirstPush) {
//setup timer to update controls
timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateUI:) userInfo:nil repeats:YES];
isFirstPush = NO;
}
if ([playPauseBtn.currentTitle isEqual:@"Play"]){
volumeSlider.value = player.volume;
volumeSlider.enabled = YES;

//switch label text
[playPauseBtn setTitle:@"Pause" forState:UIControlStateNormal];
[playPauseBtn setEnabled:YES];
[player play];
} else if ([playPauseBtn.currentTitle isEqual:@"Pause"]){
[playPauseBtn setTitle:@"Play" forState:UIControlStateNormal];
[playPauseBtn setEnabled:YES];
[player pause];
}
}

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

//updating values on screen
-(void)updateUI:(id)sender {
[player updateMeters];


float avg = -1 * [player averagePowerForChannel:0];
float peak = -1 * [player peakPowerForChannel:0];

avgVolume.progress = avg/20;
peakVolume.progress = peak/20;

NSLog(@"Playing %d", player.playing);
NSLog(@"avg %f, peak %f, avg.progress %f, peak.progress %f", avg, peak, avgVolume.progress, peakVolume.progress);

//invalidate timer when we reach to the end of play
if (player.playing == 0){
[timer invalidate];
[playPauseBtn setTitle:@"Play" forState:UIControlStateNormal];
timeScale.value = 0;
[playPauseBtn setEnabled:YES];
}
}

-(IBAction)changePlayerTime:(id)sender{

player.currentTime = timeScale.value;
}

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

/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/


Below is my tabBarViewController.m file



//
// tabBarViewController.m
// TablehomeworkFinal
//
// Created by Studio 300 Patron on 10/30/18.
// Copyright © 2018 Studio 300 Patron. All rights reserved.
//

#import "tabBarViewController.h"
#import "tab1VC.h"
#import "tab2VC.h"
#import "tab3VC.h"
#import "tab4VC.h"

@interface tabBarViewController ()

@end

@implementation tabBarViewController
@synthesize inSection, inRow, inLabelText, imageView;

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

tab1VC *tab1;
tab1 = [[self viewControllers]objectAtIndex:0];
tab1.inLabelText = inLabelText;
tab1.inRow = inRow;
tab1.inSection = inSection;

// //object at index is what tab we are on, it is in array language so tab 2 is index 1.
tab2VC *tab2;
tab2 = [[self viewControllers]objectAtIndex:1];
tab2.inLabelText = inLabelText;
tab2.inRow = inRow;
tab2.inSection = inSection;

// //object at index is what tab we are on, it is in array language so tab 2 is index 1.
tab3VC *tab3;
tab3 = [[self viewControllers]objectAtIndex:2];
tab3.inLabelText = inLabelText;
tab3.inRow = inRow;
tab3.inSection = inSection;
tab3.imageView = imageView;
// [tab3.imageView setImage:[UIImage imageNamed:@"oatmeal.jpg"]];
// [tab3.imageView setHidden:NO];
//
tab4VC *tab4;
tab4 = [[self viewControllers]objectAtIndex:3];
tab4.inLabelText = inLabelText;
tab4.inRow = inRow;
tab4.inSection = inSection;


}

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

/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end









share|improve this question




























    up vote
    0
    down vote

    favorite
    1












    I have copy pasted my tabbarviewcontroller.m file and my tab4VC.h files below.
    my question is this. My app works fine. I click a recipe and it goes into a tab bar view controller that has 4 tabs at the bottom, my audio that I want to play are in tab 4. when i first open by app and go to row 0 and section 0 the audio plays fine when I first click the play button. but when i go to access row 1 section 0 my audio file doesn't play, Also when i try to return to my row 0 section 0 tab 4, the audio doesn't play again. I hope I'm not being confusing with the wording. Any feedback is greatly appreciated. Please let me know if I also need to upload any other files, however I am confident that the problem is arising somewhere in the 2 files listed below, maybe my if statements are the problem?



    #import "tab4VC.h"

    @interface tab4VC ()

    @end

    @implementation tab4VC
    @synthesize inRow, inLabelText, inSection;
    @synthesize outLabel, showRow, showSection;
    @synthesize player, path;

    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // prep Audio Player local variable
    NSError *myError;

    //pathforresource is where your mp3 file name would go


    if (!player){
    NSLog(@"viewDidLoad:error %@", [myError localizedDescription]);
    NSLog(@"viewDidLoad:failure reason %@", [myError localizedFailureReason]);
    NSLog(@"viewDidLoad:recovery options %@", [myError localizedRecoveryOptions]);
    NSLog(@"viewDidLoad:recovery suggestions %@",[myError localizedRecoverySuggestion]);
    }

    [player prepareToPlay];
    player.meteringEnabled = YES;
    avgVolume.progress = 0.0f;
    peakVolume.progress = 0.0f;

    timeScale.minimumValue = 0;
    timeScale.maximumValue = player.duration;

    //BEGINING OF THE BREAKFAST SECTION
    if(inRow == 0 && inSection == 0)
    {

    NSError *myError;
    path = [[NSBundle mainBundle] pathForResource:@"voice1" ofType:@"m4a"];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&myError];
    outLabel.text = self.inLabelText;
    showSection.text = [NSString stringWithFormat:@"Simmer uncovered for 3 to 5 minutes until thickened, stirring occasionally. Remove from heat and let cool slightly.Divide equally between two bowls. Drizzle each serving with 1/2 teaspoon honey.%lu", inSection];
    }

    if(inRow == 1 && inSection == 0)
    {

    NSError *myError;
    path = [[NSBundle mainBundle] pathForResource:@"voice2" ofType:@"m4a"];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&myError];
    outLabel.text = self.inLabelText;
    outLabel.text = self.inLabelText;
    showSection.text = [NSString stringWithFormat:@" Spray or lightly grease a large skillet and heat over medium heat.Crack 3 eggs into a glass measuring cup and beat with a fork or whisk until scrambled. Pour eggs into heated skillet, swirling the pan to coat the bottom. Cover and let cook until eggs are firm, about 2 minutes. %lu", inSection];
    }


    //BEGINING OF THE DESERT SECTION
    if(inRow == 0 && inSection == 1)
    {
    NSError *myError;
    path = [[NSBundle mainBundle] pathForResource:@"voice3" ofType:@"m4a"];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&myError];
    outLabel.text = self.inLabelText;
    showRow.text = [NSString stringWithFormat:@"1 cup of Greek Yogurt, 1 Scoop of your favorite protein powder %lu", inRow];
    showSection.text = [NSString stringWithFormat:@"Add 1 cup of Greek Yogurt and 1 Scoop of your favorite protein powder into a bowl and mix, you can add whatever favorite toppings you like and enjoy! %lu", inSection];
    }

    if(inRow == 1 && inSection == 1)
    {
    outLabel.text = self.inLabelText;
    showRow.text = [NSString stringWithFormat:@"1 Scoop of your favorite protein powder, Frozen Bannana, Milk"];
    showSection.text = [NSString stringWithFormat:@"Add all of the ingrediants above into a Blender, Blend until no chunks are visible, pour into a glass ane enjoy! %lu", inSection];
    }

    //BEGINING OF THE DINNER SECTION
    if(inRow == 0 && inSection == 2)
    {
    outLabel.text = self.inLabelText;
    showRow.text = [NSString stringWithFormat:@"1 cup of Rice, 1 Can of Black Beans %lu", inRow];
    showSection.text = [NSString stringWithFormat:@"Add 1 cup of Rice and 1 Can of Black Beans into a cooking pot, place the pot over a stove. Add 2 cups of water. Let the water come to a boil, once that happens, place a lid on top and turn off the heat. wait 15, 20 minutes. Add your favorite seasonings and enjoy! %lu", inSection];
    }

    if(inRow == 1 && inSection == 2)
    {
    outLabel.text = self.inLabelText;
    showRow.text = [NSString stringWithFormat:@"romaine, tomatoes, Swiss cheese, almonds if desired, Parmesan cheese"];
    showSection.text = [NSString stringWithFormat:@"In a large serving bowl, toss the romaine, tomatoes, Swiss cheese, almonds if desired, Parmesan cheese and bacon. Shake dressing; pour over salad and toss. Add croutons and serve immediately. %lu", inSection];
    }

    //BEGINING OF LUNCH SECTION
    if(inRow == 0 && inSection == 3)
    {
    outLabel.text = self.inLabelText;
    showRow.text = [NSString stringWithFormat:@"1 egg, 1 can of Tuna, 3 tablespoons sweet pickle relish, celery, 1 pinch ground black pepper. %lu", inRow];
    showSection.text = [NSString stringWithFormat:@"Place egg in a saucepan and cover with cold water. Bring water to a boil and immediately remove from heat. Cover and let egg stand in hot water for 10 to 12 minutes. Remove from hot water; cool for 5 minutes. Peel and chop into bite-sized pieces.In a medium bowl, mix together tuna and mayonnaise. Mix in egg, celery, relish, and black pepper. %lu", inSection];
    }

    if(inRow == 1 && inSection == 3)
    {
    outLabel.text = self.inLabelText;
    showRow.text = [NSString stringWithFormat:@"Bread of any kind, Mozeralla Cheese"];
    showSection.text = [NSString stringWithFormat:@"Slice the break in half, put mozeralla cheese strips between the bread, put on a stove top with medium heat, cook until bread crust is brown and the cheese is melted, Servce and enjoy! %lu", inSection];
    }
    }

    -(IBAction)play:(id)sender{
    static BOOL isFirstPush = YES; //we will set up timer only on the first push of the button

    if (isFirstPush) {
    //setup timer to update controls
    timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateUI:) userInfo:nil repeats:YES];
    isFirstPush = NO;
    }
    if ([playPauseBtn.currentTitle isEqual:@"Play"]){
    volumeSlider.value = player.volume;
    volumeSlider.enabled = YES;

    //switch label text
    [playPauseBtn setTitle:@"Pause" forState:UIControlStateNormal];
    [playPauseBtn setEnabled:YES];
    [player play];
    } else if ([playPauseBtn.currentTitle isEqual:@"Pause"]){
    [playPauseBtn setTitle:@"Play" forState:UIControlStateNormal];
    [playPauseBtn setEnabled:YES];
    [player pause];
    }
    }

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

    //updating values on screen
    -(void)updateUI:(id)sender {
    [player updateMeters];


    float avg = -1 * [player averagePowerForChannel:0];
    float peak = -1 * [player peakPowerForChannel:0];

    avgVolume.progress = avg/20;
    peakVolume.progress = peak/20;

    NSLog(@"Playing %d", player.playing);
    NSLog(@"avg %f, peak %f, avg.progress %f, peak.progress %f", avg, peak, avgVolume.progress, peakVolume.progress);

    //invalidate timer when we reach to the end of play
    if (player.playing == 0){
    [timer invalidate];
    [playPauseBtn setTitle:@"Play" forState:UIControlStateNormal];
    timeScale.value = 0;
    [playPauseBtn setEnabled:YES];
    }
    }

    -(IBAction)changePlayerTime:(id)sender{

    player.currentTime = timeScale.value;
    }

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

    /*
    #pragma mark - Navigation
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    }
    */


    Below is my tabBarViewController.m file



    //
    // tabBarViewController.m
    // TablehomeworkFinal
    //
    // Created by Studio 300 Patron on 10/30/18.
    // Copyright © 2018 Studio 300 Patron. All rights reserved.
    //

    #import "tabBarViewController.h"
    #import "tab1VC.h"
    #import "tab2VC.h"
    #import "tab3VC.h"
    #import "tab4VC.h"

    @interface tabBarViewController ()

    @end

    @implementation tabBarViewController
    @synthesize inSection, inRow, inLabelText, imageView;

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

    tab1VC *tab1;
    tab1 = [[self viewControllers]objectAtIndex:0];
    tab1.inLabelText = inLabelText;
    tab1.inRow = inRow;
    tab1.inSection = inSection;

    // //object at index is what tab we are on, it is in array language so tab 2 is index 1.
    tab2VC *tab2;
    tab2 = [[self viewControllers]objectAtIndex:1];
    tab2.inLabelText = inLabelText;
    tab2.inRow = inRow;
    tab2.inSection = inSection;

    // //object at index is what tab we are on, it is in array language so tab 2 is index 1.
    tab3VC *tab3;
    tab3 = [[self viewControllers]objectAtIndex:2];
    tab3.inLabelText = inLabelText;
    tab3.inRow = inRow;
    tab3.inSection = inSection;
    tab3.imageView = imageView;
    // [tab3.imageView setImage:[UIImage imageNamed:@"oatmeal.jpg"]];
    // [tab3.imageView setHidden:NO];
    //
    tab4VC *tab4;
    tab4 = [[self viewControllers]objectAtIndex:3];
    tab4.inLabelText = inLabelText;
    tab4.inRow = inRow;
    tab4.inSection = inSection;


    }

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

    /*
    #pragma mark - Navigation
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    }
    */

    @end









    share|improve this question


























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I have copy pasted my tabbarviewcontroller.m file and my tab4VC.h files below.
      my question is this. My app works fine. I click a recipe and it goes into a tab bar view controller that has 4 tabs at the bottom, my audio that I want to play are in tab 4. when i first open by app and go to row 0 and section 0 the audio plays fine when I first click the play button. but when i go to access row 1 section 0 my audio file doesn't play, Also when i try to return to my row 0 section 0 tab 4, the audio doesn't play again. I hope I'm not being confusing with the wording. Any feedback is greatly appreciated. Please let me know if I also need to upload any other files, however I am confident that the problem is arising somewhere in the 2 files listed below, maybe my if statements are the problem?



      #import "tab4VC.h"

      @interface tab4VC ()

      @end

      @implementation tab4VC
      @synthesize inRow, inLabelText, inSection;
      @synthesize outLabel, showRow, showSection;
      @synthesize player, path;

      - (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view.
      // prep Audio Player local variable
      NSError *myError;

      //pathforresource is where your mp3 file name would go


      if (!player){
      NSLog(@"viewDidLoad:error %@", [myError localizedDescription]);
      NSLog(@"viewDidLoad:failure reason %@", [myError localizedFailureReason]);
      NSLog(@"viewDidLoad:recovery options %@", [myError localizedRecoveryOptions]);
      NSLog(@"viewDidLoad:recovery suggestions %@",[myError localizedRecoverySuggestion]);
      }

      [player prepareToPlay];
      player.meteringEnabled = YES;
      avgVolume.progress = 0.0f;
      peakVolume.progress = 0.0f;

      timeScale.minimumValue = 0;
      timeScale.maximumValue = player.duration;

      //BEGINING OF THE BREAKFAST SECTION
      if(inRow == 0 && inSection == 0)
      {

      NSError *myError;
      path = [[NSBundle mainBundle] pathForResource:@"voice1" ofType:@"m4a"];
      player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&myError];
      outLabel.text = self.inLabelText;
      showSection.text = [NSString stringWithFormat:@"Simmer uncovered for 3 to 5 minutes until thickened, stirring occasionally. Remove from heat and let cool slightly.Divide equally between two bowls. Drizzle each serving with 1/2 teaspoon honey.%lu", inSection];
      }

      if(inRow == 1 && inSection == 0)
      {

      NSError *myError;
      path = [[NSBundle mainBundle] pathForResource:@"voice2" ofType:@"m4a"];
      player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&myError];
      outLabel.text = self.inLabelText;
      outLabel.text = self.inLabelText;
      showSection.text = [NSString stringWithFormat:@" Spray or lightly grease a large skillet and heat over medium heat.Crack 3 eggs into a glass measuring cup and beat with a fork or whisk until scrambled. Pour eggs into heated skillet, swirling the pan to coat the bottom. Cover and let cook until eggs are firm, about 2 minutes. %lu", inSection];
      }


      //BEGINING OF THE DESERT SECTION
      if(inRow == 0 && inSection == 1)
      {
      NSError *myError;
      path = [[NSBundle mainBundle] pathForResource:@"voice3" ofType:@"m4a"];
      player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&myError];
      outLabel.text = self.inLabelText;
      showRow.text = [NSString stringWithFormat:@"1 cup of Greek Yogurt, 1 Scoop of your favorite protein powder %lu", inRow];
      showSection.text = [NSString stringWithFormat:@"Add 1 cup of Greek Yogurt and 1 Scoop of your favorite protein powder into a bowl and mix, you can add whatever favorite toppings you like and enjoy! %lu", inSection];
      }

      if(inRow == 1 && inSection == 1)
      {
      outLabel.text = self.inLabelText;
      showRow.text = [NSString stringWithFormat:@"1 Scoop of your favorite protein powder, Frozen Bannana, Milk"];
      showSection.text = [NSString stringWithFormat:@"Add all of the ingrediants above into a Blender, Blend until no chunks are visible, pour into a glass ane enjoy! %lu", inSection];
      }

      //BEGINING OF THE DINNER SECTION
      if(inRow == 0 && inSection == 2)
      {
      outLabel.text = self.inLabelText;
      showRow.text = [NSString stringWithFormat:@"1 cup of Rice, 1 Can of Black Beans %lu", inRow];
      showSection.text = [NSString stringWithFormat:@"Add 1 cup of Rice and 1 Can of Black Beans into a cooking pot, place the pot over a stove. Add 2 cups of water. Let the water come to a boil, once that happens, place a lid on top and turn off the heat. wait 15, 20 minutes. Add your favorite seasonings and enjoy! %lu", inSection];
      }

      if(inRow == 1 && inSection == 2)
      {
      outLabel.text = self.inLabelText;
      showRow.text = [NSString stringWithFormat:@"romaine, tomatoes, Swiss cheese, almonds if desired, Parmesan cheese"];
      showSection.text = [NSString stringWithFormat:@"In a large serving bowl, toss the romaine, tomatoes, Swiss cheese, almonds if desired, Parmesan cheese and bacon. Shake dressing; pour over salad and toss. Add croutons and serve immediately. %lu", inSection];
      }

      //BEGINING OF LUNCH SECTION
      if(inRow == 0 && inSection == 3)
      {
      outLabel.text = self.inLabelText;
      showRow.text = [NSString stringWithFormat:@"1 egg, 1 can of Tuna, 3 tablespoons sweet pickle relish, celery, 1 pinch ground black pepper. %lu", inRow];
      showSection.text = [NSString stringWithFormat:@"Place egg in a saucepan and cover with cold water. Bring water to a boil and immediately remove from heat. Cover and let egg stand in hot water for 10 to 12 minutes. Remove from hot water; cool for 5 minutes. Peel and chop into bite-sized pieces.In a medium bowl, mix together tuna and mayonnaise. Mix in egg, celery, relish, and black pepper. %lu", inSection];
      }

      if(inRow == 1 && inSection == 3)
      {
      outLabel.text = self.inLabelText;
      showRow.text = [NSString stringWithFormat:@"Bread of any kind, Mozeralla Cheese"];
      showSection.text = [NSString stringWithFormat:@"Slice the break in half, put mozeralla cheese strips between the bread, put on a stove top with medium heat, cook until bread crust is brown and the cheese is melted, Servce and enjoy! %lu", inSection];
      }
      }

      -(IBAction)play:(id)sender{
      static BOOL isFirstPush = YES; //we will set up timer only on the first push of the button

      if (isFirstPush) {
      //setup timer to update controls
      timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateUI:) userInfo:nil repeats:YES];
      isFirstPush = NO;
      }
      if ([playPauseBtn.currentTitle isEqual:@"Play"]){
      volumeSlider.value = player.volume;
      volumeSlider.enabled = YES;

      //switch label text
      [playPauseBtn setTitle:@"Pause" forState:UIControlStateNormal];
      [playPauseBtn setEnabled:YES];
      [player play];
      } else if ([playPauseBtn.currentTitle isEqual:@"Pause"]){
      [playPauseBtn setTitle:@"Play" forState:UIControlStateNormal];
      [playPauseBtn setEnabled:YES];
      [player pause];
      }
      }

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

      //updating values on screen
      -(void)updateUI:(id)sender {
      [player updateMeters];


      float avg = -1 * [player averagePowerForChannel:0];
      float peak = -1 * [player peakPowerForChannel:0];

      avgVolume.progress = avg/20;
      peakVolume.progress = peak/20;

      NSLog(@"Playing %d", player.playing);
      NSLog(@"avg %f, peak %f, avg.progress %f, peak.progress %f", avg, peak, avgVolume.progress, peakVolume.progress);

      //invalidate timer when we reach to the end of play
      if (player.playing == 0){
      [timer invalidate];
      [playPauseBtn setTitle:@"Play" forState:UIControlStateNormal];
      timeScale.value = 0;
      [playPauseBtn setEnabled:YES];
      }
      }

      -(IBAction)changePlayerTime:(id)sender{

      player.currentTime = timeScale.value;
      }

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

      /*
      #pragma mark - Navigation
      // In a storyboard-based application, you will often want to do a little preparation before navigation
      - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
      // Get the new view controller using [segue destinationViewController].
      // Pass the selected object to the new view controller.
      }
      */


      Below is my tabBarViewController.m file



      //
      // tabBarViewController.m
      // TablehomeworkFinal
      //
      // Created by Studio 300 Patron on 10/30/18.
      // Copyright © 2018 Studio 300 Patron. All rights reserved.
      //

      #import "tabBarViewController.h"
      #import "tab1VC.h"
      #import "tab2VC.h"
      #import "tab3VC.h"
      #import "tab4VC.h"

      @interface tabBarViewController ()

      @end

      @implementation tabBarViewController
      @synthesize inSection, inRow, inLabelText, imageView;

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

      tab1VC *tab1;
      tab1 = [[self viewControllers]objectAtIndex:0];
      tab1.inLabelText = inLabelText;
      tab1.inRow = inRow;
      tab1.inSection = inSection;

      // //object at index is what tab we are on, it is in array language so tab 2 is index 1.
      tab2VC *tab2;
      tab2 = [[self viewControllers]objectAtIndex:1];
      tab2.inLabelText = inLabelText;
      tab2.inRow = inRow;
      tab2.inSection = inSection;

      // //object at index is what tab we are on, it is in array language so tab 2 is index 1.
      tab3VC *tab3;
      tab3 = [[self viewControllers]objectAtIndex:2];
      tab3.inLabelText = inLabelText;
      tab3.inRow = inRow;
      tab3.inSection = inSection;
      tab3.imageView = imageView;
      // [tab3.imageView setImage:[UIImage imageNamed:@"oatmeal.jpg"]];
      // [tab3.imageView setHidden:NO];
      //
      tab4VC *tab4;
      tab4 = [[self viewControllers]objectAtIndex:3];
      tab4.inLabelText = inLabelText;
      tab4.inRow = inRow;
      tab4.inSection = inSection;


      }

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

      /*
      #pragma mark - Navigation
      // In a storyboard-based application, you will often want to do a little preparation before navigation
      - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
      // Get the new view controller using [segue destinationViewController].
      // Pass the selected object to the new view controller.
      }
      */

      @end









      share|improve this question















      I have copy pasted my tabbarviewcontroller.m file and my tab4VC.h files below.
      my question is this. My app works fine. I click a recipe and it goes into a tab bar view controller that has 4 tabs at the bottom, my audio that I want to play are in tab 4. when i first open by app and go to row 0 and section 0 the audio plays fine when I first click the play button. but when i go to access row 1 section 0 my audio file doesn't play, Also when i try to return to my row 0 section 0 tab 4, the audio doesn't play again. I hope I'm not being confusing with the wording. Any feedback is greatly appreciated. Please let me know if I also need to upload any other files, however I am confident that the problem is arising somewhere in the 2 files listed below, maybe my if statements are the problem?



      #import "tab4VC.h"

      @interface tab4VC ()

      @end

      @implementation tab4VC
      @synthesize inRow, inLabelText, inSection;
      @synthesize outLabel, showRow, showSection;
      @synthesize player, path;

      - (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view.
      // prep Audio Player local variable
      NSError *myError;

      //pathforresource is where your mp3 file name would go


      if (!player){
      NSLog(@"viewDidLoad:error %@", [myError localizedDescription]);
      NSLog(@"viewDidLoad:failure reason %@", [myError localizedFailureReason]);
      NSLog(@"viewDidLoad:recovery options %@", [myError localizedRecoveryOptions]);
      NSLog(@"viewDidLoad:recovery suggestions %@",[myError localizedRecoverySuggestion]);
      }

      [player prepareToPlay];
      player.meteringEnabled = YES;
      avgVolume.progress = 0.0f;
      peakVolume.progress = 0.0f;

      timeScale.minimumValue = 0;
      timeScale.maximumValue = player.duration;

      //BEGINING OF THE BREAKFAST SECTION
      if(inRow == 0 && inSection == 0)
      {

      NSError *myError;
      path = [[NSBundle mainBundle] pathForResource:@"voice1" ofType:@"m4a"];
      player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&myError];
      outLabel.text = self.inLabelText;
      showSection.text = [NSString stringWithFormat:@"Simmer uncovered for 3 to 5 minutes until thickened, stirring occasionally. Remove from heat and let cool slightly.Divide equally between two bowls. Drizzle each serving with 1/2 teaspoon honey.%lu", inSection];
      }

      if(inRow == 1 && inSection == 0)
      {

      NSError *myError;
      path = [[NSBundle mainBundle] pathForResource:@"voice2" ofType:@"m4a"];
      player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&myError];
      outLabel.text = self.inLabelText;
      outLabel.text = self.inLabelText;
      showSection.text = [NSString stringWithFormat:@" Spray or lightly grease a large skillet and heat over medium heat.Crack 3 eggs into a glass measuring cup and beat with a fork or whisk until scrambled. Pour eggs into heated skillet, swirling the pan to coat the bottom. Cover and let cook until eggs are firm, about 2 minutes. %lu", inSection];
      }


      //BEGINING OF THE DESERT SECTION
      if(inRow == 0 && inSection == 1)
      {
      NSError *myError;
      path = [[NSBundle mainBundle] pathForResource:@"voice3" ofType:@"m4a"];
      player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&myError];
      outLabel.text = self.inLabelText;
      showRow.text = [NSString stringWithFormat:@"1 cup of Greek Yogurt, 1 Scoop of your favorite protein powder %lu", inRow];
      showSection.text = [NSString stringWithFormat:@"Add 1 cup of Greek Yogurt and 1 Scoop of your favorite protein powder into a bowl and mix, you can add whatever favorite toppings you like and enjoy! %lu", inSection];
      }

      if(inRow == 1 && inSection == 1)
      {
      outLabel.text = self.inLabelText;
      showRow.text = [NSString stringWithFormat:@"1 Scoop of your favorite protein powder, Frozen Bannana, Milk"];
      showSection.text = [NSString stringWithFormat:@"Add all of the ingrediants above into a Blender, Blend until no chunks are visible, pour into a glass ane enjoy! %lu", inSection];
      }

      //BEGINING OF THE DINNER SECTION
      if(inRow == 0 && inSection == 2)
      {
      outLabel.text = self.inLabelText;
      showRow.text = [NSString stringWithFormat:@"1 cup of Rice, 1 Can of Black Beans %lu", inRow];
      showSection.text = [NSString stringWithFormat:@"Add 1 cup of Rice and 1 Can of Black Beans into a cooking pot, place the pot over a stove. Add 2 cups of water. Let the water come to a boil, once that happens, place a lid on top and turn off the heat. wait 15, 20 minutes. Add your favorite seasonings and enjoy! %lu", inSection];
      }

      if(inRow == 1 && inSection == 2)
      {
      outLabel.text = self.inLabelText;
      showRow.text = [NSString stringWithFormat:@"romaine, tomatoes, Swiss cheese, almonds if desired, Parmesan cheese"];
      showSection.text = [NSString stringWithFormat:@"In a large serving bowl, toss the romaine, tomatoes, Swiss cheese, almonds if desired, Parmesan cheese and bacon. Shake dressing; pour over salad and toss. Add croutons and serve immediately. %lu", inSection];
      }

      //BEGINING OF LUNCH SECTION
      if(inRow == 0 && inSection == 3)
      {
      outLabel.text = self.inLabelText;
      showRow.text = [NSString stringWithFormat:@"1 egg, 1 can of Tuna, 3 tablespoons sweet pickle relish, celery, 1 pinch ground black pepper. %lu", inRow];
      showSection.text = [NSString stringWithFormat:@"Place egg in a saucepan and cover with cold water. Bring water to a boil and immediately remove from heat. Cover and let egg stand in hot water for 10 to 12 minutes. Remove from hot water; cool for 5 minutes. Peel and chop into bite-sized pieces.In a medium bowl, mix together tuna and mayonnaise. Mix in egg, celery, relish, and black pepper. %lu", inSection];
      }

      if(inRow == 1 && inSection == 3)
      {
      outLabel.text = self.inLabelText;
      showRow.text = [NSString stringWithFormat:@"Bread of any kind, Mozeralla Cheese"];
      showSection.text = [NSString stringWithFormat:@"Slice the break in half, put mozeralla cheese strips between the bread, put on a stove top with medium heat, cook until bread crust is brown and the cheese is melted, Servce and enjoy! %lu", inSection];
      }
      }

      -(IBAction)play:(id)sender{
      static BOOL isFirstPush = YES; //we will set up timer only on the first push of the button

      if (isFirstPush) {
      //setup timer to update controls
      timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateUI:) userInfo:nil repeats:YES];
      isFirstPush = NO;
      }
      if ([playPauseBtn.currentTitle isEqual:@"Play"]){
      volumeSlider.value = player.volume;
      volumeSlider.enabled = YES;

      //switch label text
      [playPauseBtn setTitle:@"Pause" forState:UIControlStateNormal];
      [playPauseBtn setEnabled:YES];
      [player play];
      } else if ([playPauseBtn.currentTitle isEqual:@"Pause"]){
      [playPauseBtn setTitle:@"Play" forState:UIControlStateNormal];
      [playPauseBtn setEnabled:YES];
      [player pause];
      }
      }

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

      //updating values on screen
      -(void)updateUI:(id)sender {
      [player updateMeters];


      float avg = -1 * [player averagePowerForChannel:0];
      float peak = -1 * [player peakPowerForChannel:0];

      avgVolume.progress = avg/20;
      peakVolume.progress = peak/20;

      NSLog(@"Playing %d", player.playing);
      NSLog(@"avg %f, peak %f, avg.progress %f, peak.progress %f", avg, peak, avgVolume.progress, peakVolume.progress);

      //invalidate timer when we reach to the end of play
      if (player.playing == 0){
      [timer invalidate];
      [playPauseBtn setTitle:@"Play" forState:UIControlStateNormal];
      timeScale.value = 0;
      [playPauseBtn setEnabled:YES];
      }
      }

      -(IBAction)changePlayerTime:(id)sender{

      player.currentTime = timeScale.value;
      }

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

      /*
      #pragma mark - Navigation
      // In a storyboard-based application, you will often want to do a little preparation before navigation
      - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
      // Get the new view controller using [segue destinationViewController].
      // Pass the selected object to the new view controller.
      }
      */


      Below is my tabBarViewController.m file



      //
      // tabBarViewController.m
      // TablehomeworkFinal
      //
      // Created by Studio 300 Patron on 10/30/18.
      // Copyright © 2018 Studio 300 Patron. All rights reserved.
      //

      #import "tabBarViewController.h"
      #import "tab1VC.h"
      #import "tab2VC.h"
      #import "tab3VC.h"
      #import "tab4VC.h"

      @interface tabBarViewController ()

      @end

      @implementation tabBarViewController
      @synthesize inSection, inRow, inLabelText, imageView;

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

      tab1VC *tab1;
      tab1 = [[self viewControllers]objectAtIndex:0];
      tab1.inLabelText = inLabelText;
      tab1.inRow = inRow;
      tab1.inSection = inSection;

      // //object at index is what tab we are on, it is in array language so tab 2 is index 1.
      tab2VC *tab2;
      tab2 = [[self viewControllers]objectAtIndex:1];
      tab2.inLabelText = inLabelText;
      tab2.inRow = inRow;
      tab2.inSection = inSection;

      // //object at index is what tab we are on, it is in array language so tab 2 is index 1.
      tab3VC *tab3;
      tab3 = [[self viewControllers]objectAtIndex:2];
      tab3.inLabelText = inLabelText;
      tab3.inRow = inRow;
      tab3.inSection = inSection;
      tab3.imageView = imageView;
      // [tab3.imageView setImage:[UIImage imageNamed:@"oatmeal.jpg"]];
      // [tab3.imageView setHidden:NO];
      //
      tab4VC *tab4;
      tab4 = [[self viewControllers]objectAtIndex:3];
      tab4.inLabelText = inLabelText;
      tab4.inRow = inRow;
      tab4.inSection = inSection;


      }

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

      /*
      #pragma mark - Navigation
      // In a storyboard-based application, you will often want to do a little preparation before navigation
      - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
      // Get the new view controller using [segue destinationViewController].
      // Pass the selected object to the new view controller.
      }
      */

      @end






      ios objective-c






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 19 hours ago









      Willeke

      7,3482923




      7,3482923










      asked 23 hours ago









      Batman

      32




      32





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203206%2fplaying-different-audiofiles-using-avaudio-based-on-where-i-am-in-a-property-lis%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203206%2fplaying-different-audiofiles-using-avaudio-based-on-where-i-am-in-a-property-lis%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff