0

我是一名业余开发人员,正在制作一个 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

视图控制器.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
4

2 回答 2

4

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

转到 ViewController.m 并在接口部分添加以下插座属性和 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 并进行以下连接:

顶部标签-> trackTitle 出口

中间标签->playedTime 出口

左键-> IBAction playOrPauseSound

右键-> IBAction stopSound

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

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

在此处输入图像描述

转到 ViewController.m 并在接口部分添加以下插座属性和 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; 
}
于 2014-09-19T13:00:43.440 回答
0

我已经在我的 Mac 上测试了你的代码,它似乎工作得很好。可能是您的问题的原因是,虽然您的 mp3 在 Xcode 中可见,但在编译期间它没有被添加到包中。就像在这个问题中一样

要修复它,请单击 Xcode 中的 mp3,查看 File Inspector 并检查目标旁边的 Target Membership 框(在屏幕截图中是 SOTest)。

检查目标会员

于 2013-09-11T13:16:47.447 回答