I'm relatively new to iOS development, but not new to (object-orientated) programming in general.
I am programming a kind of recorder app, and I need your help when it comes to this:
I've tried to outsource the AVAudioPlayer object into an extra class, AudioPlayer, so I can access it from different controllers.
I does everything it should, except playing any sound.
Heres a simplified project of my problem:
ViewController.m:
- (IBAction)playButtonPressed:(id)sender
{
NSArray *pathComponents = [NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],#"AudioMemo.m4a",nil];
NSURL *fileURL = [NSURL fileURLWithPathComponents:pathComponents];
AudioPlayer *myPlayer = [[AudioPlayer alloc] init];
[myPlayer myPlayAudio:fileURL];
}
The rest of the file is empty/standard.
ViewController.h:
#import <UIKit/UIKit.h>
#import "AudioPlayer.h"
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIButton *playButton;
- (IBAction)playButtonPressed:(id)sender;
#end
AudioPlayer.m:
#import "AudioPlayer.h"
#implementation AudioPlayer
#synthesize aPlayer;
-(id) myPlayAudio:(NSURL *)myURL
{
NSLog(#"in myPlayAudio; before play");
NSError *err = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:NULL];
[audioSession setActive:YES error:&err];
if(err)
{
NSLog(#"%#",err);
}
aPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:myURL error:&err];
if(err)
{
NSLog(#"%#",err);
}
[aPlayer play];
NSLog(#"in myPlayAudio; after play");
//[audioSession setActive:NO error:&err];
return err;
}
#end
AudioPlayer.h:
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#interface AudioPlayer : NSObject <AVAudioPlayerDelegate> {
AVAudioPlayer *aPlayer;
}
#property (nonatomic, strong) AVAudioPlayer *aPlayer;
-(id) myPlayAudio:(NSURL *) myURL;
#end
I've added AudioToolbox Framework, activated iTunes file sharing in the .plist file and loaded an AudioMemo.m4a file to the documents folder.
It shows the two NSLogs (before + afterplay), but doesn't make any noise. I know I didn't enable the loudspeaker of the iPhone, i wanted to keep it simple in this case.
Of course I checked volume, silent mode etc.
When I make everything local (put everything in the ViewController.m), like most of the tutorials on the net, it works fine.
I searched, but didn't find this problem before, so I would be very thankful if anybody could help me.
As I see you are using ARC, so this object:
AudioPlayer *myPlayer = [[AudioPlayer alloc] init];
is released shortly after you allocated it. Keep it in a property instead of a local variable and it should work.
Related
I have a series of sound files ranging from 0.3s to 3.0s.
When trying to play with AVAudioPlayer, I get nothing, unless I add a sleep(4) call after to ensure the sound can play. Really weird.
Apart from that, no errors with the error param that gets passed in, and the [player play] returns YES every time. The delegate methods are not called, though, oddly enough.
Here's my code.
(.m file)
#interface MyClass ()
#property (nonatomic, strong) AVAudioPlayer *soundPlayer;
#end
#implementation SLInspireKit
//view did load here
- (void)playRandomSound {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
NSError *error;
NSString *musicFileName = [NSString stringWithFormat:#"my-sound-%d.aiff", arc4random_uniform(8)];
NSString *path = [NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] resourcePath], musicFileName];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
self.soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&error];
self.soundPlayer.delegate = self;
[self.soundPlayer setVolume:1.0];
[self.soundPlayer prepareToPlay];
if ([self.soundPlayer play]){
NSLog(#"Played fine");
} else {
NSLog(#"Did not play fine");
}
sleep(1); //uncomment this out, and nothing. Set to 1, first second of each sound, 2, 2 seconds and so on.
}
Any ideas? I don't think it's an ARC thing, but it could be :/ I have to initiate each time because the sound changes each time.
Quick summary of the comments:
Turns out the sleep(n) was needed since the entire class containing the AVAudioPlayer was deallocated.
Retaining the class fixes the issue.
Cheers!
Okay, so I'm working on a game using SpriteKit, and there is background music throughout the app. Initially, I had just used a new AVAudioPlayer in each scene with the appropriate audio file, but now I am finding that I'd like to have uninterrupted audio playback when moving between menus and such. So I am attempting use a separate class to handle all of the background music. I'm not sure if this is technically possible, if not, I'd like to see if there's another way to solve my problem.
So onto the code... here's the header file for my audio class:
#import <AVFoundation/AVFoundation.h>
typedef NS_ENUM(int, songTitle) {
Menu_Music,
Level_1,
Level_2,
Game_Over,
};
#interface NWAudioPlayer : AVAudioPlayer
#property (nonatomic, assign) songTitle songName;
#property (nonatomic) AVAudioPlayer* bgPlayer;
-(void)createAllMusicWithAudio: (songTitle)audio;
#end
and here's the implementation:
#import "NWAudioPlayer.h"
#implementation NWAudioPlayer
-(void)createAllMusicWithAudio: (songTitle)audio {
if ([[GameState sharedGameData] audioWillPlay] == YES) {
switch (audio) {
case Menu_Music:
[self playMusicWithString:#"menuMusic"];
break;
case Level_1:
[self playMusicWithString:#"Level-1-Music"];
break;
case Level_2:
[self playMusicWithString:#"Level-2-Music"];
break;
case Game_Over:
break;
default:
break;
}
}
}
-(void)playMusicWithString: (NSString *)file {
NSString *soundFile = [[NSBundle mainBundle] pathForResource:#"menuMusic" ofType:#"m4a"];
NSURL *soundFileUrl = [NSURL fileURLWithPath:soundFile];
NSError *Error = nil;
_bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileUrl error:&Error];
_bgPlayer.numberOfLoops = -1;
[_bgPlayer prepareToPlay];
NSLog(soundFile);
}
#end
And then here's what I have in my scene's implementation:
-(void)createAudio
{
[[NWAudioPlayer alloc] createAllMusicWithAudio:Menu_Music];
[[NWAudioPlayer alloc].bgPlayer play];
}
and obviously I'll call [self createAudio] in the init for that scene.
The frustrating part is that I'm not getting a single error, and my NSLog is showing that the method is calling the correct audio file, but nothing is playing. Clearly I'm missing something... I'm stumped. Any help is appreciated! Thanks so much!
Separate the code that manages an AVAudioPlayer from the scene code. You could put it in your application delegate, for example, or create your own music player class with a singleton instance.
in my project i have Url of audio...look at my code....
ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController<AVAudioPlayerDelegate,NSURLConnectionDelegate>
{
AVAudioPlayer *song;
// NSMutableData *fileData;
NSData *myData;
}
#property (nonatomic, retain) AVAudioPlayer *song;
#property(nonatomic,retain) NSData *myData;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize song,myData;
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *myUrl;
myUrl = [NSURL URLWithString:#"http://p0.bcbits.com/download/track/60523f9784a2912348eff92f7b7e21e8/mp3-128/1269403107?c22209f7da1bd60ad42305bae71896761f6cd1f4d6c29bf401ef7e97b90c3d5939b57f5479b37ad4d41c48227740d7ba8b5f79b76aa8d6735b8f87c781f44fb019762a2903fe65aeafb30d90cb56b385e27cd770cc9f9d60e5ea3f130da6ad3db728ff7e24a09fab9f351120810ee91f78f1e94fcabc98aabb37d4248358f6da4a0fa7a74fe8c89da2a768&fsig=cd06c325fc41b6f8edb0409011e8839c&id=1269403107&stream=1&ts=1395489600.0"];
myData = [NSData dataWithContentsOfURL:myUrl];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
in above code how can convert mydata into one array?????
or how can i download this audio and store in phone memory??
You can store your audio data to device memory as like below
myData = [NSData dataWithContentsOfURL:myUrl];
[myData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:#"Documents/audio.mp3"] atomically:YES];
To play an audio file that you saved in device :
Add AudioToolbox.framework and AVFoundation.framework to your project build
In .h file
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
AVAudioPlayer *player;
In .m file
-(void)initPlayer
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
NSError *err;
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];
audioSession.delegate = self;
[audioSession setActive:YES error:&err];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:#"Documents/audio.mp3"]] error:nil];
}
-(void)playFile
{
if (player.playing)
{
[player pause];
isPlaying = NO;
}
else
{
[player play];
isPlaying = YES;
}
}
Thanks!
Your code for downloading the file has several problems. First, as you say, it will download the file every time. Second, you are using a synchronous download call. That code will lock up the app's user interface until the download completes or fails. If there is a network problem that could take up to 2 minutes. Rule of thumb: Don't EVER download synchronously.
You should take a look at the NSURLConnection method sendAsynchronousRequest:queue:completionHandler:. That method will let you download a file asynchronously and execute a block of code once the download is complete.
If you want to be able to play your sound quickly, it's a good idea to move the code to download the file into your applicationDidFinishLaunching:withOptions: code in the app delegate. Put code there that checks to see if the file exists, and starts downloading it if not using sendAsynchronousRequest:queue:completionHandler:. Then, by the time you need to play the sound, it should be loaded. (The download might have failed or not be completed, so you do still need to check if it is available at the time you need to play it.)
I'm struggling to follow Apple's documentation regarding playing back a small .wav file using the AVAudioPlayer class. I'm also not sure what toolboxes I need for basic audio playback. So far I have imported:
AVFoundation.framework
CoreAudio.framework
AudioToolbox.framework
Here is my code .h and .m:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController <AVAudioPlayerDelegate>
- (IBAction)playAudio:(id)sender;
#end
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)playAudio:(id)sender {
NSLog(#"Button was Pressed");
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"XF_Loop_028" ofType:#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
// create new audio player
AVAudioPlayer *myPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
[myPlayer play];
}
#end
I don't seem to have any errors. However, I am not getting any audio.
You're having an ARC issue here. myPlayer is being cleaned up when it's out of scope. Create a strong property, assign the AVAudioPlayer and you're probably all set!
#property(nonatomic, strong) AVAudioPlayer *myPlayer;
...
// create new audio player
self.myPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
[self.myPlayer play];
Is the filePath coming back with a valid value? Is the fileURL coming back with a valid value? Also, you should use the error parameter of AVAudioPlayer initWithContentsOfURL. If you use it it will likely tell you EXACTLY what the problem is.
Make sure you check for errors and non-valid values in your code. Checking for nil filepath and fileURL is the first step. Checking the error parameter is next.
Hope this helps.
What I do is create an entire miniature class just for this purpose. That way I have an object that I can retain and which itself retains the audio player.
- (void) play: (NSString*) path {
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
NSError* err = nil;
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: &err];
// error-checking omitted
self.player = newPlayer; // retain policy
[self.player prepareToPlay];
[self.player setDelegate: self];
[self.player play];
}
I'm very new to programming in Xcode and have already hit a brick wall.
What I am trying to do is to get a sound to play if there is sufficient acceleration detected.
Can someone please advise me on what I am doing wrong? (I suspect its everything).
CODE:
#import "LZDViewController.h"
#interface LZDViewController ()
#end
#implementation LZDViewController
- (void)viewDidLoad;{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CMAccelerometerData *accelerometerData = self.motionManager.accelerometerData;
if (fabsf(accelerometerData.acceleration.x) > 1.2
|| fabsf(accelerometerData.acceleration.y) > 1.2
|| fabsf(accelerometerData.acceleration.z) > 1.2)
{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audio.wav", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
}
}
#end
EDIT:
Here is the code for my .h
#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>
#import <AVFoundation/AVFoundation.h>
#interface LZDViewController : UIViewController
//{AVAudioPlayer *audioPlayer;}
#property (strong, nonatomic) CMMotionManager *motionManager;
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
#end
I'm still not getting any joy at all, and I do apologise for the hand holding.
All I get is a white screen but the sound file never plays. I have imported the correct framework and can get the sound playing with just a button, but I really want to play with the accelerometer.
Thanks
Alright, the first thing you're going to want to do is define two properties in either your interface file, or in the interface extension at the top of your implementation file. These will be the motion manager, and the audio player.
#property (strong, nonatomic) CMMotionManager *motionManager;
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
Then we can get everything else done in viewDidLoad. The code below sets up a new operation queue for the motion manager to receive accelerometer events on, initializes the audio player and the motion manager, and then starts getting accelerometer updates at an interval of 30 times per second.
Finally, within the accelerometers update block, just use the same logic you already build to check the acceleration data and if it meets your requirements continue on to the next condition.
**I have configured this example to not start playing the sound again if the player is already playing
- (void)viewDidLoad
{
[super viewDidLoad];
NSOperationQueue *motionQueue = [NSOperationQueue new];
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:#"audioFile" ofType:#"m4a"]];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.audioPlayer prepareToPlay];
self.motionManager = [CMMotionManager new];
[self.motionManager setAccelerometerUpdateInterval:1.0f/30.0f];
[self.motionManager startAccelerometerUpdatesToQueue:motionQueue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
if (fabsf(accelerometerData.acceleration.x) > 1.8 || fabsf(accelerometerData.acceleration.y) > 1.8 || fabsf(accelerometerData.acceleration.z) > 1.8) {
NSLog(#"%#",accelerometerData);
if (self.audioPlayer) {
if (!self.audioPlayer.isPlaying) {
[self.audioPlayer play];
}
}
}
}];
}