【问题标题】:AVAudioPlayer is nil after i alloc init我分配初始化后 AVAudioPlayer 为零
【发布时间】:2015-01-05 07:26:45
【问题描述】:

我在我的项目中被困在 AVAudioPlayer。情况是, 在 CollectionViewController 的“didSelectItemAtIndexPath”方法中,我像下面这样分配和初始化 AVAudioPlayer,

dict=[plist objectAtIndex:indexPath.item];

    NSURL* urlPath=[NSURL new];
    urlPath=[NSURL URLWithString:[dict objectForKey:@"FilePath"]];
     NSError *error = nil;
    self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:urlPath error:&error];
    NSLog(@"Error[%@]",[error localizedDescription]);
    [self.player setDelegate:self];
    [self.player prepareToPlay];

但在此之后,它仍然是空的.. 首先,我尝试不让玩家拥有财产。但没有运气.... 所以我尝试使用this

【问题讨论】:

  • 你有没有仔细检查 urlPath 不为零?
  • @WorldOfWarcraft "urlPath" 不为零..
  • 你把你的代码放在didSelectItemAtIndexPath,这意味着播放器只有在你选择的时候才会启动。
  • 在此处发布您的代码。

标签: objective-c iphone ios8 xcode6 avaudioplayer


【解决方案1】:

这样试试

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  {


    NSLog(@" selected data ==%@",[yoururlarray objectAtIndex: indexPath.row]);

 urlPath=[yoururlarray objectAtIndex: indexPath.row]);

    self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:urlPath error:nil];
    [self.player setDelegate:self];
    [self.player prepareToPlay];


    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 2011-07-21
    相关资源
    最近更新 更多