0
我正在使用iframe和'Widget API'來控制流式傳輸。 當我打電話'播放();或'暫停();'播放按鈕正在改變,但不播放音頻。Soundcloud iframe不能與UIWebView一起使用swift
@IBOutlet weak var videoWebView: UIWebView!
var soundCloudIsLoaded : Bool = false
var isPlaying : Bool = false
override func viewDidLoad() {
super.viewDidLoad()
self.videoWebView.isUserInteractionEnabled = true
self.videoWebView.allowsInlineMediaPlayback = true
var htmlString = "<iframe id=\"sc-widget\" width=\"100%\" height=\"350\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F1848538&color=ff5500&auto_play=false&hide_related=true&show_comments=false&show_user=false&show_reposts=false&buying=false&liking=false&download=false&sharing=false&show_artwork=false&show_playcount=false\" playsinline></iframe><script src=\"https://w.soundcloud.com/player/api.js\" type=\"text/javascript\"></script><script type=\"text/javascript\">var duration = 0;var position = 0;var isLoaded = false;var isPaused = true;var widgetIframe = document.getElementById('sc-widget'), widget = SC.Widget(widgetIframe), newSoundUrl = '{{url}}';widget.load(newSoundUrl, { show_artwork: false, hide_related : true, show_comments : false, show_user : false, show_reposts : false, buying : false, liking : false, download : false, sharing : false, show_playcount : false});widget.bind(SC.Widget.Events.READY, function(){ isLoaded = true; widget.getDuration(function(dur){ duration = parseInt(dur); }); setInterval(function() { widget.getDuration(function(dur){ duration = parseInt(dur); }); widget.getPosition(function(pos){ position = parseInt(pos); }); }, 500);});widget.bind(SC.Widget.Events.PLAY, function(){ isPaused = false;});widget.bind(SC.Widget.Events.PAUSE, function(){ isPaused = true;});function Play() { widget.play();}function Pause() { widget.pause();}function Toggle() { widget.toggle();}function SeekTo(milliseconds) { widget.seekTo(milliseconds); widget.getPosition(function(pos){ position = parseInt(pos); }); }function getDuration() { return duration;}function getPosition() { return position;}function soundCloudIsLoaded(){ return isLoaded;}function IsPaused(){ return isPaused;}</script>"
htmlString = htmlString.replacingOccurrences(of: "{{url}}", with: "URL")
self.videoWebView.loadHTMLString(htmlString, baseURL: nil)
self.videoWebView.delegate = self
}
@IBAction func playButtonAction(_ sender: Any) {
if self.soundCloudIsLoaded {
self.isPlaying = !self.isPlaying
if self.isPlaying {
let _ = self.videoWebView.stringByEvaluatingJavaScript(from: "Play();")
self.playButton.setTitle("Pause", for: .normal)
} else {
let _ = self.videoWebView.stringByEvaluatingJavaScript(from: "Pause();")
self.playButton.setTitle("Play", for: .normal)
}
}
}
func webViewDidFinishLoad(_ webView: UIWebView) {
self.soundCloudIsLoaded = true
}
在htmlString我有這樣的代碼:
<iframe id="sc-widget" width="100%" height="350" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F1848538&color=ff5500&auto_play=false&hide_related=true&show_comments=false&show_user=false&show_reposts=false&buying=false&liking=false&download=false&sharing=false&show_artwork=false&show_playcount=false"></iframe>
<script src="https://w.soundcloud.com/player/api.js" type="text/javascript"></script>
<script type="text/javascript">
var duration = 0;
var position = 0;
var isLoaded = false;
var isPaused = true;
var widgetIframe = document.getElementById('sc-widget'),
widget = SC.Widget(widgetIframe),
newSoundUrl = '{{url}}';
widget.load(newSoundUrl, {
show_artwork: false,
auto_play : false,
hide_related : true,
show_comments : false,
show_user : false,
show_reposts : false,
buying : false,
liking : false,
download : false,
sharing : false,
show_playcount : false
});
widget.bind(SC.Widget.Events.READY, function(){
isLoaded = true;
widget.getDuration(function(dur){
duration = parseInt(dur);
});
setInterval(function() {
widget.getPosition(function(pos){
position = parseInt(pos);
});
widget.getDuration(function(dur){
duration = parseInt(dur);
});
}, 500);
});
widget.bind(SC.Widget.Events.PLAY, function(){
isPaused = false;
});
widget.bind(SC.Widget.Events.PAUSE, function(){
isPaused = true;
});
function Play() {
widget.play();
}
function Pause() {
widget.pause();
}
function Toggle() {
widget.toggle();
}
function SeekTo(milliseconds) {
widget.seekTo(milliseconds);
widget.getPosition(function(pos){
position = parseInt(pos);
});
}
function getDuration() {
return duration;
}
function getPosition() {
return position;
}
function soundCloudIsLoaded(){
return isLoaded;
}
function IsPaused(){
return isPaused;
}
</script>
此代碼用來做工精細,但它停在IOS webView的突然工作。這在桌面瀏覽器上正常工作。
謝謝了很多,這對我的作品。 :d –