Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-25-2009, 05:49 PM   #1 (permalink)
College Student
 
ColouredRobot's Avatar
 
Join Date: Dec 2009
Location: Los Angeles, CA
Posts: 75
Exclamation Playing Audio when App Launches

I was wondering what was the best way to play audio at the beginning of the app, without having any build errors or warnings.

The way I approached it works, but gives me a warning.

Quote:
warning: class 'SoundAudioVewController' does not implement the 'AVAudioPlayerDelegate' protocol
So I imported the AV Foundation Framework into my project called "SoundAudio." In my SoundAudioViewController.m I entered the following code:

Code:
@implementation SoundAudioViewController
- (void)awakeFromNib {
		NSString *path = [[NSBundle mainBundle]	pathForResource:@"startApp" ofType:@"wav"];
		AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];
		theAudio.delegate = self;
		[theAudio play];
		
}
Is there a better way? Thanks!
ColouredRobot is offline   Reply With Quote
Old 12-25-2009, 08:05 PM   #2 (permalink)
David Porter Apps
 
bobbypage's Avatar
 
Join Date: Aug 2009
Posts: 71
Default

You get the warning because you didn't declare that you are using the AVFoundation in your .h
__________________
Check out my website/blog: http://www.davidporterapps.com

Follow Me on Twitter:
http://twitter.com/bobbypage
bobbypage is offline   Reply With Quote
Old 12-25-2009, 08:14 PM   #3 (permalink)
simpsonaty Apps
 
simpsonaty's Avatar
 
Join Date: Dec 2009
Location: Australia
Posts: 93
Default

i play audio sound a little differently than you.

Add the AVFoundation framework into your project, then in the header of the main view, import the AVFoundation/AVFoundation.h.

Code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface SoundAudioViewController : UIViewController {
	AVAudioPlayer *audioPlayer;
}

@end
then in the -(void)awakeFromNib method, add the following code. Don't forget to change the "audiofile.mp3" to the name of your .mp3 file.

Code:
-(void)awakeFromNib {

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];

	NSError *error;
	audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
	audioPlayer.numberOfLoops = 0;
	[audioPlayer play];

}
Don't forget to release the audioPlayer in the dealloc

Code:
- (void)dealloc {
	[audioPlayer release];
	[super dealloc];
}
You can use this code in an -(IBAction), or under any other code, such as the didSelectRowAtIndexPath, etc.
__________________
http://simpsonatyapps.com
... yep, that's our site.
simpsonaty is offline   Reply With Quote
Old 12-25-2009, 08:14 PM   #4 (permalink)
David Porter Apps
 
bobbypage's Avatar
 
Join Date: Aug 2009
Posts: 71
Default

To clarify, your header (.h) file should look something like this. Then you won't get any warnings

__________________
Check out my website/blog: http://www.davidporterapps.com

Follow Me on Twitter:
http://twitter.com/bobbypage
bobbypage is offline   Reply With Quote
Old 12-26-2009, 12:34 AM   #5 (permalink)
College Student
 
ColouredRobot's Avatar
 
Join Date: Dec 2009
Location: Los Angeles, CA
Posts: 75
Exclamation

@simpsonaty I followed your way, and it worked perfectly. I have a question concerning a new sound.

Code:
You can use this code in an -(IBAction), or under any other code, such as the didSelectRowAtIndexPath, etc.
How would I code it correctly. I want to make a sound when I slide an object to the right. The code is already fixed for it. But when I run it the sound does not play. Am I inserting the audio code correctly? Thank you!

Code:
- (void) sliderAction: (UISlider *) sender
{
		if (customSlider.value != 1.0)  //if the value is not the max, slide this bad boy back to zero
		{
				[sender setValue: 0 animated: YES];
		}
		else {	
				if (customSlider.value == 1.0)  //if the value is max, slide this bad boy back to zero and execute code
				{
						[sender setValue: 0 animated: NO];
        }
				// the rest of your code here.
				NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/installChime.wav", [[NSBundle mainBundle] resourcePath]]];
				
				NSError *error;
				audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
				audioPlayer.numberOfLoops = 0;
				[audioPlayer play];
				
		}
}

Last edited by ColouredRobot; 12-26-2009 at 02:00 AM.
ColouredRobot is offline   Reply With Quote
Old 12-26-2009, 02:39 AM   #6 (permalink)
simpsonaty Apps
 
simpsonaty's Avatar
 
Join Date: Dec 2009
Location: Australia
Posts: 93
Default

I'm not sure why the code doesn't work, I tried it myself and it worked. Does the app build successfully in the simulator? If it does, try looking at this site: AVAudioPlayer Plugin
__________________
http://simpsonatyapps.com
... yep, that's our site.
simpsonaty is offline   Reply With Quote
Old 12-26-2009, 03:13 AM   #7 (permalink)
College Student
 
ColouredRobot's Avatar
 
Join Date: Dec 2009
Location: Los Angeles, CA
Posts: 75
Default

Quote:
Originally Posted by simpsonaty View Post
I'm not sure why the code doesn't work, I tried it myself and it worked. Does the app build successfully in the simulator? If it does, try looking at this site: AVAudioPlayer Plugin
Well I've tried it on my device, and it does load successfully on both the simulator and the device, but I hear no sound when I slide the button all the way to the right, and it isn't the slider problem, so it must be me not implementing the audio code correctly.

Here's my MainViewController.h file
Code:
//
//  MainViewController.h
//  Unpackr
//
//  Created by Victor Alexander on 12/22/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "FlipsideViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {

		UISlider *customSlider;
		AVAudioPlayer *audioPlayer;
}

- (IBAction)showInfo;

@end
Here's my MainViewController.m file

Code:
//
//  MainViewController.m
//  Unpackr
//
//  Created by Victor Alexander on 12/22/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//
#import <AVFoundation/AVFoundation.h>
#import "MainViewController.h"
#import "MainView.h"



@implementation MainViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

- (void)create_Custom_UISlider
{
		CGRect frame = CGRectMake(0.00, 50.00, 315, 52.0);
		CGRect thumb = CGRectMake(0.00, 50.00, 71.0, 47.0);
		customSlider = [[UISlider alloc] initWithFrame:frame];
		[customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
		// in case the parent view draws with a custom color or gradient, use a transparent color
		customSlider.backgroundColor = [UIColor clearColor];	
		UIImage *stetchLeftTrack = [[UIImage imageNamed:@"sliderTrack.png"]
																stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
		UIImage *stetchRightTrack = [[UIImage imageNamed:@"sliderTrack.png"]
																 stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
		[customSlider setThumbImage: [UIImage imageNamed:@"sliderThumb.png"] forState:UIControlStateNormal];
		[customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
		[customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
		[customSlider thumbRectForBounds: thumb trackRect: frame value: customSlider.value];
		customSlider.minimumValue = 0.0;
		customSlider.maximumValue = 100.0;
		customSlider.continuous = NO;
		customSlider.value = 5.0;
}



// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)viewDidLoad {
		[self create_Custom_UISlider];
		[self.view addSubview: customSlider];
		[super viewDidLoad];
}

- (void) sliderAction: (UISlider *) sender
{
		if (customSlider.value != 1.0)  //if the value is not the max, slide this bad boy back to zero
		{
				[sender setValue: 0 animated: YES];
		}
		else {	
				if (customSlider.value == 1.0)  //if the value is max, slide this bad boy back to zero and execute code
				{
						[sender setValue: 0 animated: NO];
        }
				// the rest of your code here.
				NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/UnpackRip.wav", [[NSBundle mainBundle] resourcePath]]];
				
				NSError *error;
				audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
				audioPlayer.numberOfLoops = 0;
				[audioPlayer play];
				
		}
}
Am I missing something? I tried cleaning the project up, but it still doesn't play the sound when I slide.
ColouredRobot is offline   Reply With Quote
Old 12-26-2009, 03:27 AM   #8 (permalink)
College Student
 
ColouredRobot's Avatar
 
Join Date: Dec 2009
Location: Los Angeles, CA
Posts: 75
Default

Quote:
Originally Posted by ColouredRobot View Post
Well I've tried it on my device, and it does load successfully on both the simulator and the device, but I hear no sound when I slide the button all the way to the right, and it isn't the slider problem, so it must be me not implementing the audio code correctly.

Here's my MainViewController.h file
Code:
//
//  MainViewController.h
//  Unpackr
//
//  Created by Victor Alexander on 12/22/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "FlipsideViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {

		UISlider *customSlider;
		AVAudioPlayer *audioPlayer;
}

- (IBAction)showInfo;

@end
Here's my MainViewController.m file

Code:
//
//  MainViewController.m
//  Unpackr
//
//  Created by Victor Alexander on 12/22/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//
#import <AVFoundation/AVFoundation.h>
#import "MainViewController.h"
#import "MainView.h"



@implementation MainViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

- (void)create_Custom_UISlider
{
		CGRect frame = CGRectMake(0.00, 50.00, 315, 52.0);
		CGRect thumb = CGRectMake(0.00, 50.00, 71.0, 47.0);
		customSlider = [[UISlider alloc] initWithFrame:frame];
		[customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
		// in case the parent view draws with a custom color or gradient, use a transparent color
		customSlider.backgroundColor = [UIColor clearColor];	
		UIImage *stetchLeftTrack = [[UIImage imageNamed:@"sliderTrack.png"]
																stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
		UIImage *stetchRightTrack = [[UIImage imageNamed:@"sliderTrack.png"]
																 stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
		[customSlider setThumbImage: [UIImage imageNamed:@"sliderThumb.png"] forState:UIControlStateNormal];
		[customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
		[customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
		[customSlider thumbRectForBounds: thumb trackRect: frame value: customSlider.value];
		customSlider.minimumValue = 0.0;
		customSlider.maximumValue = 100.0;
		customSlider.continuous = NO;
		customSlider.value = 5.0;
}



// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)viewDidLoad {
		[self create_Custom_UISlider];
		[self.view addSubview: customSlider];
		[super viewDidLoad];
}

- (void) sliderAction: (UISlider *) sender
{
		if (customSlider.value != 1.0)  //if the value is not the max, slide this bad boy back to zero
		{
				[sender setValue: 0 animated: YES];
		}
		else {	
				if (customSlider.value == 1.0)  //if the value is max, slide this bad boy back to zero and execute code
				{
						[sender setValue: 0 animated: NO];
        }
				// the rest of your code here.
				NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/UnpackRip.wav", [[NSBundle mainBundle] resourcePath]]];
				
				NSError *error;
				audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
				audioPlayer.numberOfLoops = 0;
				[audioPlayer play];
				
		}
}
Am I missing something? I tried cleaning the project up, but it still doesn't play the sound when I slide.

Hey I actually attempted to add sound to an info button, and it worked. It turns out the max value in my code was 1.0 but one line accidentally had it at 100.00 so it basically was a slider problem, oopsie! Thanks though!
ColouredRobot is offline   Reply With Quote
Old 12-26-2009, 04:38 AM   #9 (permalink)
simpsonaty Apps
 
simpsonaty's Avatar
 
Join Date: Dec 2009
Location: Australia
Posts: 93
Default

no worries
__________________
http://simpsonatyapps.com
... yep, that's our site.
simpsonaty is offline   Reply With Quote
Reply

Bookmarks

Tags
audio, avfoundation, launch, sound, wav

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 309
19 members and 290 guests
ADY, AragornSG, BrianSlick, Dani77, Dattee, dre, glenn_sayers, HDshot, HemiMG, JasonR, karlam963, prchn4christ, Rudy, spiderguy84, themathminister, tomtom100, viniciusdamone, vogueestylee, vvenkatachallam
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,884
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, karlam963
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:18 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0