简体   繁体   English

avplayer无法在iOS中播放音频

[英]avplayer not playing audio with in IOS

i am trying to play a audiio with below code but its not playing. 我正在尝试使用以下代码播放听众,但没有播放。 Any kind of help would be really appreciated. 任何形式的帮助将非常感激。

    NSError *sessionError = nil;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&sessionError];
     [[AVAudioSession sharedInstance] setActive: YES error: nil];
     AVPlayerItem *item = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:audioUrl]];
    [self setPlayer:[[AVPlayer alloc] initWithPlayerItem:item]];
    [[self player] setActionAtItemEnd:AVPlayerActionAtItemEndNone];

    [[self player] play];

If you're using ARC, you'll need to retain the AVAudioPlayer as it sets to nil automatically, which would be a likely reason for no sound. 如果使用的是ARC,则需要保留AVAudioPlayer,因为它会自动设置为nil,这可能是没有声音的原因。 In your header file if you could make sure your self.player's property is set up like this,, 如果可以确保在头文件中这样设置self.player的属性,

@property (strong, nonatomic) AVPlayer *player;

I hope this helps 我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM