Loading local sound into UIWebView?

Discussion in 'iPhone/iPad Programming' started by Danneman101, Dec 8, 2008.

  1. macrumors 6502

    Joined:
    Aug 14, 2008
    #1
    I'm able to successfully load a local html-file into a UIWebView, and from that load a local image via the html-code.

    Loading a local sound-file from the UIWebView, however, is a different matter.

    Does anybody know why the code below only manages to display a local image but no local sound, and what the solution would be?


    Code:
    // 1) Get: Get string from “outline.plist” in the “DrillDownSave”-codesample.
    savedUrlString = [item objectForKey: @"itemUrl"];
    
    // 2) Set: The url in string-format, excluding the html-appendix.
    NSString *tempUrlString = savedUrlString;
    
    // 3) Set: Format a url-string correctly. The html-file is located locally.
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:tempUrlString ofType:@”html”];
    
    // 4) Set: Set an “NSData”-object of the url-sting.
    NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
    
    // 5. Gets the path to the main bundle root folder
    NSString *imagePath = [[NSBundle mainBundle] resourcePath];
    
    // 6. Need to be double-slashes to work correctly with UIWebView, so change all “/” to “//”
    imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
    
    // 7. Also need to replace all spaces with “%20″
    imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    
    //  Load:	Loads the local html-page.
    [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//",imagePath]]];
    

    And here is the simple html-fil loaded into the UIWebView (not only the "img src" finds the local image, whereas the "embed src" does not find the local sound-file):

    Code:
    <HTML>
    <BODY>
    <img src=”logo.jpg”>
    <br>
    <embed src=”test.wav”>
    </BODY>
    </HTML>
    
     
  2. macrumors 68000

    Joined:
    Feb 12, 2008
    Location:
    Illinois
    #2
    The iPhone's WebView cannot play html embedded sound.
     
  3. thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #3
    Hi Niiro13, and thanks for the input.

    Actually the embedded sound works fine as long as the sound-file is not loaded locally. Ie. this works:

    <embed src=”http://www.mysite.com/test.wav”>

    So the problem must be that the code doesn't find the local sound-file, not that WebView doesn't play embedded sounds, right?
     
  4. macrumors newbie

    Joined:
    Jul 25, 2009
    #4
    Hi,

    Did you try base64 encoding ?
     
  5. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #5
    You could always try aiming the .wav file URL at localhost, and building a mini web server into your app. The mini web server wouldn't have to be fancy. It could just respond to any request by shipping the sound file.
     

Share This Page