how can i play remote .mp3 file in my ios application?

落爺英雄遲暮 提交于 2019-12-02 05:20:32

问题


I'm trying to play remote .mp3 file

but its giving the following error.

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

here is my code :

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


回答1:


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

Sample Code:

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



回答2:


Use this to play song from URL

NSData *songData=[NSData dataWithContentsOfURL:url];

self.player = [[AVAudioPlayer alloc] initWithData:songData error:nil];
    self.player.numberOfLoops=0;
    _player.delegate=self;
    [_player prepareToPlay];
[_player play]


来源:https://stackoverflow.com/questions/17568917/how-can-i-play-remote-mp3-file-in-my-ios-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!