audio in a button?

Discussion in 'iOS Programming' started by themacster298, Feb 21, 2011.

  1. themacster298, Feb 21, 2011
    Last edited: Feb 21, 2011

    themacster298 macrumors member

    Joined:
    Feb 20, 2011
    #1
    well I have followed this tutorial showing you how to make a button that takes you to another view. But the only problem is that I don't know how to put audio when it takes you to the other view. Can someone please help or point to a tutorial. Any help is appreciated.
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    there is a viewDidLoad option in ur second view controller that ur loading.
    U can load a sound there.
    If u want to display a sound really when u click the button, just put it inside the void or the IBAction. Depends what ur code looks like.
     
  3. nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #3
    Search YouTube for avaudioplayer and, once you find a code for playing audio, place it in your viewDidLoad method.

    Nick
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    Or in your viewWillAppear
     
  5. themacster298 thread starter macrumors member

    Joined:
    Feb 20, 2011
    #5
    I am having trouble with this. Can someone please add the code for the background music? heres the code:


    This is the SecondViewController.h
    Code:
    #import <UIKit/UIKit.h>
    
    
    @interface SecondViewController : UIViewController {
    
    }
    
    @end
    

    This is the SecondViewController.m
    Code:
    #import "SecondViewController.h"
    
    
    @implementation SecondViewController
    
    /*
     // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
            // Custom initialization
        }
        return self;
    }
    */
    
    /*
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    */
    
    /*
    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    */
    
    - (void)didReceiveMemoryWarning {
    	// Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    	
    	// Release any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
    	// Release any retained subviews of the main view.
    	// e.g. self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
        [super dealloc];
    }
    
    
    @end
    
    P.S. If you want the code for the first view controller just ask
     
  6. themacster298 thread starter macrumors member

    Joined:
    Feb 20, 2011
    #7
    Hi! I already seen the first one before but a button can only have one action so that didn't work.
    As for the second one it succeeded but no sound played.:(
     
  7. nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #8
    Not true but that's another topic.

    This is the same code used in the video but in the viewDidLoad method.
    Code:
    - (void)viewDidLoad {
    	
    	NSString *path = [[NSBundle mainBundle] pathForResource:@"bgmusic" ofType:@"m4a"];
    	if(theAudio)[theAudio release];
    	theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    	[theAudio play];
    }
    You can also put it in viewWillAppear like jnoxx said, or in a button action, or in a void method using performSelector.
     
  8. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #9
    An IBAction method can call two or more other methods or actions.
     
  9. ClutchThese macrumors 65816

    ClutchThese

    Joined:
    Jun 25, 2010
    Location:
    Alexandria, VA
  10. themacster298 thread starter macrumors member

    Joined:
    Feb 20, 2011
    #11
    I dont know if this is asking TO MUCH but I found a much simpler tutorial here. I did it without the back button. I tried what nickculbertson said with the second view and no sound came out. Can someone (I cant believe im going to type this again but) help?
     
  11. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #12
    They provided all the code, means u are doing something wrong..
    Don't think they can help, have u tested on a device or not, and have u actually added the framework? instead of just the code.
    it's couple lines of code, can't mess that up actually.
     

Share This Page