1

我正在尝试使用 AVPlayer 播放 mp3(相机快门声音)。我已将所有内容简化为一个简单的按钮,按下该按钮即可播放声音。所有对象均已正确创建,但是当我在 iPhone 或 iPad(均运行 iOS 6)上运行应用程序时,不会产生声音。不,静音按钮未打开,音量已调高。:) 我可以在两台设备上的其他应用程序中听到声音。

我在做傻事吗?

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

2 回答 2

10

'player' 在方法结束时被释放。它必须保留为类变量。

于 2012-10-14T22:31:09.157 回答
0

尝试这些行并将“yoursound.mp3”替换为您的文件名。:

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];
于 2012-10-14T22:26:11.953 回答