本文介绍了如何在我的ios应用程序中播放远程.mp3文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试播放远程 .mp3文件

但是它出现以下错误。

The operation couldn’t be completed. (OSStatus error -43.)

这是我的代码:

- (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];


    }


}

任何人都可以指导我在哪里做错了

Can anyone please guide me where i'm doing mistake

推荐答案

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

For steaming audio from a remote server, use AVPlayer instead of AVAudioPLayer.AVPlayer Documentation

示例代码:

- (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);
}

这篇关于如何在我的ios应用程序中播放远程.mp3文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 07:44