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