我正在做一些非常简单的事情,只是播放一段音频。 但它不播放任何内容。不在模拟器或设备上。
音频文件是捆绑的,预览可以播放音频,我也试过不同的音频没有播放。
可能是什么问题?我是否必须为 avplayer 添加委托(delegate)方法?(如果是的话那对我来说没有意义)
我也尝试过不使用 avplayer 项目和不同的 nsurl 方法......
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"JohnsPizza" ofType:@"aiff"];
//NSURL *assetURL = [[NSBundle mainBundle] URLForResource:@"JohnsPizza" withExtension:@"aiff"];
NSURL *url = [NSURL fileURLWithPath:path];
AVPlayer *player = [[AVPlayer alloc] init];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
player = [AVPlayer playerWithPlayerItem:playerItem];
[player play];
}
我也试过,但没有成功
// SystemSoundID sound1;
// AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &sound1);
// AudioServicesPlaySystemSound(sound1);
// AudioServicesRemoveSystemSoundCompletion(sound1);
// AudioServicesDisposeSystemSoundID(sound1);
最佳答案
你必须保留 AVPlayer
对象,因为它可能会自动设置为 nil
@property (strong, nonatomic) AVPlayer *player;
关于ios - AVPlayer 不播放任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29308886/