Note that there are some explanatory texts on larger screens.

plurals
  1. POAVAudioPlayer not playing when outsourced in class
    text
    copied!<p>I'm relatively new to iOS development, but not new to (object-orientated) programming in general.</p> <p>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, <code>AudioPlayer</code>, so I can access it from different controllers. I does everything it should, except playing any sound. Heres a simplified project of my problem:</p> <p><code>ViewController.m</code>:</p> <pre><code>- (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]; } </code></pre> <p>The rest of the file is empty/standard.</p> <p><code>ViewController.h</code>:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "AudioPlayer.h" @interface ViewController : UIViewController @property (strong, nonatomic) IBOutlet UIButton *playButton; - (IBAction)playButtonPressed:(id)sender; @end </code></pre> <p><code>AudioPlayer.m</code>:</p> <pre><code>#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:&amp;err]; if(err) { NSLog(@"%@",err); } aPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:myURL error:&amp;err]; if(err) { NSLog(@"%@",err); } [aPlayer play]; NSLog(@"in myPlayAudio; after play"); //[audioSession setActive:NO error:&amp;err]; return err; } @end </code></pre> <p><code>AudioPlayer.h</code>:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import &lt;AVFoundation/AVFoundation.h&gt; @interface AudioPlayer : NSObject &lt;AVAudioPlayerDelegate&gt; { AVAudioPlayer *aPlayer; } @property (nonatomic, strong) AVAudioPlayer *aPlayer; -(id) myPlayAudio:(NSURL *) myURL; @end </code></pre> <p>I've added <code>AudioToolbox Framework</code>, activated iTunes file sharing in the .plist file and loaded an <code>AudioMemo.m4a</code> file to the documents folder.</p> <p>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.</p> <p>When I make everything local (put everything in the <code>ViewController.m</code>), like most of the tutorials on the net, it works fine.</p> <p>I searched, but didn't find this problem before, so I would be very thankful if anybody could help me.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload