首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在我的ios应用程序中播放远程.mp3文件?

如何在我的ios应用程序中播放远程.mp3文件?
EN

Stack Overflow用户
提问于 2013-07-10 18:59:06
回答 2查看 12.8K关注 0票数 4

我正在尝试玩远程.mp3 file

但是它给出了以下错误。

代码语言:javascript
复制
The operation couldn’t be completed. (OSStatus error -43.)

下面是我的代码:

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];

    isPlaying = NO;

/*   

 NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]


pathForResource:@"Dar-e-Nabi-Per" ofType:@"mp3"]];

*/

    NSURL *url = [NSURL 

URLWithString:@"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];

    NSError *error;

    audioPlayer = [[AVAudioPlayer alloc]

                   initWithContentsOfURL:url

                   error:&error];

    if (error)

    {

        NSLog(@"Error in audioPlayer: %@",


              [error localizedDescription]);

 }

 else

 {

   audioPlayer.delegate = self;


   [audioPlayer prepareToPlay];


    }


}

有谁能指点我哪里做错了吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-10 19:41:03

要从远程服务器传输音频,请使用AVPlayer而不是AVAudioPLayer。AVPlayer Documentation

示例代码:

代码语言:javascript
复制
- (void)playSampleSong:(NSString *)iSongName {
    NSString *aSongURL = [NSString stringWithFormat:@"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
    // NSLog(@"Song URL : %@", aSongURL);

    AVPlayerItem *aPlayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:aSongURL]];
    AVPlayer *anAudioStreamer = [[AVPlayer alloc] initWithPlayerItem:aPlayerItem];
    [anAudioStreamer play];

    // Access Current Time
    NSTimeInterval aCurrentTime = CMTimeGetSeconds(anAudioStreamer.currentTime);

    // Access Duration
    NSTimeInterval aDuration = CMTimeGetSeconds(anAudioStreamer.currentItem.asset.duration);
}
票数 15
EN

Stack Overflow用户

发布于 2013-07-10 19:39:59

使用此选项从URL播放歌曲

代码语言:javascript
复制
NSData *songData=[NSData dataWithContentsOfURL:url];

self.player = [[AVAudioPlayer alloc] initWithData:songData error:nil];
    self.player.numberOfLoops=0;
    _player.delegate=self;
    [_player prepareToPlay];
[_player play]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17568917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档