我是iPhone开发的新手。我正在开发一款iPhone App。因为我使用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];
}
答案 0 :(得分: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;