ios - AVPlayer 不播放任何内容

标签 ios objective-c audio avplayer nsbundle

我正在做一些非常简单的事情,只是播放一段音频。 但它不播放任何内容。不在模拟器或设备上。

音频文件是捆绑的,预览可以播放音频,我也试过不同的音频没有播放。

可能是什么问题?我是否必须为 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];
}

enter image description here

我也试过,但没有成功

//    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/

相关文章:

ios - NSURLSession 任务取消问题(CFNetwork 内部错误)

javascript - 跨浏览器兼容音频有哪些选项?

java - InputStream音频混合(MODE_STREAM)

ios - 用特定类型覆盖 UIViewController.view

objective-c - 从2个或更多MKPolygon对象的交集创建MKPolygon

ios - tableView 和普通 View (相同的数据对象)

objective-c - NSTableView 不显示 NSMutableArray 的内容

iphone - 无法识别触摸导航栏的 ImageView 。?

ios - 重新签署 xcarchive 文件

java - android制作一个音乐应用程序访问sd卡以获取音乐数据帮助/信息