Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would suggest you create a global player object. Because right now, each time you push this view on the navigation controller you create a new one. This also means that you do not have any reference to the previous player (and can not stop it). So: declare the AVAudioPlayer one step higher (in the tableview). Now, when you select a row in it, assign it to a property of this new view (the one you linked).</p> <p>Do you work with storyboard? Then you have to implement the method <code>prepareForSegue:</code>. Give your segue on the storyboard an identifier (like <code>showPlayer</code> and check for that with <code>if (segue.identifier isEqualToString:@"showPlayer")</code>).</p> <p>Now in your <code>viewDidLoad</code> do a check if <code>audioPlayer</code> is nil. If you </p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; if (audioPlayer == nil) { // your init (audioplayer =[[AVAudioPlayer ... } else { if (audioPlayer.isPlaying) { // i.e. pause the player or do other stuff } } } </code></pre> <p>Hope this helps you. </p> <p><strong>Also</strong>: Do not post images of code. Just insert the code in your response or post it to a site like pastebin.com and link that page in your question. This makes it easier for others to respond and make suggestions how to alter your code.</p> <p><strong>In response to your comment</strong>: The relevant stuff should be: <em>In AudioTableViewController.h</em>:</p> <pre><code>@property (strong, nonatomic) AVAudioPlayer *audioPlayer; </code></pre> <p><em>In AudioTableViewController.m</em>:</p> <pre><code>@synthesize audioPlayer; ... - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic may go here. Create and push another view controller. NSUInteger row = indexPath.row; if (row == 0) { self.audio1DetailViewController =[[Audio1DetailViewController alloc] initWithNibName:@"Audio1DetailViewController" bundle:nil]; self.audio1DetailViewController.title = @"Audio"; self.audio1DetailViewController.audioPlayer = self.audioPlayer; [self.navigationController pushViewController:self.audio1DetailViewController animated:YES]; [self.audio1DetailViewController release]; ... </code></pre> <p><em>Audio1DetailViewController.m</em></p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"001Fatiha" ofType:@"MP3"]]; NSError *error; // this is the important part: if we already have something playing, stop it! if (audioPlayer != nil) { [audioPlayer stop]; } // everything else should happen as normal. audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&amp;error]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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