简体   繁体   中英

AVAudioPlayer mp3 file not playing

I am an amateur developer making an ios app which includes a sound file (mp3) being played when a button impressed. However, the sound is not playing (tested on a mobile device, not ios simulator). I already have the sound file in bundle resources and have added AVFoundation to link binary with libraries. I have xcode 4.6 and have mac osx 10.8.4. Here is my code for viewcontroller.h:

#import <UIKit/UIKit.h>
#import <RevMobAds/RevMobAds.h>
#import <AVFoundation/AVFoundation.h>


@interface ViewController : UIViewController 


- (IBAction)playButton:(id)sender;
- (IBAction)stopButton:(id)sender;

@property (nonatomic, strong) AVAudioPlayer *avPlayer;

- (void)revmob;
@end

Viewcontroller.m:

#import "ViewController.h"
#import <RevMobAds/RevMobAds.h>


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[RevMobAds session] showBanner];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *stringPath = [[NSBundle mainBundle] pathForResource:@"98648__dobroide__20100606-mosquito" ofType:@"mp3"];
    if (stringPath) 
    {
        NSURL *url = [NSURL fileURLWithPath:stringPath];

        NSError *error;

       _avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
        if (!error) 
        {
            [_avPlayer setNumberOfLoops:-1];
        } 
        else  
        {
            NSLog(@"Error occurred: %@", [error localizedDescription]);
        }
    } 
    else 
    {
        NSLog(@"Resource not found");
    }
    [self performSelector:@selector(revmob) withObject:nil afterDelay:10.0];
}

- (void)revmob 
{
    [[RevMobAds session] showFullscreen];
}
- (IBAction)playButton:(id)sender 
{
    [_avPlayer play];
    NSLog(@"Sound playing");
}

- (IBAction)stopButton:(id)sender 
{
    [_avPlayer pause];
    NSLog(@"Sound Stopped");
}
@end

In ViewController.h import the AVFoundation header file: #import

Go the ViewController.m and add the following outlet properties and IBAction method definitions in the interface section:

@interface ViewController () 

@property (nonatomic, strong) IBOutlet UILabel *trackTitle;
@property (nonatomic, strong) IBOutlet UILabel *playedTime;

- (IBAction)stopSound:(id)sender; 
- (IBAction)playOrPauseSound:(id)sender;

@end

Go back to ViewController.xib and make the following connections:

top label -> trackTitle outlet

middle label -> playedTime outlet

left button -> IBAction playOrPauseSound

right button -> IBAction stopSound

Now the connections are made we can finish our code in ViewController.m. In the interface section add the following properties

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (nonatomic) BOOL isPlaying; 
@property (nonatomic, strong) NSTimer *timer;

在此处输入图片说明

Go the ViewController.m and add the following outlet properties and IBAction method definitions in the interface section.

@interface ViewController () 

@property (nonatomic, strong) IBOutlet UILabel *trackTitle;
@property (nonatomic, strong) IBOutlet UILabel *playedTime;

- (IBAction)stopSound:(id)sender; 
- (IBAction)playOrPauseSound:(id)sender;

@end

Change the viewDidLoad method in :

- (void)viewDidLoad 
{ 
  [super viewDidLoad];

  self.view.backgroundColor = [UIColor whiteColor];
  self.isPlaying = NO; 
  self.trackTitle.text = @"The Stone Foxes - EveryBody Knows";

  NSBundle *mainBundle = [NSBundle mainBundle]; 
  NSString *filePath = [mainBundle pathForResource:@"thestonefoxes-everybodyknows" ofType:@"mp3"]; 
  NSData *fileData = [NSData dataWithContentsOfFile:filePath];

  NSError *error = nil;

  self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&error] 
    [self.audioPlayer prepareToPlay]; 
}


- (IBAction)playOrPauseSound:(id)sender; 
{ 
  if (self.isPlaying)
  { 
    // Music is currently playing 
    [self.audioPlayer pause]; 
    self.isPlaying = NO; 
  } 
  else 
  { 
    // Music is currenty paused/stopped 
    [self.audioPlayer play]; 
    self.isPlaying = YES;

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; 
  } 
}

- (void)updateTime 
{ 
  NSTimeInterval currentTime = self.audioPlayer.currentTime;

  NSInteger minutes = floor(currentTime/60);
  NSInteger seconds = trunc(currentTime - minutes * 60);

  // update your UI with currentTime; 
  self.playedTime.text = [NSString stringWithFormat:@"%d:%02d", minutes, seconds]; 
}

- (IBAction)stopSound:(id)sender 
{ 
  [self.audioPlayer stop];
  self.audioPlayer.currentTime = 0; 
  self.isPlaying = NO; 
}

I've tested your code on my Mac and it seems to work just fine. It could be that the cause for your problem is that while your mp3 is visible in Xcode, it's not being added to the bundle during compilation. Like in this question .

To fix it, click the mp3 in Xcode, look at the File Inspector and check the Target Membership box next to your target (in the screenshot mine is SOTest).

检查目标会员

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM