2011-07-10 1 views
7

मैं उद्देश्य-सी में नवागंतुक हूं और आईफोन विकास में केवल 5 महीने का अनुभव करता हूं।आईओएस में 2 या 3 ऑडियो फाइलों को कैसे जोड़ना है?

मैं क्या जरूरत है:
मैं और एआइएफएफ, एमपी 3, CAF या m4a प्रारूप के रूप में निर्यात परिणाम एक में 2 या अधिक ऑडियो फाइलों को श्रेणीबद्ध करने की जरूरत है,।

उदाहरण के लिए:
पहले ऑडियो वाले "आप की जरूरत है", दूसरा "डाउनलोड" और तीसरा "दस्तावेज़" फ़ाइल।
प्रत्येक ऑडियो भाग उपयोगकर्ता से कार्यों पर निर्भर करता है।

मैंने भाग्य के बिना 2 दिन बिताए। वह जगह मेरी आखिरी सीमा है।

मैं कोड के एक टुकड़े के लिए बहुत सराहना करता हूं।

+2

तुम सच मतलब है * मर्ज *, या आप शायद मतलब है * CONCATENATE *? –

+0

मेरे अच्छी तरह से अंग्रेजी के लिए खेद है। मुझे परिणामस्वरूप एक ऑडियो फ़ाइल "आपको डाउनलोड दस्तावेज़ की आवश्यकता है" 3 फाइलों से "आपको चाहिए" + "डाउनलोड" + "दस्तावेज़"। मैं सही शब्द सोच रहा हूं ** concatenate **। – EEduard

+0

ठीक है - मैंने इसे स्पष्ट करने के लिए अपना प्रश्न संपादित कर लिया है। –

उत्तर

-1

मुझे एक विचार है, यकीन नहीं है कि यह काम करेगा। इन 3 फ़ाइलों से NSData प्राप्त करने का प्रयास करें, डेटा को किसी अन्य NSData में जोड़ें और फिर इसे लिखें। कुछ की तरह:

NSMutableData *concatenatedData = [NSMutableData alloc] init]; 
NSData *data1 = [[NSData alloc] initWithContentsOfFile:(NSString *)path]; 
NSData *data2 = [[NSData alloc] initWithContentsOfFile:(NSString *)path]; 
NSData *data3 = [[NSData alloc] initWithContentsOfFile:(NSString *)path]; 
[concatenatedData appendData: data1]; 
[concatenatedData appendData: data2]; 
[concatenatedData appendData: data3]; 
[concatenatedData writeToFile:@"/path/to/concatenatedData.mp3" atomically:YES]; 

यह एक सिद्धांत मुझे यकीन है कि यह काम करेगा :), यह वास्तव में अगर मैं हेक्स संपादक के साथ एक एमपी 3 खोलने के काम करता है नहीं कर रहा हूँ है - सब कुछ कॉपी और अंत में पेस्ट - तो मैं एक ही है दो बार आवाज कृपया इसे आज़माएं और अगर यह काम करता है तो हमें बताएं।

AudioFileCreateWithURL //to create the output file 
For each input file: 
    AudioFileOpenURL //open file 
    repeat 
     AudioFileReadBytes //read from input file 
     AudioFileWriteBytes //write to output file 
    until eof(input file) 
    AudioFileClose //close input file 
AudioFileClose //close output file 

यह शायद की आवश्यकता होगी कि इनपुट फ़ाइलें सभी एक ही प्रारूप है और है कि एक ही प्रारूप में उत्पादन फ़ाइल बना सकेगी:

+0

** Dimitar Marinov **, आपके उत्तर के लिए धन्यवाद। मै कोशिश करुॅगा। मैं यहां परिणाम पोस्ट करूंगा वैसे भी मदद करता हूं या नहीं। आपकी सहायता के लिए एक बार फिर से धन्यवाद। – EEduard

+0

सुनिश्चित नहीं है कि आप उपर्युक्त कोड के साथ जो चाहते थे वह करने में सफल रहे। यह एमपी 3 या एम 4 ए ऑडियो फाइलों के लिए काम नहीं करता है। –

0

आप कुछ इस तरह की कोशिश की है। यदि आपको प्रारूप को रूपांतरित करने की आवश्यकता है, तो आउटपुट फ़ाइल बनाने के बाद यह बेहतर हो सकता है।

22

नीचे ऑडियो कोड ऑडियो फ़ाइलों को मर्ज करने के लिए उपयोग किया जा सकता है।

इनपुट फ़ाइलें: फ़ाइलों के आईडी सरणी ऑडियो आईडी में आपूर्ति की जा सकती हैं। ईजी। audio1.mp3, audio2.mp3 ... audioN.mp3 दस्तावेजों में उपलब्ध होने की फ़ोल्डर आउटपुट फ़ाइल: combined.m4a

 - (BOOL) combineVoices { 

     NSError *error = nil; 
     BOOL ok = NO; 


     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 


     CMTime nextClipStartTime = kCMTimeZero; 
     //Create AVMutableComposition Object.This object will hold our multiple AVMutableCompositionTrack. 
     AVMutableComposition *composition = [[AVMutableComposition alloc] init]; 

    AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 

    for (int i = 0; i< [self.audioIds count]; i++) { 
     int key = [[self.audioIds objectAtIndex:i] intValue]; 
     NSString *audioFileName = [NSString stringWithFormat:@"audio%d", key]; 

     //Build the filename with path 
     NSString *soundOne = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3", audioFileName]]; 
     //NSLog(@"voice file - %@",soundOne); 

     NSURL *url = [NSURL fileURLWithPath:soundOne];  
     AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil]; 
     NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio]; 
     if ([tracks count] == 0) 
      return NO; 
     CMTimeRange timeRangeInAsset = CMTimeRangeMake(kCMTimeZero, [avAsset duration]); 
     AVAssetTrack *clipAudioTrack = [[avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 
     ok = [compositionAudioTrack insertTimeRange:timeRangeInAsset ofTrack:clipAudioTrack atTime:nextClipStartTime error:&error]; 
     if (!ok) { 
      NSLog(@"Current Video Track Error: %@",error); 
     } 
     nextClipStartTime = CMTimeAdd(nextClipStartTime, timeRangeInAsset.duration); 
    } 

    // create the export session 
    // no need for a retain here, the session will be retained by the 
    // completion handler since it is referenced there 
    AVAssetExportSession *exportSession = [AVAssetExportSession 
              exportSessionWithAsset:composition 
              presetName:AVAssetExportPresetAppleM4A]; 
    if (nil == exportSession) return NO; 

    NSString *soundOneNew = [documentsDirectory stringByAppendingPathComponent:@"combined.m4a"]; 
    //NSLog(@"Output file path - %@",soundOneNew); 

    // configure export session output with all our parameters 
    exportSession.outputURL = [NSURL fileURLWithPath:soundOneNew]; // output path 
    exportSession.outputFileType = AVFileTypeAppleM4A; // output file type 

    // perform the export 
    [exportSession exportAsynchronouslyWithCompletionHandler:^{ 

     if (AVAssetExportSessionStatusCompleted == exportSession.status) { 
      NSLog(@"AVAssetExportSessionStatusCompleted"); 
     } else if (AVAssetExportSessionStatusFailed == exportSession.status) { 
      // a failure may happen because of an event out of your control 
      // for example, an interruption like a phone call comming in 
      // make sure and handle this case appropriately 
      NSLog(@"AVAssetExportSessionStatusFailed"); 
     } else { 
      NSLog(@"Export Session Status: %d", exportSession.status); 
     } 
    }]; 

    return YES; 
} 
+0

परिणामस्वरूप m4a फ़ाइल को एमपी 3 फ़ाइल में परिवर्तित करने का कोई तरीका है। ? –

+0

यह पूरी तरह से काम करता है .. अगर आपको फ़ोल्डर संरचना को बदलने की आवश्यकता है, तो बस अपनी ज़रूरत के अनुसार दस्तावेज़ निर्देशिका को संशोधित करें ... –

+0

यह काम कर रहा है – commando24

1

आप शायद एक ही करने के लिए इस पोस्ट में दिखना चाहिए: Combine two audio files into one in objective c

जवाब Dimitar के सुझाव के समान है। लेकिन आपको ध्यान में रखना महत्वपूर्ण 2 महत्वपूर्ण चीजें हैं, पहला - यह केवल एमपी 3 प्रारूप के लिए काम करता है और दूसरी फाइलों की बिट-रेट जो आप संयोजित करने की कोशिश कर रहे हैं, वही होना चाहिए, या अन्यथा आपकी फ़ाइल के कुछ हिस्से अंतिम आउटपुट में खेलते हैं। यह खेलना बंद कर देगा जहां बिट दर बदलती है।

एसएसटीवी- वाव फाइलों जैसी फ़ाइलें अपने स्वयं के शीर्षलेख हैं और यदि आप सिर्फ एक फ़ाइल के बाद एक फ़ाइल लिखते हैं, तो यह केवल पहली फ़ाइल खेलेंगे और फिर खेलना बंद कर देगा, इसके बावजूद फ़ाइल जानकारी अधिक फ़ाइल आकार दिखाती है। ऐसा इसलिए है क्योंकि हमारे पास पहली फ़ाइल के शीर्षलेख में अपडेट की गई जानकारी नहीं है।

3

आप इस विधि का उपयोग 3 ध्वनियों को एक साथ विलय करने के लिए कर सकते हैं।

- (BOOL) combineVoices1 
{ 
    NSError *error = nil; 
    BOOL ok = NO; 


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 


    CMTime nextClipStartTime = kCMTimeZero; 
    //Create AVMutableComposition Object.This object will hold our multiple AVMutableCompositionTrack. 
    AVMutableComposition *composition = [[AVMutableComposition alloc] init]; 

    AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [compositionAudioTrack setPreferredVolume:0.8]; 
    NSString *soundOne =[[NSBundle mainBundle]pathForResource:@"test1" ofType:@"caf"]; 
    NSURL *url = [NSURL fileURLWithPath:soundOne]; 
    AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil]; 
    NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio]; 
    AVAssetTrack *clipAudioTrack = [[avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 
    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil]; 

    AVMutableCompositionTrack *compositionAudioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [compositionAudioTrack setPreferredVolume:0.3]; 
    NSString *soundOne1 =[[NSBundle mainBundle]pathForResource:@"test" ofType:@"caf"]; 
    NSURL *url1 = [NSURL fileURLWithPath:soundOne1]; 
    AVAsset *avAsset1 = [AVURLAsset URLAssetWithURL:url1 options:nil]; 
    NSArray *tracks1 = [avAsset1 tracksWithMediaType:AVMediaTypeAudio]; 
    AVAssetTrack *clipAudioTrack1 = [[avAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 
    [compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) ofTrack:clipAudioTrack1 atTime:kCMTimeZero error:nil]; 


    AVMutableCompositionTrack *compositionAudioTrack2 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [compositionAudioTrack2 setPreferredVolume:1.0]; 
    NSString *soundOne2 =[[NSBundle mainBundle]pathForResource:@"song" ofType:@"caf"]; 
    NSURL *url2 = [NSURL fileURLWithPath:soundOne2]; 
    AVAsset *avAsset2 = [AVURLAsset URLAssetWithURL:url2 options:nil]; 
    NSArray *tracks2 = [avAsset2 tracksWithMediaType:AVMediaTypeAudio]; 
    AVAssetTrack *clipAudioTrack2 = [[avAsset2 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 
    [compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset2.duration) ofTrack:clipAudioTrack2 atTime:kCMTimeZero error:nil]; 



    AVAssetExportSession *exportSession = [AVAssetExportSession 
              exportSessionWithAsset:composition 
              presetName:AVAssetExportPresetAppleM4A]; 
    if (nil == exportSession) return NO; 

    NSString *soundOneNew = [documentsDirectory stringByAppendingPathComponent:@"combined10.m4a"]; 
    //NSLog(@"Output file path - %@",soundOneNew); 

    // configure export session output with all our parameters 
    exportSession.outputURL = [NSURL fileURLWithPath:soundOneNew]; // output path 
    exportSession.outputFileType = AVFileTypeAppleM4A; // output file type 

    // perform the export 
    [exportSession exportAsynchronouslyWithCompletionHandler:^{ 

     if (AVAssetExportSessionStatusCompleted == exportSession.status) { 
      NSLog(@"AVAssetExportSessionStatusCompleted"); 
     } else if (AVAssetExportSessionStatusFailed == exportSession.status) { 
      // a failure may happen because of an event out of your control 
      // for example, an interruption like a phone call comming in 
      // make sure and handle this case appropriately 
      NSLog(@"AVAssetExportSessionStatusFailed"); 
     } else { 
      NSLog(@"Export Session Status: %d", exportSession.status); 
     } 
    }]; 


    return YES; 


} 
+0

यह हमेशा वापस लौटाता है AVAssetExportSessionStatusFailed – riddhi

0

मैंने AVMutableCompositionTrack से दो भिन्न एमपी 3 फ़ाइलें ली हैं। और इन दो एमपी 3 फ़ाइलों को एक ही AVMutableComposition में संग्रहीत किया जाता है।

जब मैं बटन दबा दूंगा तो नया एमपी 3 का मार्ग कंसोल द्वारा दिखाया जाएगा।

-(IBAction)play 
{ 
    [self mixAudio];  
} 

-(void)mixAudio 
{ 
    CFAbsoluteTime currentTime=CFAbsoluteTimeGetCurrent(); 
    AVMutableComposition *composition = [[AVMutableComposition alloc] init]; 

    AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [compositionAudioTrack setPreferredVolume:0.8]; 
    NSString *soundOne =[[NSBundle mainBundle]pathForResource:@"KICK1" ofType:@"mp3"]; 
    NSURL *url = [NSURL fileURLWithPath:soundOne]; 
    AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil]; 
    NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio]; 
    AVAssetTrack *clipAudioTrack = [tracks objectAtIndex:0]; 
    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil]; 

    AVMutableCompositionTrack *compositionAudioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [compositionAudioTrack setPreferredVolume:0.8]; 
    NSString *soundOne1 =[[NSBundle mainBundle]pathForResource:@"KICK2" ofType:@"mp3"]; 
    NSURL *url1 = [NSURL fileURLWithPath:soundOne1]; 
    AVAsset *avAsset1 = [AVURLAsset URLAssetWithURL:url1 options:nil]; 
    NSArray *tracks1 = [avAsset1 tracksWithMediaType:AVMediaTypeAudio]; 
    AVAssetTrack *clipAudioTrack1 = [tracks1 objectAtIndex:0]; 
    [compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset1.duration) ofTrack:clipAudioTrack1 atTime: kCMTimeZero error:nil]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 
    NSString *libraryCachesDirectory = [paths objectAtIndex:0]; 
    NSString *strOutputFilePath = [libraryCachesDirectory stringByAppendingPathComponent:@"output.mov"]; 
    NSString *requiredOutputPath = [libraryCachesDirectory stringByAppendingPathComponent:@"output.m4a"]; 
    NSURL *audioFileOutput = [NSURL fileURLWithPath:requiredOutputPath]; 
    [[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL]; 

    AVAssetExportSession *exporter=[[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetAppleM4A]; 
    exporter.outputURL=audioFileOutput; 
    exporter.outputFileType=AVFileTypeAppleM4A; 

    [exporter exportAsynchronouslyWithCompletionHandler:^{ 

     NSLog(@" OUtput path is \n %@", requiredOutputPath); 
     NSFileManager * fm = [[NSFileManager alloc] init]; 
     [fm moveItemAtPath:strOutputFilePath toPath:requiredOutputPath error:nil]; 

     NSLog(@" OUtput path is \n %@", requiredOutputPath); 
     NSLog(@"export complete: %lf",CFAbsoluteTimeGetCurrent()-currentTime); 
     NSError *error; 
     audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:audioFileOutput error:&error]; 
     audioPlayer.numberOfLoops=0; 
     [audioPlayer play]; 

    }]; 

} 
+0

ऑडियो 1 को चलाने के लिए क्या करना है और इस ऑडियो के बाद 2 play –

+0

यह नाटक ऑडियो 1 और ऑडियो 2 एक साथ –

0

सबसे आसान तरीका है aac के कई संयोजन लागू करने के लिए:

- (NSString *)concatenatedAACVoicesPath{ 
NSMutableData *concatenatedData = [[NSMutableData alloc] init]; 

NSArray *aacPathArr = [self queryAAC]; 
for (NSString *path in aacPathArr) { 
    NSData *data = [[NSData alloc] initWithContentsOfFile:path]; 
    [concatenatedData appendData: data]; 
} 

NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@.aac",[NSString createPath:currRecordDocName],currRecordDocName]; 
[concatenatedData writeToFile:fileNamePath atomically:YES]; 

return fileNamePath; 

}

+0

जहां मैं [self queryAAC] की आपकी विधि परिभाषा कहां हूं? –