[英]How to detect whether an audio file stored in document directory is playable or not programmatically Xcode iPhone
我正在使用AVAudioPlayer播放存儲在文檔目錄中的音頻文件。音頻存在於目錄中,大小為82個字節。
我想檢測此音頻是否可播放。
/*save the Audio file in Document Directory */
NSFileManager *fileManager=[NSFileManager defaultManager];
/*Get the Path to Application Documents Directory*/
NSArray *docDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
/*append the neccessary file extension */
NSLog(@"%d",bt.tag);
int a=bt.tag-10000;
NSString *filepathStr=[NSString stringWithFormat:@"/%@/%@.mp3",docDir,[NSString stringWithFormat:@"%d",a]];
/*Check if my crrent file exists in the Documents Directory*/
if([fileManager fileExistsAtPath:filepathStr])
{
NSData *dat = [fileManager contentsAtPath:filepathStr];
NSLog(@"data is %d",[dat length]);
if(player)
{
[player stop];
[player release];
player=nil;
}
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:filepathStr] options:nil];
/* You can then check its playable property:*/
if (asset.playable)
{
player = [[AVAudioPlayer alloc] initWithData:dat error:nil];
player.delegate = self;
[player play];
//Do Something
}
我還有其他可播放的音頻,它們也可以很好地存儲在Document目錄中。
我已使用AVURLAsset並嘗試檢查其屬性“可播放”,但由於未形成AVURLAsset對象,因此無法成功。
該音頻文件的大小為82字節,但即使在iTunes中也不播放。
我剛剛找到解決方案,可以使用以下代碼來檢測音頻是否可播放
NSURL *url=[[NSURL alloc] initFileURLWithPath:filepathStr];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
/* You can then check its playable property:*/
if (asset.playable)
{
player = [[AVAudioPlayer alloc] initWithData:dat error:nil];
player.delegate = self;
[player play];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.