objective-c - iOS - 访问可修改的原始音乐库数据?

标签 objective-c ios ios5 audio

我知道现在可以从用户的音乐库中获取歌曲并通过更改速度、音高等进行编辑,但我似乎找不到有关如何访问原始数据的有用教程的链接的一首歌。有人可以指导我做这样的事情,或者可能有这样的图书馆吗?谢谢!

最佳答案

这里是一些您可以开始的源代码:

- (void) doSomethingWithAssett:(AVURLAsset *)songAsset {

NSError * error = nil;


AVAssetReader * reader = [[AVAssetReader alloc] initWithAsset:songAsset error:&error];

if (error) {
    NSLog(@"%@", [error description]);
}

AVAssetTrack * songTrack = [songAsset.tracks objectAtIndex:0];

NSDictionary* outputSettingsDict = [[NSDictionary alloc] initWithObjectsAndKeys:

                                    [NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
                                    //     [NSNumber numberWithInt:44100.0],AVSampleRateKey, /*Not Supported*/
                                    //     [NSNumber numberWithInt: 2],AVNumberOfChannelsKey,    /*Not Supported*/

                                    [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
                                    [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
                                    [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                                    [NSNumber numberWithBool:NO],AVLinearPCMIsNonInterleaved,

                                    nil];


AVAssetReaderTrackOutput* output = [[AVAssetReaderTrackOutput alloc] initWithTrack:songTrack outputSettings:outputSettingsDict];

[reader addOutput:output];

UInt32 sampleRate,channelCount;

NSArray* formatDesc = songTrack.formatDescriptions;
for(unsigned int i = 0; i < [formatDesc count]; ++i) {
    CMAudioFormatDescriptionRef item = (__bridge CMAudioFormatDescriptionRef)[formatDesc objectAtIndex:i];
    const AudioStreamBasicDescription* fmtDesc = CMAudioFormatDescriptionGetStreamBasicDescription (item);
    if(fmtDesc ) {

        sampleRate = fmtDesc->mSampleRate;
        channelCount = fmtDesc->mChannelsPerFrame;

        //    NSLog(@"channels:%u, bytes/packet: %u, sampleRate %f",fmtDesc->mChannelsPerFrame, fmtDesc->mBytesPerPacket,fmtDesc->mSampleRate);
    }
}


UInt32 bytesPerSample = 2 * channelCount;
SInt16 normalizeMax = 0;

NSMutableData * fullSongData = [[NSMutableData alloc] init];
[reader startReading];


UInt64 totalBytes = 0; 


SInt64 totalLeft = 0;
SInt64 totalRight = 0;
NSInteger sampleTally = 0;

NSInteger samplesPerPixel = sampleRate / 50;


while (reader.status == AVAssetReaderStatusReading){

    AVAssetReaderTrackOutput * trackOutput = (AVAssetReaderTrackOutput *)[reader.outputs objectAtIndex:0];
    CMSampleBufferRef sampleBufferRef = [trackOutput copyNextSampleBuffer];

    if (sampleBufferRef){
        CMBlockBufferRef blockBufferRef = CMSampleBufferGetDataBuffer(sampleBufferRef);

        size_t length = CMBlockBufferGetDataLength(blockBufferRef);
        totalBytes += length;


        @autoreleasepool {
            NSMutableData * data = [NSMutableData dataWithLength:length];
            CMBlockBufferCopyDataBytes(blockBufferRef, 0, length, data.mutableBytes);


            SInt16 * samples = (SInt16 *) data.mutableBytes;

你有 sample ,用它们做点什么 ...

关于objective-c - iOS - 访问可修改的原始音乐库数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11443382/

相关文章:

iphone - 在 MPMoviePlayerController 中播放 mov 文件时出现问题

ios - 访问应用程序包中包含的文件

ios - 在 ios6 的 ipad 应用程序中将图像附加到电子邮件

ios5 - iAd 无法在 iOS 6 上运行

iOS Swift 使用 for 循环在 TableView Cell 中添加两个 View

ios - 如何即时更改弹出窗口的坐标

ios - App Delegate的应用程序:didReceiveLocalNotification: not called

ios - 为什么同一个应用程序在不同设备上的大小不同

ios - 没有这样的模块 'AppTrackingTransparency' 错误

objective-c - 横向启动时不调用 iOS6 旋转方法