I am writing an app that will play MIDI files from parsed URL's. Currently, my code downloads the file then plays it using MusicPlayer and MusicSequence.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
NSData *midData = [[NSData alloc] initWithContentsOfURL:playURL];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"part.mid"];
[midData writeToFile:filePath atomically:YES];
NSLog(#"Downloaded");
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *url = [NSURL fileURLWithPath:filePath];
MusicSequence s;
NewMusicSequence(&s);
MusicSequenceFileLoad(s, (__bridge CFURLRef)(url), 0, 0);
NewMusicPlayer(&midPlayer);
MusicPlayerSetSequence(midPlayer, s);
MusicPlayerPreroll(midPlayer);
MusicPlayerStart(midPlayer);
MusicTrack t;
MusicTimeStamp len;
UInt32 sz = sizeof(MusicTimeStamp);
MusicSequenceGetIndTrack(s, 1, &t);
MusicTrackGetProperty(t, kSequenceTrackProperty_TrackLength, &len, &sz);
midPlaying = YES;
});
});
What I'm looking for is a notification when the file reached its end, similar to something like AVPlayer:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
Is this possible? I've been looking through Apple's documentation and cannot find anything.
Documentation: MusicSequence Reference, MusicPlayer Reference
I don't recall ever seeing any built in way to do that but you can easily implement it through polling playback status on your own. CADisplayLink is a built in timer you might want to use if you are updating a progress bar or doing other screen drawing (or are just lazy like me).
Being confronted to the same problem and having searched around, I don't think there's a way of doing that.
Related
I am building up a APP on iOS by AudioKit(version 4.5.3), and I find out the AKTimePitch class does not work for me, here is my code(objective-c xcode 10):
(IBAction)startButton:(id)sender {
NSURL *url = [[NSBundle mainBundle] URLForResource:#"burncalory" withExtension:#"m4a"];
AKAudioFile *file = [[AKAudioFile alloc] initForReading:url error:nil];
AKAudioPlayer *player = [[AKAudioPlayer alloc] initWithFile:file looping:NO lazyBuffering:YES error:nil completionHandler:^{
NSLog(#"Finished!");
}];
AKTimePitch *akTimePitch = [[AKTimePitch alloc] init:player rate:2.0 pitch:1600 overlap:8];
AudioKit.output = akTimePitch;
[akTimePitch start];
[AudioKit startAndReturnError:nil];
[player playFrom:0.0];
}
I check out the playground(4.5.3), and the sample of "Time Stretching and Pitch Shifting" works well.
Is there something wrong in my code to use AKTimePitch or something wrong with my audio file example.m4a? By the way, this audio file can be loaded and play well by AKAudioPlayer.
After some testing I found that the parameter in the init method does not work, but after I add akTimePitch.pitch=1600 before [player playFrom:0.0], then the AKTimePitch effects works!! I don't know why the AKTimePitch *akTimePitch = [[AKTimePitch alloc] init:player rate:2.0 pitch:1600 overlap:8]; just does not work...
On “back to menu” button press there is a sound that should be played, but the sound is not playing because of the navigation to the main view controller. I tried doing this so it waits for the sound before going back, but it doesn’t work, what should I do?
if(appDelegate.disabledSoundEffects==0)
[self performSelectorOnMainThread:#selector(playButtonSound)
withObject:NULL waitUntilDone:true];
playButtonSound is the method that plays the sound obviously that’s the code for it in case I’m doing something wrong there
dispatch_queue_t dispatchQueue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_sync(dispatchQueue, ^(void){
NSBundle * mainBundle = [NSBundle mainBundle];
NSString * filePath = [mainBundle pathForResource:#"clickSound"
ofType:#"mp3"];
NSData * fileData=[NSData dataWithContentsOfFile:filePath];
NSError * error= nil;
self.audioPlayer=[[AVAudioPlayer alloc] initWithData:fileData error:&error];
if(self.audioPlayer!=nil)
self.audioPlayer.delegate=self;
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
});
As #Surjeet said "Instead of allocating AVAudioPlayer within the class. Allocate this in AppDelegate class, then create a method in appdelegate, and call that method from any class button click. This way your sound always plays”.
I have read some threads about this topic, and currently have this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *soundFilePath = [NSString stringWithFormat:#"%#/testsoundsr.aiff",
[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath: soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
player.numberOfLoops = -1; //Infinite
[player play];
}
The sound is not played though, and I can only imagine that the problem is with the NSString definition which is used to reference the file
The file I'm trying to play is named 'testsoundsr.aiff' and I have dragged that into the project. Also I have added several frameworks to the project, including AudioToolBox.framework
---UPDATE---
I have now tried using an .mp3 file instead, but still getting no sound through.
Try player.delegate = self before [player play]
I have a ViewController : GLKViewController
with this code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"background-music-aac" ofType:#"caf"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithData:data error:nil];
audio.delegate = self;
audio.volume = 1.0;
audio.numberOfLoops = -1;
[audio prepareToPlay];
[audio play];
[self setupContext];
[self setupDisplay];
}
However... my graphical things working fine but the iOS simulator plays no music.
did I something wrong?
Assuming you are using ARC, the player is getting deallocated as soon your method returns to its caller. To quote from this answer:
ARC inserts a release call to the audio player, so it's deallocated right after leaving the method where it is created.
Try implementing the AVAudioPlayer as a property.
If your data is nil, the file was not properly added to the project. To add your file to the project, just drag and drop it and add it to your targets.
Also, AVAudioPlayer has a method, initWithContentsOfURL:, which you might want to use.
hi does anyone know how to add audio to buttons i have added the framework creatd an action that place the sounds but when i press the button no sound plays?
heres what i have done so far
-(void)buttonSound{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/ButtonSound.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer2.numberOfLoops = -1;
[audioPlayer2 play];
}
- (IBAction)backToMenu:(id)sender {
[self buttonSound];
[self.navigationController popViewControllerAnimated:YES];
}
For short system sounds, you should use something like:
SystemSoundID sound;
CFURLRef alertURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), (CFStringRef)#"ButtonSound", CFSTR("mp3"), NULL);
OSStatus status = AudioServicesCreateSystemSoundID(alertURL, &sound);
AudioServicesPlayAlertSound(sound);
your audioplayer is getting released by immediately popping up navigation controller.
if you remove this line:
[self.navigationController popViewControllerAnimated:YES];
you should be able to hear the audio file (if the path is correct).