IOS-在uiwebview媒体播放器中询问用户播放的歌曲

时间:2014-09-19 08:12:50

标签: ios uiwebview media-player

如果我点击UIWebView中的mp3网址,它会弹出媒体播放器自动播放mp3。 但在这里,我需要的是让用户确认在UIWebview媒体播放器中播放歌曲。是否可以访问UIWebview媒体播放器?如果是这样的话? 请帮帮我

1 个答案:

答案 0 :(得分:3)

是的,可以询问用户确认。

用户UIWebView委托方法:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
self.URL = [request URL];
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
    AVURLAsset* asset;

    self.songURL = [NSString stringWithFormat:@"%@",self.URL];

    //Check for supported song formatted 
    if([self.songURL hasSuffix:@".mp3"] ||
       [self.songURL hasSuffix:@".wav"] ||
       [self.songURL hasSuffix:@".aiff"] ||
       [self.songURL hasSuffix:@".aac"] ||
       [self.songURL hasSuffix:@".m4a"] ||
       [self.songURL hasSuffix:@".m4p"] ||
       [self.songURL hasSuffix:@".caf"])
    {

        asset = [[AVURLAsset alloc]initWithURL:self.URL options:nil];

        if(asset != nil)
        {
            UIActionSheet *actSheet = [[UIActionSheet alloc]
                                       initWithTitle:@"Songs"
                                       delegate:self
                                       cancelButtonTitle:@"Cancel"
                                       destructiveButtonTitle:@"Download"
                                       otherButtonTitles:@"Listen Online", nil];
            [actSheet showInView:self.view];
        }
        [asset release];
        return NO;

    }
}
return YES;
}

在UIActionSheetDelegate方法上:

-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if(buttonIndex == 1)
  {
     [self.webView loadRequest:[NSURLRequest requestWithURL:self.URL]];
 }
}