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

标签 ios avaudioplayer

我正在尝试播放远程 .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];


    }


}

谁能指导我哪里做错了

最佳答案

要从远程服务器传输音频,请使用 AVPlayer 而不是 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 - 如何在我的 iOS 应用程序中播放远程 .mp3 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17568917/

相关文章:

Objective-C Facebook API

ios - 使用 AVAudioPlayer 播放多个音频文件

iphone - AVAudioPlayer使用audioPlayerDecodeErrorDidOccur和NSOSStatusErrorDomain -50中断播放

objective-c - audioPlayer 在 IPhone 上播放的声音与 IPAD 不同(Iphone 音量小,必须靠近耳朵)

ios - 如何将触摸从 UIView 传递到 CCLayer(ccTouchesBegin 等)

ios - 触摸 MKMapView 会导致引脚轻微移动

ios - Xcode:如何制作 launchScreen Storyboard

ios - AVAudioPlayer 在 iOS 设备上运行,在模拟器中崩溃。我应该担心吗?

ios - 如何在单击下一个按钮时播放下一首歌曲

ios - 快速检测音量变化