[英]AVAudioPlayer: MP3 file not playing on device, but works in Simulator (iOS 6)
我正在使用下面的代碼在iOS 6上播放帶有AVAudioPlayer的MP3文件。該文件似乎正在播放,但設備上沒有聲音輸出。 它在iOS模擬器中工作正常。
檔案.h:
#import "UIKit/UIKit.h"
#import "AVFoundation/AVFoundation.h"
@interface ViewController : UIViewController <AVAudioPlayerDelegate>
@property (strong, nonatomic) AVAudioPlayer *player;
@end
檔案.m:
#import "ViewController.h"
@implementation ViewController
@synthesize player;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"beep_7" ofType:@"mp3"];
NSLog(@"Audio path: %@", soundFilePath);
NSError *error;
player =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFilePath] error:&error];
if (error) {
NSLog(@"Error in audioPlayer: %@",[error localizedDescription]);
}
else {
[player setDelegate:self];
[player setNumberOfLoops:3]; //just to make sure it is playing my file several times
player.volume = 1.0f;
if([player prepareToPlay]) //It is always ready to play
NSLog(@"It is ready to play");
else
NSLog(@"It is NOT ready to play ");
if([player play]) //It is always playing
NSLog(@"It should be playing");
else
NSLog(@"An error happened");
}
}
@end
您發布的代碼對我來說很好。
檢查設備的靜音模式是打開還是關閉? 我曾經遇到過和你一樣的問題,在我改變了我的模式之后,一切都解決了!
你有沒有設置AudioSessionCategory?
{
NSError *setCategoryError = nil;
BOOL success = [[AVAudioSession sharedInstance] setCategory:
avaudiosessioncategoryplayandrecorderror: &setCategoryError];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.