我想接受两个mp3文件。这两个mp3文件应该存储在同一个m4a文件中(覆盖?),当播放特定的m4a文件时,两个mp3应该同时播放。
我想播放m4a歌曲并确定UIButton
动作中m4a歌曲的路径。 UIButton
行动的代码已在下面给出。
我已经使用两个不同的AVAudioPlayer
来接受两个mp3并使用一个变量(* audioFileOutput
)来存储目标m4a文件。
我在.m文件中导入了两个库:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
-(IBAction)playTheSong
{
// Path of your source audio file
NSString *strInputFilePath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"saewill.mp3"];
NSString *strInputFilePath2 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"kick.mp3"];
NSURL *audioFileInput1 = [NSURL fileURLWithPath:strInputFilePath1];
NSURL *audioFileInput2 = [NSURL fileURLWithPath:strInputFilePath2];
// Path of your destination save audio file
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];
AVAsset *asset1 = [AVAsset assetWithURL:audioFileInput1];
AVAsset *asset2 = [AVAsset assetWithURL:audioFileInput2];
AVAssetExportSession *exportSession1 = [AVAssetExportSession exportSessionWithAsset:asset1
presetName:AVAssetExportPresetAppleM4A];
AVAssetExportSession *exportSession2 = [AVAssetExportSession exportSessionWithAsset:asset2
presetName:AVAssetExportPresetAppleM4A];
//two store two diifrent mp3 in same variable
exportSession1.outputURL = audioFileOutput;
exportSession1.outputFileType = AVFileTypeAppleM4A;
exportSession2.outputURL = audioFileOutput;
exportSession2.outputFileType = AVFileTypeAppleM4A;
//initialised AVAudioplayer for 1st mp3.
[exportSession1 exportAsynchronouslyWithCompletionHandler:^
{
if (exportSession1.status == AVAssetExportSessionStatusCompleted)
{
NSLog(@"Success!");
NSLog(@" OUtput path is \n %@", requiredOutputPath);
NSFileManager * fm = [[NSFileManager alloc] init];
[fm moveItemAtPath:strOutputFilePath toPath:requiredOutputPath error:nil];
NSURL *url=[NSURL fileURLWithPath:requiredOutputPath];
NSError *error;
audioPlayer1=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
audioPlayer1.numberOfLoops=0;
[audioPlayer1 setVolume:0.75];
[audioPlayer1 play];
}
else if (AVAssetExportSessionStatusFailed == exportSession1.status)
{
NSLog(@"failed %@", exportSession1.error.localizedDescription);
}
}];
//initialised AVAudioplayer for 2nd mp3.
[exportSession2 exportAsynchronouslyWithCompletionHandler:^
{
if (exportSession2.status == AVAssetExportSessionStatusCompleted)
{
NSLog(@"Success!");
NSLog(@" OUtput path is \n %@", requiredOutputPath);
NSFileManager * fm = [[NSFileManager alloc] init];
[fm moveItemAtPath:strOutputFilePath toPath:requiredOutputPath error:nil];
NSURL *url=[NSURL fileURLWithPath:requiredOutputPath];
NSError *error;
audioPlayer2=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
audioPlayer2.numberOfLoops=0;
[audioPlayer2 setVolume:0.75];
[audioPlayer2 play];
}
else if (AVAssetExportSessionStatusFailed == exportSession2.status)
{
NSLog(@"failed %@", exportSession2.error.localizedDescription);
}
}];
}
答案 0 :(得分:0)
不要自己做,让一些iOS音频库帮助您实现它,这里有两个不错的选择:
编辑:
在Apple的医生:
同时播放多个声音
要同时播放多个声音,请为每个声音创建一个播放音频队列对象。对于每个音频队列,使用AudioQueueEnqueueBufferWithParameters函数安排第一个音频缓冲区同时启动。
您应该使用audio queue
另外我认为还有第二种选择:每次调用AVPlayer之前,将两个音频文件混合到一个临时的单个音频文件中。
答案 1 :(得分:0)
我已经解决了我的问题。
as i am new to the ios development so it took much time to obtain the desired result. thanks for your suggestions.
and here is what i did.
-(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:@"Kick 1" 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:@"Kick 2" 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];
}];
}