Can't use two "audioPlayerDidFinishPlaying" - xcode

Can you please tell me how to fix this? I want to release two different AVAudioPlayer when they finish playing, but separately.
Here's my code:
.h File
#interface ViewController : UIViewController <AVAudioPlayerDelegate>
{
NSString *path;
}
- (IBAction)Short:(id)sender;
- (IBAction)BeatLong:(id)sender;
.m File
AVAudioPlayer *media;
AVAudioPlayer *media2;
- (IBAction)Short:(id)sender
{
path = [[NSBundle mainBundle] pathForResource:#"Short" ofType:#"wav"];
media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[media setDelegate:self];
[media play];
}
- (IBAction)Beat:(id)sender
{
path = [[NSBundle mainBundle] pathForResource:#"Beat" ofType:#"mp3"];
media2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[media2 setDelegate:self];
[media2 play];
}
(Open the image in a new tab to see it better ^^,)

Nay, that's illegal on the grounds of the language.
You have to distinguish the different players according to the AVAudioPlayer * pointer
submitted with the message.
If you just want to release it, just write
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)aPlayer successfully:(BOOL)flag
{
[aPlayer release];
}
and you're done and illegal too, since you don't own aPlayer.
But a better solution would be to detect which audio player you own and release it.
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)aPlayer successfully:(BOOL)flag
{
if ( aPlayer == self.media )
[self.media release];
else if ( aPlayer == self.media2 )
[self.media2 release];
// other players cannot be released, since we don't know anything about their owner.
}

Related

Why do NSFilePresenter protocol methods never get called?

I am trying to monitor file changes in local and iCloud directories and have implemented the NSFilePresenter protocol methods but the only method that gets called is presentedItemAtURL.
Am I correct in assuming that I should be able to monitor a local or an iCloud directory and get notified any time any process adds, modifies or deletes a file in the directory.
Here is the basic code for the OS X App:
- (void)awakeFromNib {
_presentedItemURL = myDocumentsDirectoryURL;
_presentedItemOperationQueue = [[NSOperationQueue alloc] init];
[_presentedItemOperationQueue setMaxConcurrentOperationCount: 1];
_fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self];
}
- (NSURL*) presentedItemURL {
FLOG(#" called %#", _presentedItemURL);
return _presentedItemURL;
}
- (NSOperationQueue*) presentedItemOperationQueue {
FLOG(#" called");
return _presentedItemOperationQueue;
}
- (void)presentedItemDidChange {
FLOG(#" called");
dispatch_async(dispatch_get_main_queue(), ^{
[self reloadData];
});
}
-(void)accommodatePresentedItemDeletionWithCompletionHandler:(void (^)(NSError *errorOrNil))completionHandler
{ FLOG(#" called");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self reloadData];
}];
completionHandler(nil);
}
-(void)presentedSubitemDidChangeAtURL:(NSURL *)url {
FLOG(#" called");
dispatch_async(dispatch_get_main_queue(), ^{
[self reloadData];
});
}
-(void)presentedSubitemDidAppearAtURL:(NSURL *)url {
FLOG(#" called");
dispatch_async(dispatch_get_main_queue(), ^{
[self reloadData];
});
}
Long time ago, I know, but perhaps this will still help. NSFilePresenter will only notify you about changes made by another process that makes changes to a directory or file USING AN NSFileCoordinator. If another process (eg: iTunes file sharing) makes changes without an NSFileCoordinator, you won't be notified.
This is in no way my final implementation and I will edit/update as I improve. But since there is nil examples on how to do this, i figured i'd share something that works!!! That's right, it works. I am able to read the file in my app, and at the same time make a change in textedit and the changes propagate to my app. Hope this helps bud.
PBDocument.h
#interface PBDocument : NSObject <NSFilePresenter>
#property (nonatomic, strong) NSTextView *textView;
#pragma mark - NSFilePresenter properties
#property (readonly) NSURL *presentedItemURL;
#property (readonly) NSOperationQueue *presentedItemOperationQueue;
- (instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError *__autoreleasing *)outError textView:(NSTextView*)textView;
#end
PBDocument.m
#interface PBDocument ()
#property (readwrite) NSURL *presentedItemURL;
#property (readwrite) NSOperationQueue *presentedItemOperationQueue;
#property (readwrite) NSFileCoordinator *fileCoordinator;
#end
#implementation PBDocument
- (instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError *__autoreleasing *)outError textView:(NSTextView*)textView {
self = [super init];
if (self) {
_textView = textView;
_presentedItemURL = url;
_presentedItemOperationQueue = [NSOperationQueue mainQueue];
[NSFileCoordinator addFilePresenter:self];
_fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self];
[self readWithCoordination];
}
return self;
}
- (void)readWithCoordination {
NSError *error = nil;
[self.fileCoordinator coordinateReadingItemAtURL:_presentedItemURL options:NSFileCoordinatorReadingWithoutChanges error:&error byAccessor:^(NSURL *newURL) {
NSLog(#"Coordinating Read");
NSError *error = nil;
NSFileWrapper *wrapper = [[NSFileWrapper alloc] initWithURL:newURL options:0 error:&error];
if (!error) {
[self readFromFileWrapper:wrapper ofType:[self.presentedItemURL pathExtension] error:&error];
}
if (error) #throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:#"%#", error] userInfo:nil];
}];
if (error) #throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:#"%#", error] userInfo:nil];
}
- (void)presentedItemDidChange {
[self readWithCoordination];
}
#end
If it's any help to anyone this is the approach (FSEvents) I ended up using recently for a file sync solution and it seems to work for any file system. I have not done any research recently on NSFileCoordinator to see whether this is better worse or what the use cases are as a comparison.
I also did not test every use case so your mileage may vary.
https://github.com/eonil/FSEvents

no visible #interface for 'UIImagePickerController

So my code says
no visible #interface for UIImagePicker Controller
I am using a Tab View Controller
My code for my FirstViewController.m is
#import "FirstViewController.h"
#interface FirstViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
UIImagePickerController *bailey;
UIImagePickerController *baileys;
UIImage *image;
IBOutlet UIImageView *imageView;
}
- (IBAction)takePhoto;
- (IBAction)chooseExisting;
#end
#implementation FirstViewController
- (IBAction)takePhoto {
bailey = [[UIImagePickerController alloc]init];
bailey.delegate = self;
[bailey setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:bailey animated:YES completion:NULL];
}
- (IBAction)chooseExisting {
baileys = [[UIImagePickerController alloc] init];
baileys.delegate = self;
[baileys setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:baileys animated:YES completion:NULL];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)bailey {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (IBAction)ShareFB {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
slComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[slComposeViewController addImage:[UIImage imageNamed:#"image"]];
[slComposeViewController addURL:[NSURL URLWithString:#"http://www.agfg.com.au"]];
[self presentViewController:slComposeViewController animated:YES completion:NULL];
//I borrowed these lines of code from http://www.bit.ly/174eqjy
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingString:#"upload.png"];
NSString *upLoad = [documentsDirectory stringByAppendingString:#"upload.png"];
NSData *myData = [[NSData alloc]initWithContentsOfFile:appFile];
//Here is the line that fails
[bailey addAttachmentData:myData mimeType:#"image/png" fileName:#"upload"];
//Up to here are the borrowed code lines.
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Facebook Account Detected" message:#"There are no Facebook Accounts Detected. You can configure them in Settings." delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alert show];
}}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And my code for my FirstViewController.h is
#import <UIKit/UIKit.h>
#import <Social/Social.h>
#interface FirstViewController : UIViewController {SLComposeViewController *slComposeViewController;
UIImage *upLoad;}
- (IBAction)ShareFB;
#end
My app is designed to Take a photo and display the photo (and it does that) and then save that image to the local documents folder on the app (which I got from another post) and then specify that image to upload to Facebook (which I have)
All that works except the saving to a local documents.
If you need to store file in documents directory, use like this,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"Images"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];
}
NSString *imageFilePath = [NSString stringWithFormat:#"%#/%#",dataPath,#"upload.png"];
UIImage *image = [UIImage imageNamed:#"image"]; //Here place your image
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
[imageData writeToFile:imageFilePath atomically:YES];
If you need to attach image in facebook, use this statement,
[slComposeViewController
addAttachmentData:UIImagePNGRepresentation(image, 1) mimeType:#"image/png" fileName:#"upload.png"];
With the help of Karthika, and his facebook upload code, I was typing it in when I realised that I did not need to implement local documents saving.
Facebook let me add the image in without saving it.
Thanks though Karthika, I could not have done it without you.

xcode cannot get sound file to play with AVAudioPlayer

I am new to iOS and trying to add sound to my app. I have followed several tutorials about adding a simple sound file to my project, but ALL with the same result. I get no errors, but no sound plays. Any ideas on what I am doing wrong? Any help would be appreciated!
- (IBAction)playSound:(id)sender {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSLog(soundFilePath);
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
player.numberOfLoops = 1;
[player play];
}
**UPDATE***
So I never had any luck with the above code which is very discouraging, but I did end up getting it to work using the AudioToolbox Framework instead, using this:
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle]
pathForResource:#"test" ofType:#"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)
[NSURL fileURLWithPath:soundFile], & soundID);
AudioServicesPlaySystemSound(soundID);
Can anyone explain to me the difference, why this one works and the other does not?? I am curious more than anything. Would love some clarification.
Finally figured it out. I needed to create a property for AVAudioPlayer in my header file in order to get it to work. Here is what I did:
// .h file
#interface ThirdViewController : UIViewController {
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
- (void) playSound //method in .m file
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"test"
ofType:#"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer play];
}
Simplify your code by doing the follow. Also drop your test.mp3 to the root of your project directory in xcode. Your mp3 placement and location is very important. Also ensure volume is up, and vibrate is off of your device, and it's unmuted.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"test"
ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
[audioPlayer play];
Make sure you install quartz.h, or a specific framework that supports audio players. Just double check.
Had the same problem a while back and the framework must be installed properly.

Limit allowed operations to only NSDragOperationCopy

I have a custom view that is a drag source. I want to limit the allowed drag operations to only a copy, and hence I return NSDragOperationCopy in my draggingSession:sourceOperationMaskForDraggingContext: method.
For some reason this has no effect.
There doesn't seem to be anything wrong with my code as returning NSDragOperationNone works as expected and blocks drags.
Below is the code from my NSView that is the dragging source. Dragging from the view to the "Trash" you'll see a NSDragOperationMove or NSDragOperationDelete even though I only want to allow the NSDragOperationCopy operation.
I've also uploaded a small demo project to demonstrate what I'm talking about: https://dl.dropbox.com/u/368222/test.zip.
- (void)mouseDown:(NSEvent *)event {
NSString *path = [[NSBundle mainBundle] resourcePath];
NSString *imagePath = [path stringByAppendingString:#"/image.png"];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:imagePath];
NSArray *file = [NSArray arrayWithObject:imagePath];
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:nil];
[pboard setPropertyList:file forType:NSFilenamesPboardType];
[self dragImage:image at:NSZeroPoint offset:NSMakeSize(0, 0) event:event pasteboard:pboard source:self slideBack:NO];
}
- (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context {
return NSDragOperationCopy;
}
- (void)draggingSession:(NSDraggingSession *)session endedAtPoint:(NSPoint)screenPoint operation:(NSDragOperation)operation {
NSLog(#"Dragging session ended with operation: %li", operation);
}
Anyone?
Thx!

Can I add sounds to Xcode's Storyboard mode?

I'm new to creating apps for iOS and I'm using storyboard mode. I wanted to know how can I add sounds. Also, does it require coding?
You can use this for playback of simple sounds. It uses the AVFoundation framework, which will require you to add AVFoundation to your project, and use #import <AVFoundation/AVFoundation.h>
- (IBAction) myPlayAction {
AVAudioPlayer *data;
NSString *name = [[NSString alloc] initWithFormat:#"SoundName"];
NSString *source = [[NSBundle mainBundle] pathForResource:name ofType:#"mp3"];
if (data) {
[data stop];
data = nil;
}
data=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: source] error:NULL];
data.delegate = self;
[data prepareToPlay];
[data play];
}

Resources