How can I play .mid file on iOS device - objective-c

I tried AVAudioPlayer it doesnt work.
url= [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/test.mid", [[NSBundle mainBundle] resourcePath]]];
NSLog(#"URL Description is %#",[url description]);
NSError *error=nil;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error!=nil) {
NSLog(#"Error is %#",[error localizedDescription]);
}
[audioPlayer play];
I get this error
Error is The operation couldn’t be completed. (OSStatus error 1954115647.)
Should I be using another method to do this? Any suggestions?
Thanks

MusicPlayer+MusicSequence on ios 5

You can make use of the built-in CoreMIDI framework. Docs here:
http://developer.apple.com/library/mac/#documentation/MusicAudio/Reference/CACoreMIDIRef/MIDISetup/index.html
http://www.slideshare.net/invalidname/core-midi-and-friends
http://goodliffe.blogspot.com/2010/10/using-coremidi-in-ios-example.html
Hope it helps.

Related

AVAudioPlayer is not working in sprite kit scene Xcode 7

i have been using the following code for playing background music in a sprite kit game and it was working fine in Xcode 6.4 but it throws a exception and crashes the game now:
- (void)startBackgroundMusic
{
NSError *err;
NSURL *file = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"lostinspace.caf" ofType:nil]];
_backgroundAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:&err];
if (err) {
NSLog(#"error in audio play %#",[err userInfo]);
return;
}
[_backgroundAudioPlayer prepareToPlay];
_backgroundAudioPlayer.numberOfLoops = -1;
[_backgroundAudioPlayer setVolume:0.5];
[_backgroundAudioPlayer play];
}
declaring the above code in didMoveToView as :[self startBackgroundMusic];
i have looked up the docs but can't find out what is wrong with what i already have. anyone knows how to fix this issue? below is the counsel out put:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
Instead of:
NSURL *file = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"lostinspace.caf" ofType:nil]];
Try this:
NSURL *file = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"lostinspace" ofType:#"caf"]];

How to play system button click sound on iPad?

I need to play some system sound when users click button in my application which is running on iPad. How to implement it in iOS?
If you want to play a short sound (shorter than 30 sec), you can do it easily like this:
Note: You'll have to add AudioToolbox framework and import it (#import <AudioToolbox/AudioToolbox.h>)
SystemSoundID mySSID;
NSString *path = [[NSBundle mainBundle] pathForResource:#"beep" ofType:#"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: path], &mySSID);
AudioServicesPlaySystemSound(mySSID);
Also note that the file can be:
No longer than 30 seconds in duration
In linear PCM or IMA4 (IMA/ADPCM) format
Packaged in a .caf, .aif, or .wav file
You should use AVAudioPlayer.
There's a great tutorial here on using AVAudioPlayer to play sounds. A very simple example of it's use:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3",dataPath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
NSString *path = [[NSBundle mainBundle] pathForResource:#"gorilla 2" ofType:#"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];

File not found error in attempt to play audio (Obj-C)

I'm new to this, so in an attempt to keep things simple, I'm trying to add sound into my main.m
As far as I know, the following code should work, but when I run it, I get a "Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)" (File not found)"
I've tried it with various audio files and formats - added to the project in Xcode but can't figure out why it isn't finding my G1.wav
AVAudioPlayer *audioPlayer;
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"/G1.wav"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
EDIT removed NSBundle and mainBundle references - my misunderstanding
You might be better off with this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"G1" ofType:#"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
...
Using pathForResource:ofType: you don't have to worry about where in the hierarchy of your bundle the file is. The API call figures it out. And in your code, whatever resourcePath is returning is clearly not the right path.
You might add some NSLog() calls here and there to see what is really happening, as well.

AVAudioPlayer play audio properly in Ipod Touch 4g but not in Ipad, Ipad2, why?

Here is my code:
NSError *error;
NSString* path = [[NSBundle mainBundle] pathForResource:[settings alarmAudioName] ofType:#"mp3"];
NSLog(#"%#",path);
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
NSLog(#"%#",[NSURL fileURLWithPath:path]);
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
{
[audioPlayer prepareToPlay];
[audioPlayer play];
NSLog(#"Should have been played");
}
NSLog:
/var/mobile/Applications/9B5B4E10-A936-4D90-8CBE-17854A0B22F7/SSCA Development.app/Song 1.mp3
file://localhost/var/mobile/Applications/9B5B4E10-A936-4D90-8CBE-17854A0B22F7/SSCA%20Development.app/Song%201.mp3
Its works correct in Ipod Touch 4g, but not working in Ipad, Ipad2, why?
Thanks the answers,

Audio playback does not start

NSError *err;
// Initialize audio player
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
audioPlayer.delegate = self;
[audioPlayer play];
With the code above, I'm trying to initialize playback of a .mp3 file, however the playback does not start at all. There is no sound. What am I doing wrong? I have inspected 'err' and there is nothing there.
Edit: After adding AVAudioSession, I'm getting the following error from AVAudioPlayer
The operation couldn’t be completed. (OSStatus error -43.)
Apparently AVAudioPlayer does not support streaming via. HTTP as I was trying to do, so by using AVPlayer instead, I got it working.
Did you initialize a AVAudioSession?
The following works fine for me. Might give you a hint of whats not working for you.
Initializing:
AVAudioSession* session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryAmbient error:nil];
[session setActive:TRUE error:nil];
Loading:
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
audioPlayer.numberOfLoops = 0;
[audioPlayer prepareToPlay];
audioPlayer.volume = 1.0;
Playing:
[audioPlayer play];
Update
To find the right NSRUL for fileUrl, I used this code:
NSURL* fileUrl = [NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:#"MySound" ofType:#"wav"] isDirectory:NO];
And then I added MySound.wav to the project (bundle).