4

リモートでプレイしようとしています.mp3 file

しかし、次のエラーが発生します。

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


    }


}

誰でも私が間違っているところを教えてもらえますか

4

2 に答える 2

15

リモート サーバーからオーディオをストリーミングするには、AVAudioPLayer の代わりに AVPlayer を使用します。 AVPlayer ドキュメント

サンプルコード:

- (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);
}
于 2013-07-10T11:41:02.533 に答える
1

これを使用して 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]
于 2013-07-10T11:39:58.607 に答える