我正在使用AVPlayer播放位于文档目录中的MP3(确认文件在那里) - 我加载AVPlayerItem
等待AVPlayerItemStatusReadyToPlay
然后实例化{{1}与项目和游戏。
AVPlayer
确实被调用,但实际上没有音频播放,任何人都知道为什么?
AVPlayerItemStatusReadyToPlay
答案 0 :(得分:2)
这样做:
- (void)setupAudio
{
if([NSFileManager defaultManager] fileExistsAtPath:[self.urlForEightBarAudioFile absoluteString]])
{
AVAsset *eightBarAsset = [AVAsset assetWithURL:self.urlForEightBarAudioFile];
self.eightBarsPlayerItem = [[AVPlayerItem alloc] initWithAsset:eightBarAsset];
self.eightBarsPlayer = [AVPlayer playerWithPlayerItem:self.eightBarsPlayerItem]; //forgot this line
[self.eightBarsPlayer addObserver:self forKeyPath:@"status" options:0 context:nil]; // add observer for player not item
}
}
答案 1 :(得分:0)
in swift
func setupAudio() {
if NSFileManager.defaultManager().fileExistsAtPath(self.urlForEightBarAudioFile.absoluteString) {
self.eightBarsPlayerItem = AVPlayerItem(asset: AVAsset(URL: self.urlForEightBarAudioFile))
self.eightBarsPlayer = AVPlayer(playerItem: self.eightBarsPlayerItem)
}
}
答案 2 :(得分:0)
- (void)setupAudio
{
dispatch_async(dispatch_get_main_queue(), ^{
AVAsset *eightBarAsset = [AVAsset assetWithURL:self.urlForEightBarAudioFile];
self.eightBarsPlayerItem = [[AVPlayerItem alloc] initWithAsset:eightBarAsset];
self.eightBarsPlayer = [AVPlayer playerWithPlayerItem:self.eightBarsPlayerItem]; //forgot this line
[self.eightBarsPlayer addObserver:self forKeyPath:@"status" options:0 context:nil]; // add observer for player not item
});
}