2015-05-14 101 views
1

有沒有辦法將NSData指定爲const char。我知道NSString可以使用指定NSData爲const char

const char *value = [myString UTF8String]; 

我嘗試使用

const char *value = [myData bytes]; 

NSString *dataString = [NSString alloc]initWithData:myData encoding: NSASCIIStringEncoding]; 
const char *value = [dataString UTF8String]; 

分配NSData但沒有工作,被分配到const char。 myData是包含音頻數據的NSData

編輯:

現有

resourcePath = [[NSBundle mainBundle] pathForResource:@"Busytone" ofType:@"wav"]; 
linphone_core_set_tone([LinphoneManager getLc], LinphoneToneBusy , [resourcePath UTF8String]); 

我試圖替換成

NSData*data= configure.mailNotificationAudioFiles[@"BusyTone"]; 
NSString *str =[[NSString alloc]initWithData:data encoding:NSASCIIStringEncoding]; 
linphone_core_set_tone([LinphoneManager getLc], LinphoneToneBusy ,[str UTF8String]); 

到數據傳遞

LINPHONE_PUBLIC void linphone_core_set_tone(LinphoneCore *lc, LinphoneToneID id, const char *audiofile); 
+0

如果是音頻數據解決的問題那麼它不是'const char',它是'const uint8_t'。你將這個const char *數據傳給了什麼? – trojanfoe

+0

'const char * value = [myData bytes];'應該有效。有什麼問題? –

+0

@trojanfoe我傳遞給具有const car參數播放音頻的方法。但是,如果音頻從NSBundle加載,並將該路徑分配爲帶有UTF8String的nsstring,它就可以工作。 – vishnuvarthan

回答

0

OK的方法,以在這個答案的平底船; OP想要用硬編碼的@"BusyTone"文件名替換我認爲可配置的東西。

假設所有的聲音文件都包含在應用包,那麼這應該做的工作:

NSString *soundFile = configure.mailNotificationAudioFiles[@"BusyTone"]; 
if (!soundFile) 
    soundFile = @"BusyTone"; 
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:soundFile 
                 ofType:@"wav"]; 
if (resourcePath) { 
    linphone_core_set_tone([LinphoneManager getLc], LinphoneToneBusy , [resourcePath UTF8String]); 
} else { 
    // Handle error 
} 
+0

代碼在NSBundle行中引發感知 - [_ NSInlineData characterAtIndex:]:發送到實例0x5716000的無法識別的選擇器 – vishnuvarthan

0

謝謝你們,我做這個

if(!configure.mailNotificationAudioFiles[@"BusyTone"]) 
      { 
       resourcePath = [[NSBundle mainBundle] pathForResource:@"Busytone" ofType:@"wav"]; 
       linphone_core_set_tone([LinphoneManager getLc], LinphoneToneBusy , [resourcePath UTF8String]); 

      } 
      else{ 

       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
       NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Busytone.wav"]; 
       NSData * data =configure.mailNotificationAudioFiles[@"BusyTone"]; 
       NSError *writeError = nil; 
       [data writeToFile:filePath options:NSDataWritingAtomic error:&writeError]; 
       if (writeError) { 
        NSLog(@"Error writing file: %@", writeError); 
       } 
       linphone_core_set_tone([LinphoneManager getLc], LinphoneToneBusy ,[filePath UTF8String]); 

      }