Note that there are some explanatory texts on larger screens.

plurals
  1. POIOS - playing mp3 fails when moved to a separate class
    text
    copied!<p>I successfully played an mp3 file within the body of a class. However, when I move the functionality out to a separate class, it fails. </p> <p>Here's the working code: </p> <p>Header: </p> <pre><code>#import &lt;AVFoundation/AVFoundation.h&gt; @interface QuestionController : UIViewController &lt;UITableViewDataSource, UITableViewDelegate, UISplitViewControllerDelegate&gt; { AVAudioPlayer *audioPlayer; } </code></pre> <p>working code: </p> <pre><code>NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/A.mp3", [[NSBundle mainBundle] resourcePath]]]; NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&amp;error]; audioPlayer.numberOfLoops = 0; if (audioPlayer == nil) NSLog(@"audio errror: %@",[error description]); else [audioPlayer play]; </code></pre> <p>Here's the new class:</p> <p>Header:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import &lt;AVFoundation/AVFoundation.h&gt; @interface AudioPlayer : NSObject { } - (void *) playAudioFile:(NSString *) mp3File; @end </code></pre> <p>implementation: </p> <pre><code> #import "AudioPlayer.h" @implementation AudioPlayer - (void *) playAudioFile:(NSString *) mp3File { NSLog(@"mp3file to play: %@", mp3File ); NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@", mp3File, [[NSBundle mainBundle] resourcePath]]]; NSError *error; AVAudioPlayer *audioPlayer; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&amp;error]; audioPlayer.numberOfLoops = 0; if (audioPlayer == nil) { NSLog(@"audio errror: %@",[error description]); } else { [audioPlayer play]; } [audioPlayer release]; return 0; } @end </code></pre> <p>Here's the calling code:</p> <pre><code> AudioPlayer *audioPlayer = [[AudioPlayer alloc] init]; [audioPlayer playAudioFile:@"/A.mp3"]; </code></pre> <p>However, when it runs in the separate class, it doesn't successfully create the player and goes to the "audio player == nil" branch </p> <p>Here's the output:</p> <pre><code>012-07-21 07:14:54.480 MyQuiz[6655:207] mp3file to play: /A.mp3 2012-07-21 07:15:40.827 MyQuiz[6655:207] audio errror: Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)" </code></pre> <p>The url is "file://localhost/A.mp3"</p> <p>Any ideas what I'm doing wrong? I somehow have always have trouble when I'm refactoring to separate methods. It's frustrating. </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