Ios AVAudioPlayer不播放我的mp3
我正在尝试在我的应用程序中播放一个简短的mp3音频文件。以下是我正在使用的代码:Ios AVAudioPlayer不播放我的mp3,ios,audio,avaudioplayer,Ios,Audio,Avaudioplayer,我正在尝试在我的应用程序中播放一个简短的mp3音频文件。以下是我正在使用的代码: AVAudioPlayer *AudioPlayer; NSError *error; NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"filename"
AVAudioPlayer *AudioPlayer;
NSError *error;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"filename"
ofType:@"mp3"]];
AudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
AudioPlayer.delegate = self;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
if (error)
{
NSLog(@"Error: %@",
[error localizedDescription]);
}
else
{
[AudioPlayer play];
}
我真的不知道我遗漏了什么,我遵循的例子似乎与我正在做的相符
编辑:我还应该提到,代码运行时没有错误,这方面有一个尝试捕获。我自己也有这个问题,直到我发现声音没有通过扬声器,而是被重定向到calls扬声器。尝试添加此选项以重定向扬声器中的声音:
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
sizeof (doChangeDefaultRoute),
&doChangeDefaultRoute);
我自己也有这个问题,直到我发现声音不是通过扬声器发出,而是被重定向到呼叫扬声器。尝试添加此选项以重定向扬声器中的声音:
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
sizeof (doChangeDefaultRoute),
&doChangeDefaultRoute);
我做了一些补充,并使其工作:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface FirstViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *data;
UIButton *playButton_;
}
@property(nonatomic, retain) IBOutlet UIButton *playButton;
-(IBAction)musicPlayButtonClicked:(id)sender;
@end
#import "ViewController.h"
@implementation FirstViewController
@synthesize playButton=playButton_;
- (IBAction)musicPlayButtonClicked:(id)sender {
NSString *name = [[NSString alloc] initWithFormat:@"09 No Money"];
NSString *source = [[NSBundle mainBundle] pathForResource:name ofType:@"mp3"];
if (data) {
[data stop];
data = nil;
}
data=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: source] error:NULL];
data.delegate = self;
[data play];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在Interface Builder中,我添加了一个按钮,并将其与IBOutlet和musicPlayButtonClicked iAction连接
别忘了将AVFoundation框架添加到您的项目中
ps
我真的为我的英语感到抱歉,请宽容一点。我做了一些补充,并使之生效:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface FirstViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *data;
UIButton *playButton_;
}
@property(nonatomic, retain) IBOutlet UIButton *playButton;
-(IBAction)musicPlayButtonClicked:(id)sender;
@end
#import "ViewController.h"
@implementation FirstViewController
@synthesize playButton=playButton_;
- (IBAction)musicPlayButtonClicked:(id)sender {
NSString *name = [[NSString alloc] initWithFormat:@"09 No Money"];
NSString *source = [[NSBundle mainBundle] pathForResource:name ofType:@"mp3"];
if (data) {
[data stop];
data = nil;
}
data=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: source] error:NULL];
data.delegate = self;
[data play];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在Interface Builder中,我添加了一个按钮,并将其与IBOutlet和musicPlayButtonClicked iAction连接
别忘了将AVFoundation框架添加到您的项目中
ps
我真的为我的英语感到抱歉,请原谅。由于ARC,我也有类似的问题。与使用相同的方法定义AVAudioPlayer不同,您应该在其他地方有一个实例变量,例如UIViewController。这样,ARC不会自动释放此对象,音频也可以播放。由于ARC,我遇到了类似的问题。与使用相同的方法定义AVAudioPlayer不同,您应该在其他地方有一个实例变量,例如UIViewController。这样ARC不会自动释放此对象,音频可以播放。非常感谢您的提示。你挽救了这一天。和平:非常感谢这个提示。你挽救了这一天。和平: