2013-07-10 47 views
2

我試圖播放遙控器.mp3 file如何在我的ios應用程序中播放遠程.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]; 


    } 


} 

任何人都可以請指導我在哪裏,我在做錯誤

+0

錯誤#1:你的代碼不可讀。錯誤#2:你沒有谷歌的錯誤號碼。 – 2013-07-10 11:16:08

+0

@ H2CO3我谷歌它,但不明白,錯誤號碼, 現在你能給我solowtion這是錯誤的我的代碼?我的代碼是可讀的。 –

+0

請請,請不要做這樣的錯誤檢查。它從根本上被打破,並且違背了Cocoa文檔。 **總是**在測試錯誤指針本身之前檢查返回值。 http://blog.bignerdranch.com/360-an-nserror-error/ –

回答

12

播放歌曲蒸來自遠程服務器的音頻,請使用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); 
} 
+0

可以請你解釋一下哪個庫要包含使用該功能的 – wasim

+0

AVFoundation框架。 – Roshit

+0

我試過了,但在Swift中,似乎沒有工作。 –

1

使用此功能可以從網址

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

我測試了這個,是的它的工作原理! 但是,蘋果的文檔聲明dataWithContentsOfURL: 不要使用這種同步方法來請求基於網絡的URL。對於基於網絡的URL,此方法可以在慢速網絡上阻止當前線程幾十秒,從而導致糟糕的用戶體驗,並且在iOS中可能會導致您的應用被終止。所以我認爲dataTaskWithURL是reccomended。 – Eadz

+0

該解決方案是同步的,這是一個壞主意。而且,它只能在模擬器上工作。在實際的設備上,AVAudioPlayer會用此代碼引發錯誤。 – Alexander

相關問題