日期:2014-05-16  浏览次数:20399 次

响应UIWebView的点击事件(播放声音),从数据库中取出声音保存在本地。
    在iPhone开发中经常会遇到需要在UIWebView中点击按钮并进行相应的操作,比如点击按钮播放声音等,以下是代码,并从数据库中取出声音数据,以文件的形式保存在本地。

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
		...AppDelegate *appDelegate=(...AppDelegate *)[[UIApplication sharedApplication] delegate];
	if (navigationType == UIWebViewNavigationTypeLinkClicked) {
		NSURL *URL = [request URL];	
		NSLog([URL scheme]);
		NSLog([URL absoluteString]);
		
		//.......code here and call the delegate method


		NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
		BOOL audioResult=FALSE;
		NSFileManager *fileManager = [NSFileManager defaultManager];
		//check if file existed in cache folder
		NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
		NSString *cachePath = [cache objectAtIndex:0] ;
		NSString *filepath ;
				
		filepath=[cachePath stringByAppendingPathComponent:[audioId stringByAppendingString:@".wav"]];
				
		if ([fileManager fileExistsAtPath:filepath]==YES){
			[self playVoice:filepath];
		}
		else
		{
			audioResult=[appDelegate saveAudio:audioId];
			if (audioResult==YES)
			{
				//NSLog(@"Speak the audio by the path=%@",filepath);
				[self playVoice:filepath];
			}
		}
		[pool release];

		
		
		[appDelegate saveAudio:gotFM];
		
	}


保存声音的方法:
-(BOOL)saveAudio:(NSString *)key
{
	NSLog(key);
	BOOL saveSuccess=FALSE;
	NSString *dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"testVoice.db"];	
	
	if(sqlite3_open([dbPath UTF8String], &database_voice) == SQLITE_OK)
	{
		
		NSString *sqlString=[NSString stringWithFormat:@"select * from tb_voice where id='%@'",key];
		NSLog(sqlString);
		const char *sql_char=[sqlString UTF8String];
		//prepare the select statement
		int returnValue = sqlite3_prepare_v2(database_voice, sql_char, -1, &selectStatement, NULL);
		if(returnValue == SQLITE_OK)
		{
			//loop all the rows returned by the query.
			if(sqlite3_step(selectStatement) == SQLITE_ROW)
			{
				
				NSFileManager *fileManager = [NSFileManager defaultManager];
				NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
				NSString *cachePath = [cache objectAtIndex:0] ;
				
				
				NSString *filename=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 0)];
				////NSLog(filename);
				
				NSData *fileData=[NSData dataWithBytes:sqlite3_column_blob(selectStatement, 2) length:sqlite3_column_bytes(selectStatement,2)];
				//save the file data
				
				//check 
				NSString *filepath = [cachePath stringByAppendingPathComponent:filename];	
				
				filepath=[filepath stringByAppendingString:@".wav"];
				if ([fileManager createDirectoryAtPath:cachePath attributes:nil]==NO){
					////NSLog(@"fileManager createDirectoryAtPath:cachePath attributes:nil");
				}
				
				if ([fileData writeToFile:filepath atomically:YES]){
					saveSuccess=TRUE;
					//NSLog(@"saveSuccess");
					////NSLog(@"returnValue");
				}
				else
				{
					//NSLog(@"save Fail");
				}
			}
			sqlite3_finalize(selectStatement);
		}
	}
	return saveSuccess;
}