2011-05-09 57 views
8

我加載MP3文件到iPad上的應用UIWebView控制,我有一個名爲UIButton預計駁回UIWebView和停止音樂的用戶,下面的代碼片段:如何阻止UIWebView播放MP3?

// points to the mp3 file path 
NSURL *url = [NSURL fileURLWithPath:self.sFilePath]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
[web loadRequest:request]; 

當用戶點擊完成我做到以下幾點:

[self.webview removeFromSuperview]; 
[self.webview release]; 

但是,這並不停止播放音樂,我注意到,在UIWebView打開QuickTime播放器加載MP3文件是心病直接的方式?

我非常感謝任何指導或幫助。

+0

的可能重複[視頻/音頻流即使UIWebView的關閉不會停止 - iPad的(HTTP://計算器。 com/questions/2719270/video-audio-streaming-does-not-stop-even-if-uiwebview-is-closed-ipad) – nielsbot 2012-08-17 17:41:58

回答

1

我建議你自己演奏聲音,例如AVAudioPlayer(可能是在iOS中播放和控制聲音的最簡單方法)。

事情是這樣的:

NSURL *url = [NSURL fileURLWithPath:self.sFilePath]; 
NSData *mySound = [NSData dataWithContentsOfURL:url]; 
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:mySound error:NULL]; 
audioPlayer.numberOfLoops = 0; 
[audioPlayer play]; 

然後,您可以停止聲音就這麼簡單:

[audioPlayer stop]; 

[audioPlayer pause]; 

不要忘了包括AVFoundation.framework在你的項目中。

+0

@iceydee:我會盡力並儘快回覆你。 – 2011-05-09 12:59:44

+0

@iceydee:謝謝你的回答,這不是我所期望的。我會等一會兒,看看有沒有人可以幫我解決'UIWebView'問題,如果沒有人會接受你的答案並使用你的解決方案,謝謝。 – 2011-05-09 13:53:59

+0

你真的想讓播放/暫停控件顯示給用戶嗎? – iceydee 2011-05-09 14:07:02