【问题标题】:Audio file not play from server音频文件无法从服务器播放
【发布时间】:2013-03-19 09:25:56
【问题描述】:

我正在播放来自服务器的音频文件,但如果我播放本地文件则它不播放,那么它在这里播放是我的代码。

    //NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];

NSURL *url = [NSURL URLWithString:@"http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3"];


NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;

if (audioPlayer == nil)
    NSLog([error description]);             
else 
    [audioPlayer play];

【问题讨论】:

    标签: iphone objective-c ipad avaudioplayer


    【解决方案1】:

    尝试使用MPMoviePlayerController 而不是AVAudioPlayer

    NSURL *url = [NSURL URLWithString:@"http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3"];
    
    
    mv = [[MPMoviePlayerController alloc] initWithContentURL:url];
    mv.movieSourceType = MPMovieSourceTypeUnknown;
    [self.view addSubview:mv.view];
    [mv play];
    

    【讨论】:

      【解决方案2】:

      使用此代码解决您的问题...

      NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3]];
      NSError *error;
      
      audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
      audioPlayer.numberOfLoops = 0;
      audioPlayer.volume = 1.0f;
      [audioPlayer prepareToPlay];
      
      if (audioPlayer == nil)
          NSLog([error description]); 
      else
          [audioPlayer play];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-30
        • 2015-06-18
        • 1970-01-01
        • 1970-01-01
        • 2014-01-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多