1

我是 iPhone 开发的新手。我正在开发一个 iPhone 应用程序。我使用 MPMediaController 来挑选歌曲。然后我将该文件转换为 NSData 并将其上传到服务器。我的代码在选择“mp3”文件时工作正常,但是当我选择“m4a”文件时我遇到了问题。该文件被转换为数据,但在通过 AVAudioPlayer 播放测试结果数据后,它没有播放。请给我一个解决方案或建议我哪里出错了。

我的代码是:

-(IBAction)selectMusicButtonPressed:(id)sender
{

     MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];

        mediaPicker.delegate = self;
        mediaPicker.allowsPickingMultipleItems = NO;

    [self presentModalViewController:mediaPicker animated:YES];
}


- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection 
{


   NSURL *url;
   NSMutableData *songData;


    MPMediaItemCollection *collection=mediaItemCollection;//[allAlbumsArray objectAtIndex:0];
    item = [collection representativeItem];
    song_name=[item valueForProperty:MPMediaItemPropertyTitle];


    NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];
    NSString *title=[item valueForProperty:MPMediaItemPropertyTitle];


    if (!assetURL) {

        NSLog(@"%@ has DRM",title);

    }

    else{



        url = [item valueForProperty: MPMediaItemPropertyAssetURL];

       NSString* AssetURL = [NSString stringWithFormat:@"%@",[item valueForProperty:MPMediaItemPropertyAssetURL]];




            url_string=[url absoluteString];

            AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:url options:nil];

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

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

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

            [reader addOutput:output];
            [output release];

            songData = [[NSMutableData alloc] init];

            [reader startReading];


            while (reader.status == AVAssetReaderStatusReading)
            {
                // AVAssetReaderTrackOutput method

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

                if (sampleBufferRef)
                {

                    CMBlockBufferRef blockBufferRef = CMSampleBufferGetDataBuffer(sampleBufferRef);
                    size_t length = CMBlockBufferGetDataLength(blockBufferRef);

                    NSLog(@"Size of the song----%zu",length);

                    UInt8 buffer[length];
                    CMBlockBufferCopyDataBytes(blockBufferRef, 0, length, buffer);
                    NSData *data = [[NSData alloc] initWithBytes:buffer length:length];
                    // NSLog(@"song length is %zu",length);
                    // NSLog(@"data is..........%@",data);
                    [songData appendData:data];
                    [data release];
                    CMSampleBufferInvalidate(sampleBufferRef);
                    CFRelease(sampleBufferRef);
                }
            }


            //Testing the result Data

     AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithData:songData] error:NULL]; 

             [player play];

}
4

1 回答 1

1

在我的旧项目中,我曾经将 m4a 文件转换为 NSData,如下所示:

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

NSURL *soundFileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/memo.m4a", documentsDirectory]];;
NSData *myData = [NSData dataWithContentsOfURL:soundFileURL];

return myData;
于 2013-12-11T23:51:28.697 回答