AVAudioPlayer is releasing before playing? - xcode

I'm making a soundboard app and I use this code to play mp3 files:
-(IBAction)playSound
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"mysound" ofType:#"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
[theAudio release];
}
When I include [theAudio release]; the sound doesn't play. If I take it out it plays but I don't want to have memory leaks. How can I fix this?
It would be very helpful if you could include whatever code I need to add. I'm new to programming (besides TrueBasic I've used in school) so I'm unfamiliar with Objective-C.

wait til the audio finishes via a delegate to release the audio. I believe there are a number of posts exactly like this on the site if you search, they will have the specific delegate code.

This is only because you use local variable. so theAudio will released immediately.
You should define it as class member and assignment it here. so when this function finished, it still work too.

Related

Why is QTMovieLoadStateDidChangeNotification not firing?

I'm seeing that setting up a notification on QTMovieLoadStateDidChangeNotification has no effect and the target selector never gets called. Am I missing something?
In awakeFromNib:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieLoadStateDidChange:)
name:QTMovieLoadStateDidChangeNotification
object:nil];
On loading movie:
NSNumber *num = [NSNumber numberWithBool:YES];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
url, QTMovieURLAttribute,
nil];
self.mQTMovie1 = [[QTMovie alloc] initWithAttributes:attributes
error:&error];
Also
- (void)movieLoadStateDidChange:(NSNotification *)notification {
NSLog(#"movieLoadStateDidChange got called");
}
I'm not sure this is the answer but I've encountered this before. The cause in my case was a file whose codec was supported only by a third-party plugin (Flip4Mac in my case).
The load state notification isn't called until the movie finishes after it auto-plays (to nowhere). For long media files, it effectively looks like the notification is never called, since we rarely wait 5 minutes or an hour for a load notification when testing our code. To the user, it looks like the app simply isn't loading the file.
Having the user disable the plugin's auto-play-on-load in System Preferences resolves the issue but unfortunately this one support FAQ I can't get around since users of the app in question frequently use Flip4Mac to support files from common digital voice recorders.

Using a URL to share an image with iOS 6 and UIActivityViewController

I am using iOS 6's UIActivityViewController.
I would like to share an image that is not available locally on my iPhone, but that it is available on a remote URL.
NSString *textToShare = _eventoTitle;
UIImage *imageToShare = [UIImage imageNamed:_iconUrl];
NSURL *url = [NSURL URLWithString:_permalink];
NSArray *activityItems = [[NSArray alloc] initWithObjects:textToShare, imageToShare, url, nil];
Unfortunately, this is not working. What am I doing wrong?
I have also tried to use the AFNetworking library:
UIImage *imageToShare = [UIImage alloc];
[*imageToShare setImageWithURL:[NSURL URLWithString:_iconUrl]];
This is not working too.
_iconUrl is something like http://www.mysite.com/picture.png
Thank you, Francesco
Try with:
UIImage *imageToShare = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#", _iconUrl]]]];
Matteo
To use remote images, I implemented a UIActivityItemProvider subclass that downloads the image when requested by the UIActivityViewController. UIActivityViewController calls your UIActivityItemProvider on a background thread so at least it doesn't block the main UI. I use a synchronous call just like Matteo suggests inside my UIActivityItemProvider. However, it's really still not a great solution because it just delays when you have to go do the expensive download. UIActivityViewController doesn't request your data until the user picks one of the activity icons in the view controller. At that time, it calls the UIActivityItemProvider to get the data. So you get a delay at this time.

How do you play an mp3 in a Mac Application in Xcode 4.2

I'm trying to play an mp3 in a Mac Application in Xcode 4.2. How would I do this?
You are not giving us much to work with...
If you just want a simple sound, use NSSound:
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:#"fn" ofType:#"mp3"];
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:resourcePath byReference:YES];
[sound play];
// wait for the sound to play before you release these...
With NSSound, the sound plays concurrently with your code. A common mistake is to release the sound or resource while it is still playing. Don't do that.
You can also use QTMovie in QTKit:
NSError *err = nil;
QTMovie *sound = [[QTMovie movieWithFile:#"fn.mp3" error:&err] retain];
[sound play];
For more demanding sounds, use CoreAudio
You should use QTKit framework and the QTMovie class.

How do you set localstorage so it persist across program runs

I have localstorage for a webview in a program working but it's always wiped out at the beginning of the next program run. I checked the file and it does persist after a program run but at the beginning of the next one, it wipes it out.
Here's my code:
- (void)awakeFromNib {
WebPreferences *prefs = [webView preferences];
[prefs _setLocalStorageDatabasePath:#"~/Library/Application Support/MyApp"];
[prefs setDatabasesEnabled:YES];
[prefs setLocalStorageEnabled:YES];
NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
NSString *htmlPath = [resourcesPath stringByAppendingString:#"/htdocs/index.html"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];
[window setDelegate:self];
}
Any help would be appreciated! Thanks
look at the doc on WebPreferences, you init it use your webview every time, the WebPreferences is pre WebView, which cause you create a new WebPreferences every time you bring up the application. If you want to keep it persistent, you should init it with initWithIdentifier:"indentifer".
WebPreferences Class Reference
I'm sure you know the risks of using private APIs.
That said, put some breakpoints in there and see at what point the cache is blown away. Try moving the database to a temp location just before that line, then putting it back immediately afterward.

How to make MPMoviePlayerController respect the ring/silent switch?

I'm trying, unsuccessfully, to get the MPMoviePlayerController to play movies silently if the ring/silent switch on the iPhone is set to silent. There are no interface methods to help me out nor does the player respect the AudioSessionProperty() trick:
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionSetProperty(
kAudioSessionProperty_AudioCategory,
sizeof (sessionCategory),
&sessionCategory);
Has anyone had any success silencing movie playback?
I spent some time trying to get this to work myself. Eventually I gave up after trying, failing and reading this post on the apple dev forums.
"The MPMoviePlayerController establishes its own audio session and there is nothing you can do to affect this"
MPMoviePlayerController has a property useApplicationAudioSession that will allow the player to respect the device's silence setting.
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
player.useApplicationAudioSession = YES;
[player play];
Add this in your code:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

Resources