2012-10-14 62 views
1

我試圖播放使用AVPlayer的mp3(相機快門聲音)。我已經把所有東西都剝離成了一個簡單的按鈕,當按下時,播放聲音。所有對象都正確創建,但是當我在iPhone或iPad上運行應用程序(都運行iOS 6)時,不會生成任何聲音。不,靜音按鈕不亮,音量調大。 :)我可以在兩臺設備上的其他應用程序中聽到聲音。沒有聲音來自AVPlayer

我在做一些愚蠢的事情嗎?

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

回答

10

「玩家」發行方法的最後替換「yoursound.mp3」。 它必須保留爲類變量。

+0

這是正確的答案:玩家應該是一個ivar或類中的財產 –

+0

太棒了!讓我頭痛。 – GoldenJoe

0

嘗試這些線,並與你的文件名:

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