响应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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值