No sound coming from AVPlayer

前端 未结 2 1739
星月不相逢
星月不相逢 2021-01-07 10:59

I\'m trying to play an mp3 (camera shutter sound) using AVPlayer. I\'ve stripped everything down to a simple button that, when pressed, plays the sound. All of the objects a

相关标签:
2条回答
  • 2021-01-07 11:28

    Try these lines and replace "yoursound.mp3" with your file name.:

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/yoursound.mp3", [[NSBundle mainBundle] resourcePath]]];
    AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
    AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
    avplayer = [AVPlayer playerWithPlayerItem:anItem];
    [avplayer play];
    
    0 讨论(0)
  • 2021-01-07 11:32

    'player' is released the end of method. It must be reserved as a class variable.

    0 讨论(0)
提交回复
热议问题