iPhone SDK: Play a single WAV from a button - ios

I am currently trying out this code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"dream" ofType:#"m4a"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
However, the SDK says that the ViewController does not implement the AVAudioPlayer Delegate.
Any body any ideas on how to play a WAV (or M4a) using the 2.2 SDK?

You need to add <AVAudioPlayerDelegate> to the end of your controller's class declaration, along these lines:
#interface MyViewController : UIViewController <AVAudioPlayerDelegate>
This'll probably generate some more warnings about delegate methods you need to implement - follow those and you'll be good to go.

Interesting. Try NSURL -URLWithString: - that's what I use in combination with the bundle method you're using. I don't see anything that could be causing a problem other than that. You're testing it on the device, right?

The answer I posted here should definitely help you. Just use audioservices for small wave sounds, unless you need concurrency, it's faster.

iPhoneToot.com has its own itootSound class and a VIDEO to show you how to play a wav file in 2 lines of code. Check them out at iPhoneToot.com. it will save you hours if not days of time!!

Related

play a sound from an arbitrary position

I'm looking for a way to play an audio file from just any point in the file.
Currently, whenever I play the file, it just starts from the beginning.
Since it is a loop, I could choose to let it loop, and open/close the volume at will, but I think it is kind of a lame solution, not in the least because of battery concerns.
I really did some research on this, but it is either a freak requirement, or I don't know how to phrase my queries.
Thanks in advance for any tip.
That should be a nice easy problem to solve. You don't post any code, so we can't see what you're trying at the moment. What I'd do is this:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"SoundFile" ofType:#"wav"];
AVAudioPlayer * soundData =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
[soundData stop];
soundData.currentTime = 22; //This will start playback 22 seconds into the file
[soundData play];
It goes without saying that you need to check the duration of your sound file so that you don't select a time beyond its end!
currentTime is a float (so you can specify millisecond values). You can read more here: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/#//apple_ref/occ/instp/AVAudioPlayer/currentTime

GPUImage and AVAssetWriter writes files to where?

I am trying to use Brad Larson's GPUImage library to record a video file. So far, the issue I am running into is the following piece of code:
NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Movie.m4v"];
unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie
NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 640.0)];
This is picked up directly from his example from his GitHub page
https://github.com/BradLarson/GPUImage
Now, I looked at the source code and I see he does the usual AVFoundation stuff - creates AVAssetWriter instance and adds inputs to it. MY question here is that where is #"Documents/Movie.m4v" located? How can I play this video back? Or how do I even access it? I know it is located in an arbitrary location where the app bundle is copied during run-time. My goal is to be able to write the video into the Gallery or atleast an album in Gallery that is created by my app. Does anyone know where the file is being written to?
Maybe I'm misunderstanding, but the movie is at the path pathToMovie (or URL movieURL.
You can play it back with something like this:
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
// later
AVPlayerViewController *avc = [[AVPlayerViewController alloc] init];
avc.player = [[AVPlayer alloc] initWithURL:movieURL];
[myViewController presentViewController:avc animated:YES completion:nil];
If you want to download the file to your computer, you can select your connect device in Xcode > Devices, then select your app and download the package. Your movie will be in the Documents directory.
To save it to the gallery, do
// TODO: use completion arguments for a better user experience
UISaveVideoAtPathToSavedPhotosAlbum(pathToMovie,nil,nil,nil);

Audio in XCode 5 does not play

I am relatively new to coding, and I was trying to make my own game in XCode. I do not have a developer's license yet, so I don't think I can put the game on my phone to try it out. I found several different ways to play sounds, and I wanted to put a 30 second song I made in garageband in the background to see how it works. I tried it with a .mp3 file, but that did not work. I found more resources on how to put a .wav file in, so I converted it and tried with that. I followed several different tutorials and instructions exactly, but none of them worked. Finally I ended up doing it like this and trying to run it, but it did not play in the simulator. I have tried unchecking and checking the Play User Interface Sound Effects button in System Preferences like people said, but it did not work. I used AudioToolbox framework. Here is my code for the music:
.h
#import <AudioToolbox/AudioToolbox.h>
SystemSoundID BackgroundMusicID;
.m (I did not #import anything into the .m, but I tried it) The file name for the music is "BackgroundMusic.wav" exactly.
- (void)viewDidLoad
{
NSURL *BackgroundMusicURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"BackgroundMusic" ofType:#"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)BackgroundMusicURL, &BackgroundMusicID);
AudioServicesPlaySystemSound(BackgroundMusicID);
}
Thank you for any help! If you have any ideas as to why it won't play, or if it is just a problem with the simulator (I saw other people saying that, but none of their fixes worked unless I did something wrong). I would like the song to play as soon as the game loads up, but I have tried putting AudioServicesPlaySystemSound(BackgroundMusicID); under a button and under touchesbegan. Again, any help is appreciated!
Your code looks fine. But try the following:
NSString *BackgroundMusicURL = [[NSBundle mainBundle] pathForResource:#"BackgroundMusic" ofType:#"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:BackgroundMusicURL], &soundID);
AudioServicesPlaySystemSound(BackgroundMusicID);
Another thing is:
Don't try creating a clip that is exactly 30 seconds in length, instead try it first with 25-second or a 28-second clip.
And if that still does't work, please provide more code specifically where your dispose your sound and if you ave animation code, please post those as well.
Try changing the NSURL to use: URLForResource: withExtension: instead of fileURLWithPath:
- (void)viewDidLoad
{
NSURL *BackgroundMusicURL = [[NSBundle mainBundle] URLForResource:#"BackgroundMusic"
withExtension:#"wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)BackgroundMusicURL, &BackgroundMusicID);
AudioServicesPlaySystemSound(BackgroundMusicID);
}

AVAudioPlayer will not play audio in one project with all correct parameters, but will work fine in another project

I have a huge Xcode project, which I have been working on an update to. After installing the iOS 7.1 SDK, playing audio on the AVAudioPlayer in this project no longer works. I created a new, blank project to test out the exact same code, and it worked perfectly.
I know for certain that the file is copied under the bundle resources, the file is added to the target, the URL is perfect because I was able to get the NSData from the NSURL of the file, and it matched. The AVAudioPlayer is a property with both the strong and nonatomic attributes, but it will not play in this one project. I also made sure to set the AVAudioSession to playback mode.
I even created some blank view controller classes to test out the AVAudioPlayer in the project, and it would not work in any of them, but in the new, blank iOS project I made, the sounds plays fine.
In the .h
NSURL *soundUrl;
#property (nonatomic, strong) AVAudioPlayer *soundPlayer;
In the .m
#synthesize soundPlayer = _soundPlayer;
- (void)playSomeSound {
NSError *audioError;
soundUrl = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:soundFile ofType:#"m4a"]];
_soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&audioError];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];
_soundPlayer.delegate = self;
[_soundPlayer play];
}
soundFile is just a string of the name of the sound file that I am trying to play.
I try to log for errors in all the delegate methods of the audio player and in the initialization of the audio player. All errors return (null).
Unlike on iOS 7, where the audio simply does not play, on iOS 6.1, initializing the audio player causes a crash with an EXC_BAD_ACCESS code 2.
Also, the AVAudioPlayer in CocosDenshion seems to work. (I have cocos2d used in parts of my project, but it is not a game.)
Another (possibly important) note is that I use AVAudioRecorder in my project as well. That works perfectly without any issues, and I make sure to switch the AVAudioSessionCategory to playback when I am not recording.
Its due to the change that apple made to AVAudioSession properties in iOS 7.
As per Apple's documentation:
The audio category has changed (e.g. AVAudioSessionCategoryPlayback
has been changed to AVAudioSessionCategoryPlayAndRecord).
So change the line [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL]; to
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];
Considering the fact that exactly the same code works in your other project, it's hard to tell what is the problem in this project. But I can give you a couple of suggestions on further debugging it:
Call your -playSomeSound as the first thing you do in application:didFinishLaunchingWithOptions:. This way you will see whether it's not working because of something else you do before calling it.
Add another sound to your project, and try playing that one. Perhaps still the problem is with the original sound.
Also a couple questions:
Where is audioError declared?
What's the result of [_soundPlayer play] (should return YES on
success)?
hava a try:
[_soundPlayer prepareToPlay];
[_soundPlayer play];
Thanks everyone, but somehow it ended up being a conflict between some Frameworks. There was a bug in Kamcord SDK for iOS, but I am in contact with their development team, and they are working to fix it.
This issue is still apparent in the latest Kamcord SDK, so instead of everyone running in to this issue having to contact them, here's the smallest amount of code/changes you need to fix AVAudioPlayer, and to have Kamcord also include audio from AVAudioPlayer in it's recordings.
First, add OpenAL to your frameworks.
Import OpenAL headers in AppDelegate.m
#import <OpenAl/al.h>
#import <OpenAl/alc.h>
Set up static variables for OpenAL device and context:
static ALCdevice *openALDevice;
static ALCcontext *openALContext;
Create two functions to start and to stop OpenAL
- (void)startOpenAL
{
NSLog(#"OpenAL: Starting");
openALDevice = alcOpenDevice(NULL);
openALContext = alcCreateContext(openALDevice, NULL);
alcMakeContextCurrent(openALContext);
}
- (void)stopOpenAL
{
NSLog(#"OpenAL: Stopping");
alcMakeContextCurrent(NULL);
alcDestroyContext(openALContext);
alcCloseDevice(openALDevice);
}
In AppDelegate.m's didFinishLaunchingWithOptions, start OpenAL (I do it before initializing Kamcord)
[self startOpenAL];
Finally, in applicationWillTerminate, call the stop OpenAL
[self stopOpenAL];
You're done. AVAudioPlayer should now start working again and all of your audio is correctly being recorded by Kamcord.

How to stream mp3 files (iOS)

I have found many related questions on SO, but or because I am very new, or because it doesn't exactly fit my problem, I can't find a way to succeed.
So, all I would like to do is playing an audio file stored on an exernal server directly on the iOS device (iPhone in my case)
I have tried differents solution on the net, and this is what I get until now.
NSString* resourcePath = #"http://www.chiptape.com/chiptape/sounds/medium/laugh.mp3"; //your url
NSURL *url = [[NSURL alloc]initWithString:resourcePath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithURL:url error:&error];
audioPlayer.delegate = self;
[url release];
[audioPlayer prepareToPlay];
[audioPlayer play];
The compiler show me this error:
No visible #interface for 'AVAudioPlayer' declares the selector
'initWithURL:error:'
I am not sure at all about my code, and would like your guys to know I have started iOS coding yesterday, so it's not impossible I'm missing something so obvious that tutorials/posts don't mention it.
Also, I have imported the library AVFoundation and imported it on my .h file.
I have also declared this on my .h file
AVAudioPlayer *audioPlayer;
I hope there is an easy way to stream audio file on iOS as provide android/java
Thank you and if you need more details comment and I'll update my post.
Try this:
NSString* resourcePath = #"http://www.chiptape.com/chiptape/sounds/medium/laugh.mp3"; //your url
NSURL *audioURL = [NSURL URLWithString:resourcePath];
AVAudioPlayer *audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL: audioURL error:&error]autorelease];
[audioPlayer play];
I'm working with it recently.
AVAudioPlayer can't work with audio stream. In fact, there isn't an easy way to do that.
You need to learn some Core Audio things, get the raw data of media, play them in audioQueue (audioToolBox framework). Real-time audio processing is not a simple work, it's full of c-level api and so many dirty work, don't rush, have fun :)
Check out the book Learning Core Audio and this open-source audio streamer.
Let me know if you need more help.

Resources