Unrecognized Selector in AVplayer setNumberOfLoops Method - objective-c

When calling the numberOfLoops method like so:
[_player setNumberOfLoops:-1];
I get the following error:
-[AVPlayer setNumberOfLoops:]: unrecognized selector sent to instance 0x7d52d30
How can this be fixed?
Code:
Header:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController {
}
#property (strong, nonatomic) AVAudioPlayer *player;
- (IBAction)playMusic:(id)sender;
#end
Implementation:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)playMusic:(id)sender {
_player = [AVPlayer playerWithURL:[NSURL URLWithString:#"http://urlpath.wav"]];
[_player setNumberOfLoops:-1];
[_player prepareToPlay];
[_player play];
}
#end
Thank you for your time,
Yoni201.

You've created an instance of AVPlayer, not an instance of AVAudioPlayer. It looks like you want to be creating an AVAudioPlayer instead (as is indicated by your choice of that class for the actual player property on your class. AVAudioPlayer actually has the numberOfLoops property, while AVPlayer does not. For more information, see the documentation for AVAudioPlayer and AVPlayer.

AVPlayer doesn't have a numberOfLoops property. That is a property of `AVAudioPlayer. Don't ignore compiler warnings when you build your app.
Also, you defined _player to be an AVAudioPlayer but you alloc/init AVPlayer.
Change your code to:
NSError *error = nil;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://urlpath.wav"] error:&error];
if (player) {
[player setNumberOfLoops:-1];
[player prepareToPlay];
[player play];
self.player = player;
} else {
NSLog(#"Error create audio player: %#", error);
}

I think that the method you are calling doesn't exist (acceosing to your error).
Try: _player.numberOfLoops=-1

Related

Impossible to play a video with AVPlayer

I've been through all the posts in StackOverflow but I can't get a full example of how to get a video from the bundle and reproduce it in a controller.
I've tried many things but my screen appeared blank or black.
In the last amend, my video remains like this:
no error is thrown on the log. This is my code:
.h (please ignore the name of the controller, it will be finally a camera)
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#interface NativeCameraViewController : UIViewController
#end
.m
#import "NativeCameraViewController.h"
#interface NativeCameraViewController ()
#property (nonatomic, retain) AVPlayerViewController *avPlayerViewcontroller;
#end
#implementation NativeCameraViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = self.view;
NSString *stringPath = [[NSBundle mainBundle]pathForResource:#"video" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:stringPath];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = [AVPlayer playerWithURL:fileURL];
self.avPlayerViewcontroller = playerViewController;
[self resizePlayerToViewSize];
[view addSubview:playerViewController.view];
view.autoresizesSubviews = TRUE;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) resizePlayerToViewSize
{
CGRect frame = self.view.frame;
NSLog(#"frame size %d, %d", (int)frame.size.width, (int)frame.size.height);
self.avPlayerViewcontroller.view.frame = frame;
}
#end
and that's what I've got so far.
My video is correctly on the Bundle, called video.mov:
I've tried both linking and copying. Same result.
What am I doing wrong??
Thank you very much in advance.
I'd like to avoid using MPMoviePlayerController since it's deprecated in iOS9
It was actually my fault...
I was trying to reproduce an alias.
When you have 20.000 videos and 30.000 resources this things happen.
Instead of using AVPlayer, use MPMoviePlayerController class:
NSString *path = [[NSBundle mainBundle] pathForResource:#"video" ofType:#"mov"];
NSURL *url = [NSURL fileURLWithPath:path];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player play];

Sound not playing in iOS Simulator. No errors

I have constructed some code that is supposed to play audio when a button is clicked. There are no errors in the code, but when I open the iOS Simulator and press the button, no sound plays. The button does the tap animation, but nothing happens.
I have both the AVFoundation and AudioToolbox frameworks.
I also have my audio file in resources.
I am not using breakpoints.
The button is set to use the 'playAudio:' first responder upon Touch Up Inside.
Here is my 'ViewController.h' file:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
- (IBAction)playAudio:(id)sender;
#end
And here is my 'ViewController.m' file:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)playAudio:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"AudioFile" ofType:#"wav"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
sleep(5);
};
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The audio segment is about 3 seconds.
Any help is much appreciated!
Thanks!
I am quite new to ios but I had the same problem and was able to fix it.
I simply declared my AVAudioPlayer variable as a global variable and called
[audioPlayer prepareToPlay];
before
[audioPlayer play];
Just calling prepareToPlay didnt fix it, I had to declare the global variable to for it to work.
Not sure why that makes a difference to the Simulator but it works now.
Play sound/music file in objective-c,
I had the same problem and was able to fix it with below code.
.h file
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
- (IBAction)actionOnPlayButton:(id)sender;
- (IBAction)actionOnStopButton:(id)sender;
- (void)playSound ;
#end
.m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
AVAudioPlayer *audioPlayer;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Play Music";
}
-(void)playSound{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"sound10"
ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1;
[audioPlayer prepareToPlay];
[audioPlayer play];
}
- (IBAction)actionOnPlayButton:(id)sender {
[self playSound];
}
****Used above lines of code, it will work fine...
Note:- Don't forget to import following framework:-**
#import
#import

Identical Functions, both get executed, no Sound in one

I'v googled the problem but couldn't find a similar case.
I've used this tutorial to play the testSound.mp3 - File when hitting a button on the iPad-Simulator:
http://mobileorchard.com/easy-audio-playback-with-avaudioplayer/
It works that way (it plays the sound), if the playSound-Method is in my ViewController.m, but not in my Sound.m (which has a identical method).
The Code gets executed (NSLog says: "Sound.m playSound executed"), but there is no sound at all.
I'd really appreciate some help here, guess I'm totally stuck... :(
Best regards,
- Teapot
// ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import "Sound.h"
#interface ViewController : UIViewController {
AVAudioPlayer *audioPlayer;
}
- (IBAction)pressButton:(id)sender;
- (void)playSound: (NSString*) soundFile volume : (NSInteger) volume repeats : (NSInteger) repeats;
#end
// ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading thea view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)pressButton:(id)sender {
NSLog (#"Method: pressButton");
[self playSound: #"testSound.mp3" volume: 2 repeats: 2 url : url]; //It works!
Sound *tempSound = [[Sound alloc] init];
[tempSound playSound: #"testSound.mp3" volume: 2 repeats: 2]; // Doesn't work. -> Says "Sound.m playSound executed", but there is no Sound.
}
- (void)playSound: (NSString*) soundFile volume : (NSInteger) volume repeats : (NSInteger) repeats {
NSLog(#"ViewControler playSound");
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil){
NSLog([error description]);
NSLog(#"ViewController.m playSound NOT executed");
}
else{
[audioPlayer play];
NSLog(#"ViewController.m playSound executed");
}
}
#end
// Sound.h
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#interface Sound : NSObject {
AVAudioPlayer *audioPlayer;
}
- (void) playSound: (NSString*) soundFile volume : (NSInteger) volume repeats : (NSInteger) repeats;
#end
// Sound.m
#import "Sound.h"
#implementation Sound
- (void)playSound: (NSString*) soundFile volume : (NSInteger) volume repeats : (NSInteger) repeats {
NSLog(#"Sound playSound");
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil){
NSLog([error description]);
NSLog(#"Sound.m playSound NOT executed");
}
else{
[audioPlayer play];
NSLog(#"Sound.m playSound executed");
}
}
#end
There are some inconsistencies in your code: playSound: has an NSString parameter, but AVAudioPlayer inside that method uses a NSURL. Then you set numberOfLoops = -1 (which means infinite repetition) instead of numberOfLoops = repeat.
But the main problem is that here (assuming that you compile with "Automatic Reference Counting")
Sound *tempSound = [[Sound alloc] init];
[tempSound playSound: #"testSound.mp3" volume: 2 repeats: 2];
the tempSound object is deallocated when the pressButton: is left, because no strong references to that object exist anymore.
If you add an instance variable (or property) sound to the view controller class, and assign to that
sound = [[Sound alloc] init];
[sound playSound: #"testSound.mp3" volume: 2 repeats: 2];
then it should work as expected.
Alternatively, you could prevent the Sound object from being deallocated too early by maintaining a "self reference" inside the object, which is removed only when the sound has finished playing:
#interface Sound () <AVAudioPlayerDelegate>
#property(strong, nonatomic) AVAudioPlayer *audioPlayer;
#property(strong, nonatomic) Sound *selfRef;
#end
#implementation Sound
- (void)playSound:(NSString *)soundFile volume:(NSInteger)volume repeats:(NSInteger)repeats
{
NSLog(#"Sound playSound");
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:soundFile withExtension:nil];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
if (self.audioPlayer == nil) {
NSLog(#"%#", [error description]);
NSLog(#"Sound.m playSound NOT executed");
} else{
self.audioPlayer.numberOfLoops = repeats;
self.audioPlayer.delegate = self;
[self.audioPlayer play];
self.selfRef = self; // self reference to avoid deallocation
NSLog(#"Sound.m playSound executed");
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
self.selfRef = nil; // remove self reference
}
#end
Of course, you shouldn't do this with "infinite repetition"!

iOS 5 movie player

Hi everyone i got a problem to play a video in iOS 5. When I'm building the program i get a warning "local declaration of hides instance variables" and when i run the program and tap the button i get a "SIGABRT". And i don't know what to do, all the help i can get is aprriciated!!
Thx in advance
MY .h file
#import <MediaPlayer/MediaPlayer.h>
#interface MainViewController : UIViewController {
MPMoviePlayerController *moviePlayer;
}
-(IBAction)Video:(id)sender;
#end
MY .m file
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
-(IBAction)Video:(id)sender {
MPMoviePlayerController *moviePlayer;
NSString *path = [[NSBundle mainBundle] pathForResource:#"big-buck-bunny-clip" ofType:#"m4v"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer setControlStyle:MPMovieControlStyleDefault];
[moviePlayer.view setFrame: self.view.bounds];
[self.view addSubview: moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
local declaration of hides instance variables
remove the first line of your -(IBAction)Video:(id)sender method.
MPMoviePlayerController *moviePlayer; //this line;
because you already have declared such variable in your .h file.
Means you declared the same variable names two times, one in .h file and another in .m file.

EXC_BAD_ACCESS when I change moviePlayer contentURL

In few words, my application is doing that :
1) My main view (RootViewController) has a buton when I tap on it, it displays the player (PlayerViewController) :
2) In my Player, I initialize the video I want to play
-> It's working good, my movie is display
My problem :
When I go back to my main view :
And I tap again on the button, I get a *Program received signal: “EXC_BAD_ACCESS”.*
If I comment self.player.contentURL = [self movieURL]; it's working, but when I let it, iI have this problem.
I read that it's due to null pointer or memory problem but I don't understand why it's working the first time and not the second time. I release my object in dealloc method.
Thanks for your help !
Bruno.
Here is my code :
Root View Controller
RootViewController.h
#import <UIKit/UIKit.h>
#import "PlayerViewController.h"
#interface RootViewController : UIViewController {
IBOutlet UIButton * myButton;
}
#property (nonatomic,retain) IBOutlet UIButton * myButton;
-(IBAction)displayPlayer:(id)sender;
- (void) returnToRoot: (PlayerViewController *) controller;
#end
RootViewController.m
#import "RootViewController.h"
#implementation RootViewController
#synthesize myButton;
-(IBAction)displayPlayer:(id)sender
{
PlayerViewController *playerViewController = [[PlayerViewController alloc] initWithNibName:#"PlayerViewController" bundle:nil];
playerViewController.delegate = self;
playerViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: playerViewController animated: YES];
[playerViewController release];
}
- (void) returnToRoot: (PlayerViewController *) controller
{
[self dismissModalViewControllerAnimated: YES];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
}
#end
Player View Controller
PlayerViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MPMoviePlayerController.h>
#protocol PlayerViewControllerDelegate;
#interface PlayerViewController : UIViewController {
UIView *viewForMovie;
MPMoviePlayerController *player;
}
#property (nonatomic, assign) id <PlayerViewControllerDelegate> delegate;
#property (nonatomic, retain) IBOutlet UIView *viewForMovie;
#property (nonatomic, retain) MPMoviePlayerController *player;
- (NSURL *)movieURL;
-(IBAction)goBackToRoot:(id)sender;
#end
#protocol PlayerViewControllerDelegate
- (void) returnToRoot: (PlayerViewController *) controller;
#end
PlayerViewController.m
#import "PlayerViewController.h"
#implementation PlayerViewController
#synthesize player;
#synthesize viewForMovie;
#synthesize delegate;
- (void)dealloc {
[super dealloc];
[player release];
[viewForMovie release];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"viewDidLoad");
self.player = [[MPMoviePlayerController alloc] init];
[self.player autorelease];
self.player.view.frame = self.viewForMovie.bounds;
self.player.view.autoresizingMask =
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
[self.viewForMovie addSubview:player.view];
self.player.contentURL = [self movieURL];
[self.player play];
}
-(NSURL *)movieURL
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath =
[bundle
pathForResource:#"myVideo"
ofType:#"mp4"];
if (moviePath) {
return [NSURL fileURLWithPath:moviePath];
} else {
return nil;
}
}
-(IBAction)goBackToRoot:(id)sender{
[self.delegate returnToRoot: self];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
#end
Problem
The second time I call "displayPlayer" I had the EXC_BAD_ACCESS
I solved it !!!
I look on the MPMoviePlayerController to see what kind of variable is contentURL
(NSURL *)contentURL
It means I have also to liberate it.
I do that in my dealloc method putting a nil value:
-(void) dealloc {
[super dealloc];
self.player.contentURL = nil;
[player release];
[viewForMovie release];
}
If I comment self.player.contentURL =
[self movieURL]; it's working, but
when I let it, iI have this problem.
In that case, how is contentURL declared? Does the #property definition include copy or retain?