gpt4 book ai didi

iphone - 如何将从 MPMediaPickerController 选取的 m4a 文件保存为 NSData?

转载 作者:行者123 更新时间:2023-11-29 13:03:32 24 4
gpt4 key购买 nike

我是 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];

}

最佳答案

在我的旧项目中,我曾经像这样将 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;

关于iphone - 如何将从 MPMediaPickerController 选取的 m4a 文件保存为 NSData?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19193199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com