ios - Xcode:如何将 MP4 文件转换为音频文件
<p><p>我想将应用程序文档文件夹中的 MP4 文件转换为音频文件(mp3 或 m4a)。
我已经试过了,但是我不能用 AVPlayer 播放转换后的 MP3 文件。</p>
<p>这是我的代码:</p>
<pre><code>-(void)convertMP4toMP3withFile:(NSString*)dstPath
{
NSURL *dstURL = ;
AVMutableComposition* newAudioAsset = ;
AVMutableCompositionTrack*dstCompositionTrack;
dstCompositionTrack = ;
AVAsset* srcAsset = ;
AVAssetTrack* srcTrack = [ objectAtIndex:0];
CMTimeRange timeRange = srcTrack.timeRange;
NSError* error;
if(NO == ) {
NSLog(@"track insert failed: %@\n", error);
return;
}
AVAssetExportSession* exportSesh = [ initWithAsset:newAudioAsset presetName:AVAssetExportPresetPassthrough];
exportSesh.outputFileType = AVFileTypeAppleM4A;
exportSesh.outputURL = dstURL;
[ removeItemAtURL:dstURL error:nil];
[exportSesh exportAsynchronouslyWithCompletionHandler:^{
AVAssetExportSessionStatusstatus = exportSesh.status;
NSLog(@"exportAsynchronouslyWithCompletionHandler: %i\n", status);
if(AVAssetExportSessionStatusFailed == status) {
NSLog(@"FAILURE: %@\n", exportSesh.error);
} else if(AVAssetExportSessionStatusCompleted == status) {
NSLog(@"SUCCESS!\n");
NSError *error;
//append the name of the file in jpg form
//check if the file exists (completely unnecessary).
NSString *onlyPath = ;
NSString *toPathString = ;
[ moveItemAtPath:dstPath toPath:toPathString error:&error];
;
}
}];
}
</code></pre>
<p>有没有人可以解决我的问题或可以改进我的代码?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>替换这一行:</p>
<pre><code>exportSesh.outputFileType = AVFileTypeAppleM4A;
</code></pre>
<p>与:</p>
<pre><code>exportSesh.outputFileType = AVFileTypeCoreAudioFormat;
</code></pre>
<p>&这个:</p>
<pre><code>NSString *toPathString = ;
</code></pre>
<p>与:</p>
<pre><code>NSString *toPathString = ;
</code></pre>
<p>为我工作:)</p></p>
<p style="font-size: 20px;">关于ios - Xcode:如何将 MP4 文件转换为音频文件,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/27590510/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/27590510/
</a>
</p>
页:
[1]