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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 04-16-2011, 11:36 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 2
kazyaz is on a distinguished road
Default adding sound to a for loop

Hi Guys I've got a for loop that lets me display a different image on each page using the uiscrollview and uipagecontroller. I'm trying to add a sound to play on each page change. I have 6 pages and I have 6 images 1.png - 6.png. I also have 6 mp3 files named 1.mp3 - 6.mp3

The page control for the images works fine. I'm wanting to add the sound and I've added a bit of code in to make it work. It does not error, however it does not work. The app just won't load? I'm not sure why?

this is the code for the for loop.

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	
	for (int i = 1; i < 7; i++) {
		UIImageView *imagen = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]]];
		imagen.frame = CGRectMake((i-1)*320, 0, 320, 480);
		
		NSString *pathTosoundclip = [[NSBundle mainBundle] pathForResource:@"%i" ofType:@"mp3"];
		soundclip = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathTosoundclip] error:NULL];
		
	[scroller addSubview:imagen];
	
	[imagen release];
	}
	
	
	scroller.delegate = self;
	scroller.contentSize = CGSizeMake(320*6, 480);
	scroller.pagingEnabled = YES;
	
	pageControl.numberOfPages=6;
	pageControl.currentPage=0;
}
Does anyone have any ideas? Obviously I want the mp3 to load on each load of an image. so 1.png would load with 1.mp3 and it would play the audio file from 0seconds. It would also stop the audio file playing and start the new audio file playing when the following page is loaded.

The main thing at this stage is just getting it to play and load.

Hope someone can help?
kazyaz is offline   Reply With Quote
Old 04-16-2011, 04:38 PM   #2 (permalink)
WJK
Registered Member
 
Join Date: Dec 2010
Posts: 3
WJK is on a distinguished road
Default

Quote:
Originally Posted by kazyaz View Post
Hi Guys I've got a for loop that lets me display a different image on each page using the uiscrollview and uipagecontroller. I'm trying to add a sound to play on each page change. I have 6 pages and I have 6 images 1.png - 6.png. I also have 6 mp3 files named 1.mp3 - 6.mp3

The page control for the images works fine. I'm wanting to add the sound and I've added a bit of code in to make it work. It does not error, however it does not work. The app just won't load? I'm not sure why?

this is the code for the for loop.

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	
	for (int i = 1; i < 7; i++) {
		UIImageView *imagen = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]]];
		imagen.frame = CGRectMake((i-1)*320, 0, 320, 480);
		
		NSString *pathTosoundclip = [[NSBundle mainBundle] pathForResource:@"%i" ofType:@"mp3"];
		soundclip = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathTosoundclip] error:NULL];
		
	[scroller addSubview:imagen];
	
	[imagen release];
	}
	
	
	scroller.delegate = self;
	scroller.contentSize = CGSizeMake(320*6, 480);
	scroller.pagingEnabled = YES;
	
	pageControl.numberOfPages=6;
	pageControl.currentPage=0;
}
Does anyone have any ideas? Obviously I want the mp3 to load on each load of an image. so 1.png would load with 1.mp3 and it would play the audio file from 0seconds. It would also stop the audio file playing and start the new audio file playing when the following page is loaded.

The main thing at this stage is just getting it to play and load.

Hope someone can help?
Below is some known working code. I'm using wav files and I believe when I tried to use a mp3 it did not work. However, I found a website that allowed me to convert mp3 to wav.

Code:
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"songFileName", CFSTR("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
WJK is offline   Reply With Quote
Old 04-16-2011, 04:45 PM   #3 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Just as a note Audacity is free and can convert many sound file types.

Are you aware that you are leaking the AVAudioPlayer objects? You are allocating them in a loop and assigning them, without using properties I might add, which you should, to the same 'soundclip' variable. After the loop is done you will end up with only one object, having leaked all the others.
baja_yu is offline   Reply With Quote
Reply

Bookmarks

Tags
avaudio, avaudiocontroller, uipagecontrol, uiscrollview

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: 362
14 members and 348 guests
dansparrow, dre, ilmman, LezB44, lorrettaui53, Nobbsy, Objective Zero, oztemel, pbart, samdanielblr, shagor012, sledzeppelin, thephotographer, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

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