Playing audio with controls in iOS

梦想与她 提交于 2019-12-03 00:25:29

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).

Do you work with storyboard? Then you have to implement the method prepareForSegue:. Give your segue on the storyboard an identifier (like showPlayer and check for that with if (segue.identifier isEqualToString:@"showPlayer")).

Now in your viewDidLoad do a check if audioPlayer is nil. If you

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (audioPlayer == nil)
    {
         // your init (audioplayer =[[AVAudioPlayer ...
    }
    else
    {
         if (audioPlayer.isPlaying)
         {
             // i.e. pause the player or do other stuff
         }
    }
}

Hope this helps you.

Also: 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.

In response to your comment: The relevant stuff should be: In AudioTableViewController.h:

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

In AudioTableViewController.m:

@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];
        ...

Audio1DetailViewController.m

- (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:&error];

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

to stop 2 songs playing at a same time you should do this:

- (void)viewWillDisappear:(BOOL)animated
{
    audioPlayer = nil;
 volumeControl = nil;
}

for more help in playing audio you can use apple sample code:

http://developer.apple.com/library/ios/#samplecode/avTouch/Introduction/Intro.html

Surprisingly the MPMoviePlayerController also plays MP3 player with controls!!!

self.moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
[self.moviePlayer.view setFrame:CGRectMake(0, 0, self.videoContainer.bounds.size.width, self.videoContainer.bounds.size.height)];
self.moviePlayer.controlStyle=MPMovieControlStyleDefault;
[self.videoContainer addSubview:self.moviePlayer.view];
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!