How to make MPMoviePlayerController respect the ring/silent switch? - mpmovieplayercontroller

I'm trying, unsuccessfully, to get the MPMoviePlayerController to play movies silently if the ring/silent switch on the iPhone is set to silent. There are no interface methods to help me out nor does the player respect the AudioSessionProperty() trick:
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionSetProperty(
kAudioSessionProperty_AudioCategory,
sizeof (sessionCategory),
&sessionCategory);
Has anyone had any success silencing movie playback?

I spent some time trying to get this to work myself. Eventually I gave up after trying, failing and reading this post on the apple dev forums.
"The MPMoviePlayerController establishes its own audio session and there is nothing you can do to affect this"

MPMoviePlayerController has a property useApplicationAudioSession that will allow the player to respect the device's silence setting.
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
player.useApplicationAudioSession = YES;
[player play];

Add this in your code:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

Related

MPMoviePlayerController shows blank screen second time launching

I am using MPMoviePlayerController to play video on iOS.
First time I hit the url and video gets downloaded to documents directory & played well.
Second time I check if the video is already downloaded or not.
If not then it goes to server & download it,
if yes then it should access it from documents directory and should play video.
but when I fetch video from documents directory then it shows the path well, along with video file name, but
it doesn't play the video.
Directly it moves to last view with blank white screen.
What I am doing wrong.
Thanks in advance.
Here is my code.
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [arrayPaths objectAtIndex:0];
NSString* destinationDirectoryPath=docDir;
NSString *clientPathString = [[NSString alloc] initWithFormat:#"/user"];
destinationDirectoryPath = [destinationDirectoryPath stringByAppendingString:clientPathString];
destinationDirectoryPath = [destinationDirectoryPath stringByAppendingString:#"/video1/"];
NSString *filePath=[NSString stringWithFormat:#"%#",userVideoDTO.learnerid];//#"author";
filePath=[destinationDirectoryPath stringByAppendingString:[NSString stringWithFormat:#"%#.mp4",filePath]];
NSLog(#"FilePath new one :- %#",filePath);
self.url = [NSURL fileURLWithPath:filePath];
Then this self.url is passed to MPMoviePlayerController as
self.mp =[[MPMoviePlayerController alloc] initWithContentURL:self.url];
[self.view addSubview:mp.view];
[self.mp play];
What I am missing.
Can anyone let me know.
This same code is working fine on lower versions of iOS. like iOS7 and below.
not on iOS8.
Thanks in advance.
I have been having a similar issue, and it turned out that while in iOS7 and below the containing view controller hadn't had it's -viewWillDisappear:animated: called before the movie player was shown, whereas in iOS8 and going further, it does get called. I was releasing the player and unsubscribing from notifications in -viewWillDisappear:animated:, this time I had to move this code to dealloc.

Why is QTMovieLoadStateDidChangeNotification not firing?

I'm seeing that setting up a notification on QTMovieLoadStateDidChangeNotification has no effect and the target selector never gets called. Am I missing something?
In awakeFromNib:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieLoadStateDidChange:)
name:QTMovieLoadStateDidChangeNotification
object:nil];
On loading movie:
NSNumber *num = [NSNumber numberWithBool:YES];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
url, QTMovieURLAttribute,
nil];
self.mQTMovie1 = [[QTMovie alloc] initWithAttributes:attributes
error:&error];
Also
- (void)movieLoadStateDidChange:(NSNotification *)notification {
NSLog(#"movieLoadStateDidChange got called");
}
I'm not sure this is the answer but I've encountered this before. The cause in my case was a file whose codec was supported only by a third-party plugin (Flip4Mac in my case).
The load state notification isn't called until the movie finishes after it auto-plays (to nowhere). For long media files, it effectively looks like the notification is never called, since we rarely wait 5 minutes or an hour for a load notification when testing our code. To the user, it looks like the app simply isn't loading the file.
Having the user disable the plugin's auto-play-on-load in System Preferences resolves the issue but unfortunately this one support FAQ I can't get around since users of the app in question frequently use Flip4Mac to support files from common digital voice recorders.

Is it okay to use MPMoviePlayerController without adding it to our view?

I'm writing an online radio streaming app. I'm using my own buttons to control the playback. I really don't want the view of MPMoviePlayerController.
Will apple reject my app if I just let it play the audio without adding MPMoviePlayerController to my view?
I'm thinking of removing second last line in the code below:
player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[player prepareToPlay];
[player.view setFrame: myView.bounds];
[myView addSubview: player.view]; //i want to remove this line
[player play];
It is also possible to do the following to hide it without removing that line:
player.view.hidden = YES;

AVAudioPlayer is releasing before playing?

I'm making a soundboard app and I use this code to play mp3 files:
-(IBAction)playSound
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"mysound" ofType:#"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
[theAudio release];
}
When I include [theAudio release]; the sound doesn't play. If I take it out it plays but I don't want to have memory leaks. How can I fix this?
It would be very helpful if you could include whatever code I need to add. I'm new to programming (besides TrueBasic I've used in school) so I'm unfamiliar with Objective-C.
wait til the audio finishes via a delegate to release the audio. I believe there are a number of posts exactly like this on the site if you search, they will have the specific delegate code.
This is only because you use local variable. so theAudio will released immediately.
You should define it as class member and assignment it here. so when this function finished, it still work too.

How do you play an mp3 in a Mac Application in Xcode 4.2

I'm trying to play an mp3 in a Mac Application in Xcode 4.2. How would I do this?
You are not giving us much to work with...
If you just want a simple sound, use NSSound:
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:#"fn" ofType:#"mp3"];
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:resourcePath byReference:YES];
[sound play];
// wait for the sound to play before you release these...
With NSSound, the sound plays concurrently with your code. A common mistake is to release the sound or resource while it is still playing. Don't do that.
You can also use QTMovie in QTKit:
NSError *err = nil;
QTMovie *sound = [[QTMovie movieWithFile:#"fn.mp3" error:&err] retain];
[sound play];
For more demanding sounds, use CoreAudio
You should use QTKit framework and the QTMovie class.

Resources