AVAudioPlayer没有播放mp3文件

时间:2013-09-10 00:02:17

标签: ios objective-c macos mp3 avaudioplayer

我是一名业余开发人员制作ios应用程序,其中包括在按钮留下深刻印象时播放的声音文件(mp3)。但是,声音没有播放(在移动设备上测试,而不是ios模拟器)。我已经在捆绑资源中有声音文件,并添加了AVFoundation以将二进制文件链接到库。我有xcode 4.6并且有mac osx 10.8.4。这是我的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)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

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

2 个答案:

答案 0 :(得分:4)

在ViewController.h中导入AVFoundation头文件:     #import

转到ViewController.m并在界面部分添加以下outlet属性和IBAction方法定义:

@interface ViewController () 

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

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

@end

返回ViewController.xib并建立以下连接:

顶部标签 - &gt; trackTitle outlet

中间标签 - &gt; playTime outlet

左键 - &gt; IBAction playOrPauseSound

右键 - &gt; IBAction stopSound

现在建立了连接,我们可以在ViewController.m中完成我们的代码。在界面部分添加以下属性

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

enter image description here

转到ViewController.m并在接口部分添加以下outlet属性和IBAction方法定义。

@interface ViewController () 

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

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

@end

更改viewDidLoad方法:

- (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; 
}

答案 1 :(得分:0)

我已经在Mac上测试了你的代码,它似乎运行得很好。问题的原因可能是你的mp3在Xcode中可见,但在编译期间它没有被添加到包中。 Like in this question

要修复它,请单击Xco​​de中的mp3,查看文件检查器并检查目标旁边的目标成员资格框(在屏幕截图中是SOTest)。

Check Target Membership