AVAudioPlayer mp3 文件无法播放
Posted
技术标签:
【中文标题】AVAudioPlayer mp3 文件无法播放【英文标题】:AVAudioPlayer mp3 file not playing 【发布时间】:2013-09-10 00:02:16 【问题描述】:我是一名业余开发人员,正在制作一个包含在按下按钮时播放的声音文件 (mp3) 的 ios 应用程序。但是,声音没有播放(在移动设备上测试,而不是 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
【问题讨论】:
【参考方案1】:在 ViewController.h 中导入 AVFoundation 头文件: #导入
转到 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;
【讨论】:
【参考方案2】:我已经在我的 Mac 上测试了您的代码,它似乎运行良好。可能是您的问题的原因是,虽然您的 mp3 在 Xcode 中可见,但在编译期间它没有被添加到包中。 Like in this question.
要修复它,请在 Xcode 中单击 mp3,查看 File Inspector 并选中目标旁边的 Target Membership 框(在屏幕截图中是 SOTest)。
【讨论】:
我也已经把mp3文件加到bundle里了,还是没有声音 您是否尝试过查看代码之外的内容,将 mp3 替换为另一个,甚至增加音量? 我把音量调到最大还是没有声音。但是,我使用了另一个剪辑(220 秒)并且它起作用了 我刚刚发现原因是文件扩展名是.wav,o我改变了它以上是关于AVAudioPlayer mp3 文件无法播放的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 AVAudioPlayer 播放存储在文档目录中的本地 mp3 文件
来自服务器的 MP3 文件在 iOS7 中无法使用 AVAudioPlayer 播放