【发布时间】:2015-03-27 19:38:05
【问题描述】:
我正在做一些非常简单的事情,只需播放音频。 但它不播放任何东西。不在模拟器或设备上。
音频文件在捆绑中,预览可以播放音频,我也试过不同的音频没有播放。
可能是什么问题?,我是否必须为 avplayer 添加委托方法?(如果这样对我来说没有意义)
我也尝试过不使用 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);
【问题讨论】:
-
为什么不使用 AVAudioPlayer 而不是 AVPlayer?
-
文件最终将是远程的。加上它是 3 行代码,它应该可以工作......
-
只是为了回答原因,因为它没有被保留
标签: ios objective-c audio avplayer nsbundle