使用iPhone应用程序从URL下载并播放mp3文件 [英] Download and play mp3 file from url with iphone application

查看:115
本文介绍了使用iPhone应用程序从URL下载并播放mp3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想创建一个下载mp3文件然后开始播放的应用程序.

We want to create an application that downloads a mp3 file and then starts to play it.

让任何人知道如何从URL下载mp3文件,然后将其存储在iPhone或应用程序中(取决于可能的方式).

Has anyone an idea of how to download the mp3 file from an url and then store it, either on the iphone or in the application (depending on what is possible).

推荐答案

将以下导入语句添加到标题中:

Add the following import statement to your header:

#import <AVFoundation/AVFoundation.h>

在您的实现中的某个地方:

In your implementation somewhere:

NSError *error;
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://xyz/abc.mp3"]];

audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = -1;
audioPlayer.delegate = self;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];

if (audioPlayer == nil)
    NSLog(@"%@", [error description]);
else
    [audioPlayer play];

别忘了在某个时间点release播放器:

Don't forget to release the player at some point:

[audioPlayer release];

这篇关于使用iPhone应用程序从URL下载并播放mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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