AVPlayer 和本地文件 [英] AVPlayer And Local Files

查看:13
本文介绍了AVPlayer 和本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个适用于 iOS 的 MP3 播放器,用于播放托管在网络上的音频文件.我想提供离线播放文件的功能,所以我使用 ASIHTTP 下载文件,但我似乎无法在应用程序文档目录中找到有关使用 mp3 初始化 AVPlayer 的信息.以前有人这样做过吗?甚至有可能吗?

I am building a MP3 player for iOS that plays audio files hosted on the web. I want to offer the ability to play the files offline so I have the files downloading using ASIHTTP but I am cannot seem to find info on initiallzing AVPlayer with a mp3 in the app documents directory. Has anyone done this before? Is it even possible?

*我在下面发布了一个答案,展示了如何将 iOS AvPlayer 用于本地和 http 文件.希望这会有所帮助!

*I posted an answer below that shows how to use the iOS AvPlayer for both local and http files. Hope this helps!

推荐答案

我决定回答我自己的问题,因为我觉得关于如何使用 Apple 提供的 AVPlayer 本地和流媒体(通过 http)的文档很少文件.为了帮助理解解决方案,我在 GitHub 上用 Objective-C 编写了一个 示例项目Swift 下面的代码是 Objective-C,但你可以下载我的 Swift 示例来查看.非常相似!

I decided to answer my own question because I felt like there is very little documentation on how to use the Apple provided AVPlayer for both local and stream (over http) files. To help understand the solution, I put together a sample project on GitHub in Objective-C and Swift The code below is Objective-C but you can download my Swift example to see that. It is very similar!

我发现两种设置文件的方法几乎相同,除了您如何为 Asset > PlayerItem > AVPlayer 链实例化 NSURL.

What I found is that the two ways of setting up the files are almost identical except for how you instantiate your NSURL for the Asset > PlayerItem > AVPlayer chain.

这里是核心方法的概述

.h 文件(部分代码)

.h file (partial code)

-(IBAction) BtnGoClick:(id)sender;
-(IBAction) BtnGoLocalClick:(id)sender;
-(IBAction) BtnPlay:(id)sender;
-(IBAction) BtnPause:(id)sender;
-(void) setupAVPlayerForURL: (NSURL*) url;

.m 文件(部分代码)

.m file (partial code)

-(IBAction) BtnGoClick:(id)sender {

    NSURL *url = [[NSURL alloc] initWithString:@""];

    [self setupAVPlayerForURL:url];
}

-(IBAction) BtnGoLocalClick:(id)sender {

    // - - - Pull media from documents folder

    //NSString* saveFileName = @"MyAudio.mp3";
    //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //NSString *documentsDirectory = [paths objectAtIndex:0];
    //NSString *path = [documentsDirectory stringByAppendingPathComponent:saveFileName];

    // - - -

    // - - - Pull media from resources folder

    NSString *path = [[NSBundle mainBundle] pathForResource:@"MyAudio" ofType:@"mp3"];

    // - - -

    NSURL *url = [[NSURL alloc] initFileURLWithPath: path];

    [self setupAVPlayerForURL:url];
}

-(void) setupAVPlayerForURL: (NSURL*) url {
    AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
    AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];

    player = [AVPlayer playerWithPlayerItem:anItem];
    [player addObserver:self forKeyPath:@"status" options:0 context:nil];
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    if (object == player && [keyPath isEqualToString:@"status"]) {
        if (player.status == AVPlayerStatusFailed) {
            NSLog(@"AVPlayer Failed");
        } else if (player.status == AVPlayerStatusReadyToPlay) {
            NSLog(@"AVPlayer Ready to Play");
        } else if (player.status == AVPlayerItemStatusUnknown) {
            NSLog(@"AVPlayer Unknown");
        }
    }
}

-(IBAction) BtnPlay:(id)sender {
    [player play];
}

-(IBAction) BtnPause:(id)sender {
    [player pause];
}

查看 Objective-C 源代码,了解有关此操作的示例.希望这会有所帮助!

Check out the Objective-C source code for a working example of this. Hope this helps!

-更新 12/7/2015 我现在有一个 Swift 源代码示例,您可以在此处查看.

-Update 12/7/2015 I now have a Swift example of the source code you can view here.

这篇关于AVPlayer 和本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆