@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.
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
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.
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!