[英]AVAudioPlayer mp3 file not playing
我是一名业余开发人员,正在开发一个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)revmob
{
[[RevMobAds session] showFullscreen];
}
- (IBAction)playButton:(id)sender
{
[_avPlayer play];
NSLog(@"Sound playing");
}
- (IBAction)stopButton:(id)sender
{
[_avPlayer pause];
NSLog(@"Sound Stopped");
}
@end
在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出口
中间标签->播放时间出口
左键-> IBAction播放或暂停声音
右键-> 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;
}
我已经在Mac上测试了您的代码,它似乎可以正常工作。 可能是造成问题的原因是,虽然mp3在Xcode中可见,但在编译过程中并未将其添加到捆绑软件中。 喜欢这个问题 。
要修复此问题,请单击Xcode中的mp3,查看“文件检查器”,然后选中目标旁边的“目标成员身份”框(在屏幕截图中,我的屏幕截图是SOTest)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.