AVAssetExportSession no audio (iPhone), works on iPad - ios

We're trying to take an existing video with audio (.mov) and make a more email friendly version. Seems pretty straightforward and the code below does just what we need ... almost.
On an iPad2 (4.3.3) it works in debug & release builds all of the time. On the iPhone 4 (4.3.3) or 4th gen iPod Touch there's no audio. From time to time, no obvious correlation as to what triggers it, it will start working on the iPhone. Delete the app, rebuild/install, and it no longer works.
AVURLAsset* asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:sourcePath] options:nil];
session = [[AVAssetExportSession alloc] initWithAsset:asset
presetName:AVAssetExportPresetLowQuality];
session.outputURL = [NSURL fileURLWithPath:destPath];
session.outputFileType = AVFileTypeQuickTimeMovie;
session.shouldOptimizeForNetworkUse = YES;
[session exportAsynchronouslyWithCompletionHandler:^{
[self performSelectorOnMainThread:#selector(conversionFinished)
withObject:nil
waitUntilDone:NO]; }];

Are you playing the movie too, for example in an MPMoviePlayer? I have had some occasional export quirks while playing or using other initialized assets with the same URLs.

Related

White video when opening AVMutableComposition in Instagram

After I export an AVMutableComposition I use PHPhotoLibrary to save the video to the camera roll. In the creationRequestForAssetFromVideoAtFileURL: completion handler, I then open the saved video in Instagram, like so:
__block PHObjectPlaceholder *videoAssetPlaceholder;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:localVideoURL];
videoAssetPlaceholder = req.placeholderForCreatedAsset;
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
completion(YES);
NSString *localID = videoAssetPlaceholder.localIdentifier;
NSRange rangeOfSlash = [localID rangeOfString:#"/"];
if (rangeOfSlash.location != NSNotFound) {
NSString *assetID = [localID substringToIndex:rangeOfSlash.location];
NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:#"instagram://library?AssetPath=%#", assetID]];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
}
}
}];
About 50% of the times Instagram opens and the video plays like expected. The other 50% of the times, however, both the video and the preview is white, and all I get is the sound. This usually gets fixed by selecting another video and then going back to my video. The video plays perfectly in the camera roll, it's only Instagram that causes problems. Is this an issue that Instagram has or could I be exporting my videos the wrong way?
These are my AVAssetExportSession settings:
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = url;
exporter.outputFileType = AVFileTypeMPEG4;
exporter.shouldOptimizeForNetworkUse = YES;
exporter.videoComposition = mainCompositionInst;
[exporter exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
handler(exporter.outputURL);
});
}];
Just heard from Apple DTS. They also agree this points to an Apple iOS bug and asked me to log it.
I cut out usage of AVAssetExportSession like mentioned above and it solved my problem as a work around. So the issue seems to be around that method which is probably contained in the Instagram method you are using.
So until Apple fixes this or Instagram builds a work around, there does not seem to be a solution for this problem... Bummer

How to manually change the streaming video quality in AV player in ios?

I am building application in which online streaming is handled by AV Player(Default iOS player).
I want to add button for HD streaming, how to I achieve that?
The solution I found was to ensure that the underlying AVAsset is ready to return basic info, such as its duration, before feeding it to the AVPlayer. AVAsset has a method loadValuesAsynchronouslyForKeys: which is handy for this:
AVAsset *asset = [AVAsset assetWithURL:self.mediaURL];
[asset loadValuesAsynchronouslyForKeys:#[#"duration"] completionHandler:^{
AVPlayerItem *newItem = [[AVPlayerItem alloc] initWithAsset:asset];
[self.avPlayer replaceCurrentItemWithPlayerItem:newItem];
}];
In my case the URL is a network resource, and replaceCurrentItemWithPlayerItem: will actually block for several seconds waiting for this information to download otherwise.

AVAudioPlayer does not play sound on some devices

I have an app that uses AVAudioPlayer to play sound effects, music, etc. It works fine for just about every user (100,000+ users), but I've had several reports of sound not playing at all even though sound effects are working on other apps on the same device. The bug has gone away for some users after an app restart/device reboot, but for others it continues to persist. AVSpeechSynthesizer isn't working for these users either.
I haven't been able to reproduce the bug to see if the AVAudioPlayer is throwing an error, so I'm not sure if it is - but I don't see why it would be working fine for 99.9% of users and not this tiny subset.
Has anyone else run into something like this?
Here's the code I'm using to set up the player:
[[AVAudioSession sharedInstance] setCategory:#"AVAudioSessionCategoryAmbient" error:nil];
NSString *file = [filename stringByDeletingPathExtension];
NSString *extension = [filename pathExtension];
NSURL *soundFileURL = [[NSBundle mainBundle] URLForResource:file withExtension:extension];
self.soundPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
[self.soundPlayer1 prepareToPlay];

xcode 5 no sound from mp3 on iOS 6 or earlier

The app plays some mp3 files and is working well on iOS 7, but on iOS 6 or earlier, the mp3 files are not played or at least I can hear nothing.
This is the code I use for playing mp3:
fileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/puerta.m4a", [[NSBundle mainBundle] resourcePath]]];
//Initialize the AVAudioPlayer.
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
// Preloads the buffer and prepares the audio for playing.
[self.audioPlayer prepareToPlay];
self.audioPlayer.currentTime = 0;
[self.audioPlayer play];
Does anyone had a similar issue and how fixed it - Many thanks for help.

iOS7 - AVAssetExportSession trimmed exported file shows wrong duration

This was working fine in iOS 6, but in iOS 7, after I export a portion of a song using AVAssetExportSession to a file, the exported file's duration is wrong in AVAudioPlayer, but correct in AVURLAsset.
AVAudioPlayer incorrectly reports the duration as the whole song duration.
I'm exporting files using steps from https://developer.apple.com/library/ios/qa/qa1730/_index.html
and checking the durations as below:
AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:outputURL options:nil];
CMTime audioDuration = audioAsset.duration; // shows correct
and
AVAudioPlayer* avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:outputURL error:nil];
NSTimeInterval duration = avAudioPlayer.duration; // shows wrong
Interestingly, if I play the exported file in iTunes, it also shows the wrong (whole) duration.
I'm not sure how to fix this problem. Could this be a bug in iOS7?

Resources