Creating a sound in iOS - iphone

I am using the following that I pulled out of a tutorial to help me play a quick sound in my game, but I will be using this very often to play sounds. Should I be doing anything different? or is this a proper way to play a sound effect.
NSString *path = [[NSBundle mainBundle] pathForResource:#"Sound/Microwave_Bell_Ding" ofType:#"mp3"];
NSURL *pathURL = [NSURL fileURLWithPath:path];
AudioServicesCreateSystemSoundID((CFURLRef) pathURL, &soundID);
AudioServicesPlaySystemSound(soundID);
Thanks!

first,you can't play mp3 in this way.second,you can store soundID,when you play next time,just call AudioServicesPlaySystemSound(soundID);

Related

AVAudioPlayer error with m4a but not mp3

I'm pretty confused by this one and can't think of anything obvious that I am doing wrong.
I can't get iOS to pull an m4a out of my app bundle
eg
NSURL *clickurl = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/fishy2.m4a", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:clickurl
error:&error];
audioPlayer.delegate = self;
if (error)
NSLog(#"Error: %#",
[error localizedDescription]);
else
[audioPlayer play];
I get an error -43
However, if I instead use an identical mp3 instead
NSURL *clickurl = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/fishy.mp3", [[NSBundle mainBundle] resourcePath]]];
everything is happy
I've tried this many many times across different projects and can't see where I'm going wrong. I've been using NSFileManager to check if it is there and it says no to the m4a, but yes to the mp3. Tried this with all manner of different methods of importing different audio files in different formats and I can't seem to get it to find the m4a (and it really really has to be m4a). mp3, wav, caf etc... all work. Interestingly, the m4as that I run from the user documents directory work just fine
All I want is to be able to copy the file!
Any ideas at all?
Work around = right click and "add files to " instead of dragging

Play video in iPhone using HTML5 and UIWebView

I am writing a small POC to play the video in iPhone app. I would like to do so in UIWebView with the help of HTML5. I dont want to use standard players like AVPlayer or MPMoviePlayerController.
Can someone throw some light on it?..
I am new to this HTML5 environment on iPhone. So, any help to start this POC would be appriciated.
I forget to add that i would like to play h264 live stream on server.
Thanks in advance.
Yes, you can do that:
NSString *bundleDirectory = [[NSBundle mainBundle] pathForResource:#"mov" ofType:nil];
NSString *filePath = [bundleDirectory stringByAppendingPathComponent:#"loopvideo.html"];
NSString *HTMLData = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[self.web loadHTMLString:HTMLData baseURL:[NSURL fileURLWithPath:bundleDirectory]];
In your HTML, you can embed the <video> tag and it should play.

sounds are not being played in my iOS app?

I have an app that should play 3 distinct sounds and I define them as so in my ViewDidLoad:
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"2357_02" ofType:#"m4a"]], &addedSound);
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"2357_01" ofType:#"m4a"]], &updatedSound);
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"notifier_12" ofType:#"m4a"]], &notFoundSound);
And I cue them to play like so:
AudioServicesPlaySystemSound(addedSound);
They play in the simulator but not on the ipad? In my Build Phases under Copy Bundle Resources I see them listed so I assumed that they should make it as part of my app - what am I missing here?
I actually used the solution suggested by Tammenn on this question and it worked great for me:
Sound on simulator but not device

Couldn't put a song into an app

Have already tried that out and failed
I put a .wav file into my app and it was fine. So I tried to put a .wav song file into the app. When runs, there's no sound coming out. ( the song is converted from .mp3 using iTunes)
Any ideas how can I fix this? Thanks in advance
one possible reason the sound file is too large. You have different kinds of audio player in iOS some of them can only play files that are really short (something like 1-3 sec). To fix that you have to choose another player like CoreAudio.
The second possible failure is the simulator. The simulator is not always doing the same, the device do. Try running on real device and check if it's working.
// EDIT: sample code:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"mySound" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL URLForString:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundFileURL];
[player play];
[player pause];

Stop sound in iPhone

SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle] pathForResource:#"static" ofType:#"caf"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
I have implemented above code in my application.
It works perfectly, but the problem is I don't know how to stop it.
i.e Sound is of 10 sec.
Before 10 sec. if user taps button, sound should be stopped? How can I do so?
Try AudioServicesDisposeSystemSoundID, it stops sounds immediately. You'll need to recreate the sound if you intend to use it again.
Use the following instruction for disposes of a system sound object and associated resources:
AudioServicesDisposeSystemSoundID(soundID);
I would suggest using the AVFoundation framework. Here is an amazing tutorial on how to play, pause, and stop sound using the AVAudioPlayer.