No sound coming from AVPlayer

人盡茶涼 提交于 2019-12-09 01:50:51

问题


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 are being created correctly but when I run the app on either my iPhone or iPad (both running iOS 6) no sound is generated. And no, the mute button is not on and the volume is turned up. :) I can hear sound in other apps on both devices.

Am I doing something stupid?

- (void)playCameraShutterSound
{
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"Grab" withExtension:@"mp3" ];
    AVPlayer* player = [AVPlayer playerWithURL:url];
    [player play];
}

回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/12887132/no-sound-coming-from-avplayer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!