I was searching through the threads and found some topics about implementing a void function to switch the audio on off. This is entirely new to me, since i am using this code right here:
This plays my audio. What I am asking though is if there is a way of making the audio file go on/off by switching one of those light blue switches in the Interface builder somehow. I would still really like to use my IBAction though, because that is what I understand right now at the moment.
musicwind95: you said that I should have an IBAction of the 'toggle Audio'. I created one in my h file, and posted your code into my m file. I linked the action to my blue switch, and clicked on touch up inside. How do I link my button to the 'audioControlSwitch'? Is that another ViewController? Or do I just rename my switch to audioControl Switch?
Domele: didn't musicwind95 include the AVAudioPlayer though? Everything should still work, because we still get the audio through the AVAudioPlayer. Thats what I believe at least.......
musicwind95: you said that I should have an IBAction of the 'toggle Audio'. I created one in my h file, and posted your code into my m file. I linked the action to my blue switch, and clicked on touch up inside. How do I link my button to the 'audioControlSwitch'? Is that another ViewController? Or do I just rename my switch to audioControl Switch?
audioControlSwitch was just a dummy name. You could just rename your switch to that, make sure it's an IBOutlet, and connect to that. One thing though—you should connect's the switch's Value Changed to toggleAudio, rather than Touch Up Inside.
Quote:
Originally Posted by penpaper
Domele: didn't musicwind95 include the AVAudioPlayer though? Everything should still work, because we still get the audio through the AVAudioPlayer. Thats what I believe at least.......
Thank you,
No, Domele's right. The AVAudioPlayer is limited to that first if() block. At least declare it right inside the method, but outside of the if() block.
in my header file, and connected it in my .xib file. The only problem is the code. The part above the if statement has the
[NSURL fileURLWithPathath]
in it. I get an error that the path is undeclared. I don't really understand this, because the code I use (which was stated earlier way above) uses the exact same code, just a little bit more down (like the third line). Do you know what the reason for this error could be?
theAudio.delegate = self;
[theAudio play];
}
else {
// Stop playing
if (audioControlSwitch.off) {
theAudio.delegate = self;
[theAudio stop];
}
}
}
I can't get the music to stop playing though once I started it. I tried with the if audioControl Switch off, but xcode doesn't recognize 'off'. So what should I do now??
…
I can't get the music to stop playing though once I started it. I tried with the if audioControl Switch off, but xcode doesn't recognize 'off'. So what should I do now??
The property's name doesn't change to off. Try something like this:
Code:
if (audioControlSwitch.on) {
// Switch is 'on': start playing
}
else {
// Switch is 'off': stop playing
}
__________________
If I have helped you, please consider donating. I use PayPal. It would mean a lot to me!
Your code will never stop the audio because you are losing your reference to your original audio player. That is what I meant by it's not going to work. Every time that method runs you are creating a new audio player object, one that is independent of the one you created before. That is why you need to create an instance variable and assign your audio player to that so you can keep a reference to it and give it a stop message when needed.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
theAudio.delegate = self;
[theAudio play];
}
else {
// Stop playing
if (audioControlSwitch.off) {
theAudio.delegate = self;
[theAudio stop];
}
}
}
I can't get the music to stop playing though once I started it. I tried with the if audioControl Switch off, but xcode doesn't recognize 'off'. So what should I do now??
Thanks in advance,
As Domele says, you should not create the audio player in the IBAction.
Make your audio player theAudio a retained property.
Create your audio player in your viewDidLoad method, and save it to the property. Then set the property to nil in your viewDidUnload method.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Thanks for replying. I'll just go with DuncanC for now, since I only started a few months back, and I have no idea how to code a bunch, just a little.
DuncanC: I understand that I have to do all that. Since I just started out a few months ago, its pretty complicated though. I have a sub view controller. This is the code for the h file:
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
self.backaudio = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
And everything works fine! The view just doesn't load from my table view when I build and run. Any idea what could be the problem? (I did everything you said I should, but maybe I did it wrong or something....)
Thanks in advance,
by the way: how do you add code to a reply of a thread? Its probably really hard to read when I just copy and paste.
Thanks for replying. I'll just go with DuncanC for now, since I only started a few months back, and I have no idea how to code a bunch, just a little.
DuncanC: I understand that I have to do all that. Since I just started out a few months ago, its pretty complicated though. I have a sub view controller. This is the code for the h file:
And everything works fine! The view just doesn't load from my table view when I build and run. Any idea what could be the problem? (I did everything you said I should, but maybe I did it wrong or something....)
Thanks in advance,
by the way: how do you add code to a reply of a thread? Its probably really hard to read when I just copy and paste.
Your code to load and unload your view controller looks good, as does the toggleAudio method.
You said your view doesn't load from your table view, but you never posted the code from the previous view controller that's supposed to trigger the loading of this one. We can't help debug code we can't see!
Tell us what user action in the previous view controller is supposed to cause this view controller to be displayed, and then show the code that should be displaying it.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Well, I mean everything worked before I implemented the toggle audio action and the part in the viewdidload. But anyways, this is the code I use to get from my table view to my other views.
Code:
This is the code from my RootViewController.m file.
@synthesize v1;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
if(self.v1 == nil) {
About *View1 =
[[About alloc] initWithNibName:@"About" bundle:[NSBundle mainBundle]];
self.v1 = View1;
[View1 release];
}
[self.navigationController pushViewController:self.v1 animated:YES];
}
and this is the code in my header file:
Code:
#import <UIKit/UIKit.h>
#import "About.h"
@interface RootViewController : UITableViewController {
About *v1;
}
@property(nonatomic,retain) About *v1;
@end
I got the code for this from a tutorial a while back. Everything works fine with the code, its just that once I added the new code it didn't load all of a sudden.
Obviously this isn't all of the code in my rootviewcontroller, but this is the important code, because the rest is the green code thats always there at the beginning of an application.