I have four videos in four different view controllers and they randomly crash the app. Generally the first one I click (regardless of which it is) will work and then if I click the button to play any other video the app crashes. It does not matter which one I click first, generally. The app behaves strangely. Here is the error code I get:
2013-07-26 10:56:40.590 Capture Controller[6558:907]
-[ShoreViewController moviePlayBackDidFinish:]: unrecognized selector sent to instance 0x2088bb20 2013-07-26 10:56:40.592 Capture
Controller[6558:907] * Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[ShoreViewController
moviePlayBackDidFinish:]: unrecognized selector sent to instance
0x2088bb20'
* First throw call stack: (0x339c43e7 0x3b84e963 0x339c7f31 0x339c664d 0x3391e208 0x33915349 0x3422cb7f 0x3484fec7 0x3484d251
0x33915349 0x3422cb7f 0x34866557 0x3486916f 0x34253b85 0x342537dd
0x3422dcbb 0x348ef73f 0x34253b85 0x342537dd 0x3422dcbb 0x348f208d
0x348f4149 0x348f1f2d 0x348f3d59 0x348f05d9 0x34862bcb 0x3484ddc7
0x33915349 0x3422cb7f 0x3484fc5b 0x3484f6a7 0x3484c055 0x49e61
0x358be087 0x358be03b 0x358be015 0x358bd8cb 0x358bddb9 0x357e65f9
0x357d38e1 0x357d31ef 0x374c75f7 0x374c7227 0x339993e7 0x3399938b
0x3399820f 0x3390b23d 0x3390b0c9 0x374c633b 0x358272b9 0x26a2d
0x3bc7bb20) libc++abi.dylib: terminate called throwing an exception
Here is some code:
EBViewController.h:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import "ReaderViewController.h"
#interface EBViewController : UIViewController <ReaderViewControllerDelegate>
#property (strong, nonatomic) MPMoviePlayerController *movieEBPlayer;
- (IBAction)playEBFilm:(id)sender;
- (IBAction)readEBDocument:(id)sender;
#end
EBViewController.m
#import "EBViewController.h"
#interface EBViewController ()
#end
#implementation EBViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *EBFilmButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[EBFilmButton addTarget:self action:#selector(playEBFilm) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:EBFilmButton];
UIButton *readEBButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[readEBButton addTarget:self action:#selector(readEBDocument) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:readEBButton];
}
-(void)readEBDocument:(id)sender {
NSString *file = [[NSBundle mainBundle] pathForResource:#"Rig" ofType:#"pdf"];
ReaderDocument *document = [ReaderDocument withDocumentFilePath:file password:nil];
if (document != nil)
{
ReaderViewController *readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];
readerViewController.delegate = self;
readerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
readerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:readerViewController animated:YES];
}
}
-(void)playEBFilm:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"118409050" ofType:#"mp4"]; NSURL *url = [NSURL fileURLWithPath:path]; MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url]; [player play]; //- See more at: http://getsetgames.com/2009/12/20/iphonedev-advent-tip-20-how-to-play-a-video-with-mpmovieplayercontroller/#sthash.dznrF0UO.J8xsiXHT.dpuf
_movieEBPlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_movieEBPlayer];
_movieEBPlayer.controlStyle = MPMovieControlStyleDefault;
_movieEBPlayer.shouldAutoplay = YES;
[self.view addSubview:_movieEBPlayer.view];
[_movieEBPlayer setFullscreen:YES animated:YES];
}
- (void)dismissReaderViewController:(ReaderViewController *)viewController {
[self dismissModalViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
PebbleViewController.h:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import "ReaderViewController.h"
#interface PebbleViewController : UIViewController <ReaderViewControllerDelegate>
#property (strong, nonatomic) MPMoviePlayerController *moviePebblePlayer;
- (IBAction)playPebbleFilm:(id)sender;
- (IBAction)readPebbleDocument:(id)sender;
#end
PebbleViewController.m:
#import "PebbleViewController.h"
#interface PebbleViewController ()
#end
#implementation PebbleViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *pebbleFilmButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[pebbleFilmButton addTarget:self action:#selector(playPebbleFilm) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pebbleFilmButton];
UIButton *readPebbleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[readPebbleButton addTarget:self action:#selector(readPebbleDocument) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:readPebbleButton];
}
-(void)readPebbleDocument:(id)sender {
NSString *file = [[NSBundle mainBundle] pathForResource:#"Rig" ofType:#"pdf"];
ReaderDocument *document = [ReaderDocument withDocumentFilePath:file password:nil];
if (document != nil)
{
ReaderViewController *readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];
readerViewController.delegate = self;
readerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
readerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:readerViewController animated:YES];
}
}
-(void)playPebbleFilm:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"Pebble2" ofType:#"mov"]; NSURL *url = [NSURL fileURLWithPath:path]; MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url]; [player play]; //- See more at: http://getsetgames.com/2009/12/20/iphonedev-advent-tip-20-how-to-play-a-video-with-mpmovieplayercontroller/#sthash.dznrF0UO.J8xsiXHT.dpuf
_moviePebblePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePebblePlayer];
_moviePebblePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePebblePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePebblePlayer.view];
[_moviePebblePlayer setFullscreen:YES animated:YES];
}
- (void)dismissReaderViewController:(ReaderViewController *)viewController {
[self dismissModalViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The rest of the view controllers are basically the same, just with of course different names for the IBActions and moviePlayers.
Any advice would be much appreciated! Thanks!
I don't see a method named:
- (void) moviePlayBackDidFinish:(NSNotification*)notification
in any of the code snippets you've cut & pasted (although it's not obvious if either view controller is actually your "ShoreViewController" object, either)
If it doesn't exist in your code, that would explain the "unrecognized selector sent to instance" crash.
Related
I have On-line Radio app. Located on the first viewController (tableView) audio streams. Audio streams are made as a table. By clicking on the name of the stream screen opens with AVpalyer. The problem is that when opening the second stream, the ашкые stream is played simultaneously. How to make so that would be played if one thread, and the second was turned down?
avplayer:
#interface RadioViewController ()
#end
#implementation RadioViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *restorationId = self.restorationIdentifier;
NSString *streamId = _detail.label;
NSString* identifer = [restorationId stringByAppendingString: streamId];
NSLog(#"%#", identifer);
self.title = apptitle;
self.streamLabel.text = _detail.label;
//Определение потока
NSString *streamURL;
if (_detail.label == #"ПЕРВЫЙ"){
streamURL = streamURLpervyj;
} if (_detail.label == #"РОК") {
streamURL = streamURLrok;
} if (_detail.label == #"БИТ") {
streamURL = streamURLbit;
} if (_detail.label == #"ПО-РУССКИ") {
streamURL = streamURLporusski;
} if (_detail.label == #"ХИП-ХОП") {
streamURL = streamURLhiphop;
} if (_detail.label == #"РЕЛАКС") {
streamURL = streamURLrelax;
} if (_detail.label == #"РЕТРО") {
streamURL = streamURLretro;
}
if(![self connected]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Невозможно установить соединение" message:#"Проверьте соединение с интернетом" delegate:self cancelButtonTitle:#"ОК" otherButtonTitles: nil];
[alert show];
} else {
//Start stream
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:streamURL]];
[playerItem addObserver:self forKeyPath:#"timedMetadata" options:NSKeyValueObservingOptionNew context:nil];
music = [AVPlayer playerWithPlayerItem:playerItem];
[music play];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playButton:)
name:#"TogglePlayPause"
object:nil];
//Set Title View
titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 240, myScrollView.frame.size.width, 40)];
[self setStyle]; //Set style layers
titleView.text = #"Подключаюсь...";
CGFloat textLength = titleView.intrinsicContentSize.width;
[myScrollView addSubview: titleView];
[self.view addSubview:myScrollView];
if (textLength < 240) {
[self centerText];
}
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:#selector(timed) userInfo:nil repeats:YES];
}
http://dev.b4u.by/radio.jpg
You can decleare avplayer object "music" in your appdelegate then access it by this way
AppDelegate *mainDelegate = [UIApplication sharedApplication].delegate;
[mainDelegate.music pause];
mainDelegate.music = nil;
when you want to start new stream or before alloc init
StreamViewController.h
#interface APStreamsViewController : UIViewController <UITableViewDataSource>
{
AVPlayer *musicSegue;
}
StreamViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
if (indexPath) {
APStreamsData *item = [_data objectAtIndex:indexPath.row];
[segue.destinationViewController setDetail:item];
}
RadioViewController *music = [segue destinationViewController];
[music setMusic: musicSegue];
}
RadioViewController.h
#property (nonatomic, strong) AVPlayer *music;
And in RadioViewController change music to _music )))
I'm building a basic app for my school and can't figure out how to change the URL to be presented based on the button pressed. I have four buttons and currently have them all linked to temporary URLs, but when I run my app, the webview appears blank and my NSLog returns null for the URL. What am I doing wrong? Here is my code:
TWViewController.h:
#import <UIKit/UIKit.h>
#interface TWViewController : UIViewController
- (IBAction)gradesPressed:(id)sender;
- (IBAction)facPressed:(id)sender;
- (IBAction)newsPressed:(id)sender;
- (IBAction)sportsPressed:(id)sender;
#property (strong, nonatomic) NSURL *url;
#end
TWViewController.m:
#import "TWViewController.h"
#import "TWWebViewController.h"
#interface TWViewController ()
#end
#implementation TWViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)gradesPressed:(id)sender{
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:#"http://google.com"];
}
- (IBAction)facPressed:(id)sender {
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:#"http://yahoo.com"];
}
- (IBAction)newsPressed:(id)sender {
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:#"http://facebook.com"];
}
- (IBAction)sportsPressed:(id)sender {
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:#"http://apple.com"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//
// TWWebViewController *wbc = (TWWebViewController *)segue.destinationViewController;
//}
#end
TWWebViewController.h:
#import <UIKit/UIKit.h>
#interface TWWebViewController : UIViewController
{
UIWebView *webView;
NSString *website;
}
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#property (strong, nonatomic) NSURL *website;
#end
TWWebViewController.m:
#import "TWWebViewController.h"
#import "TWWebViewController.h"
#interface TWWebViewController ()
#end
#implementation TWWebViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:self.website];
[self.webView loadRequest:urlRequest];
NSLog(#"%#", website);
// Do any additional setup after loading the view.
}
I'm fairly new to iOS Development, so I'm not quite sure if I'm even on the right track. Any help would be appreciated, thanks!
You need to push your controller after initializing it: Also, your NSString is not an NSURL object so do this also in your TWWebViewController: NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:self.website]];
TWWebViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:
[NSURL URLWithString:self.website]];
[self.webView loadRequest:urlRequest];
}
TWViewController.m:
- (IBAction)sportsPressed:(id)sender
{
TWWebViewController *webView = [self.storyboard
instantiateViewControllerWithIdentifier:#"WebView"];
webView.website = #"http://apple.com";
[self.navigationController pushViewController:webView animated:YES];
}
Also don't forget to do this in your storyboard file:
Here is my code:
MainViewConroller.h
#import <UIKit/UIKit.h>
#import "FlipsideViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
AVAudioPlayer *audioPlayer;
NSArray *soundsArray;
}
#property(nonatomic, retain) AVAudioPlayer *audioPlayer;
#property(nonatomic, retain) NSArray *soundsArray;
-(void)prepareSounds;
- (IBAction)playSound:(UIButton *)sender;
#end
Here is my MainViewController.m:
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
#synthesize audioPlayer;
#synthesize soundsArray;
-(void)prepareSounds
{
NSString *filepath= [[NSBundle mainBundle] pathForResource:#"Sound" ofType:#"plist"];
self.soundsArray = [[NSArray alloc] initWithContentsOfFile:filepath];
}
- (void)stopAudio
{
if (audioPlayer!= nil) {
[audioPlayer stop];
//do some task for changing the Image i.e setting the default image
}
}
- (IBAction)playSound:(UIButton *)sender
{
UIButton *btn = (UIButton*)sender;
NSString *soundName = [soundsArray objectAtIndex:(btn.tag - 1)];
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:#"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *p = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
self.audioPlayer = p;
if([audioPlayer isPlaying])
{
[self stopAudio];
}
[self.audioPlayer play];
}
- (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.
}
#pragma mark - Flipside View
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)showInfo:(id)sender
{
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideViewController" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:controller animated:YES completion:nil];
}
#end
I have 3 buttons. In the 'Supporting Files' folder I have an array of strings with the name of the sound files I want to play, named 'Sound.plist', and in the 'Supporting Files' folder I have a folder named 'Sounds', which contains the sound files.
All of my buttons play the same sound, even when I delete the .plist file. It seems to always play the sound that is first in the 'Sounds' folder. Can someone please provide some insight, I feel like I have tried everything. Thanks!
I want to open links of uiwebview in to the safari browser my code is working perfectly if I implement shouldStartLoadWithRequest method in viewController but when I implement shouldStartLoadWithRequest in same class and set UIWebView's delegate to self it doesn't work it get halt in between and shows assembly level code with error EXC_BAD_ACCESS(code=2, address=0x9) my files are as follows
//content of ShowView.h file
#import <UIKit/UIKit.h>
#interface ShowView : UIView <UIWebViewDelegate> {
}
- (void) showViewFunction;
#property (nonatomic, assign) UIViewController *mainViewContObj;
#end
//content of ShowView.m file is :
#import "ShowView.h"
#implementation ShowView
- (void) showViewFunction {
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[aWebView setDelegate:self];
NSString *urlAddress = #"http://localhost/test/index.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
aWebView.delegate = self;
[aWebView loadRequest:requestObj];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[[[self mainViewContObj] view] addSubview:aWebView];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"In shouldStartLoadWithRequest method");
if ([[[request URL] absoluteString] isEqual:#"http://localhost/test/index.php"])
return YES;
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
#end
// Content of ViewController.h
#import "ViewController.h"
#import "ShowView.h"
#interface mnetViewController ()
#end
#implementation mnetViewController
- (void)viewDidLoad {
[super viewDidLoad];
MNETMobAd *bannerObj = [[MNETMobAd alloc] init];
bannerObj.mainViewContObj = self;
[bannerObj showAd];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Some times html page shows but when I click on link it opens in same UIWebview window, and not even going into the shouldStartLoadWithRequest method, am I doing anything wrong?
Your code isn't clear, might need more info's, but from what i see, the ShowView class is never instanciated, so it shouldn't even show.
you should make something like this i guess :
//mnetViewController.m
#import "mnetViewController.h"
#import "ShowView.h"
#interface mnetViewController ()
#end
#implementation mnetViewController
- (void)viewDidLoad {
[super viewDidLoad];
ShowView* theShowView = [[ShowView alloc] initWithFrame:CGRectMake(insert the frame you want your webview to have)];
theShowView.autoresizesSubviews = YES;
[self.view addSubview:theShowView];
[theShowView release];
MNETMobAd *bannerObj = [[MNETMobAd alloc] init];
bannerObj.mainViewContObj = self;
[bannerObj showAd];
}
now for the ShowView class, try something like this :
//ShowView.h
#import <UIKit/UIKit.h>
#interface ShowView : UIView <UIWebViewDelegate> {
}
#end
//ShowView.m
#import "ShowView.h"
#implementation ShowView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
aWebView.scalesPageToFit = YES;
[aWebView setDelegate:self];
[self addSubview:aWebView];
NSString *urlAddress = #"http://localhost/test/index.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
[aWebView release];
}
return self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"In shouldStartLoadWithRequest method for URL : %#",[request [URL absolutString]]);
if ([[[request URL] absoluteString] isEqual:#"http://localhost/test/index.php"])
return YES;
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
This should work, i didn't try it, i'll comeback tomorrow to try it if necessary.
I'm struggling for couple of days now to sort this thing out and simply just can't find the way. I want to play audio in background when app exits or when i click on link to go to Safari, but i just wont go to background mode. Please help.
FirstViewController.h file :
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVAudioPlayer.h>
#import <AVFoundation/AVFoundation.h>
#interface RygestopFirstViewController : UIViewController <AVAudioPlayerDelegate> {
IBOutlet UIButton *playButton;
IBOutlet UISlider *volumeSlider;
IBOutlet UISlider *progressBar;
IBOutlet UILabel *currentTime;
IBOutlet UILabel *duration;
AVAudioPlayer *player;
UIImage *playBtnBG;
UIImage *pauseBtnBG;
NSTimer *updateTimer;
BOOL inBackground;
}
- (IBAction)playButtonPressed:(UIButton*)sender;
- (IBAction)volumeSliderMoved:(UISlider*)sender;
- (IBAction)progressSliderMoved:(UISlider*)sender;
#property (nonatomic, retain) UIButton *playButton;
#property (nonatomic, retain) UISlider *volumeSlider;
#property (nonatomic, retain) UISlider *progressBar;
#property (nonatomic, retain) UILabel *currentTime;
#property (nonatomic, retain) UILabel *duration;
#property (nonatomic, retain) NSTimer *updateTimer;
#property (nonatomic, assign) AVAudioPlayer *player;
#property (nonatomic, assign) BOOL inBackground;
#end
FirstViewController.m code:
// amount to skip on rewind or fast forward
#define SKIP_TIME 1.0
// amount to play between skips
#define SKIP_INTERVAL .2
#implementation RygestopFirstViewController
#synthesize playButton;
#synthesize volumeSlider;
#synthesize progressBar;
#synthesize currentTime;
#synthesize duration;
#synthesize updateTimer;
#synthesize player;
#synthesize inBackground;
-(void)updateCurrentTimeForPlayer:(AVAudioPlayer *)p
{
currentTime.text = [NSString stringWithFormat:#"%d:%02d", (int)p.currentTime / 60, (int)p.currentTime % 60, nil];
progressBar.value = p.currentTime;
}
- (void)updateCurrentTime
{
[self updateCurrentTimeForPlayer:self.player];
}
- (void)updateViewForPlayerState:(AVAudioPlayer *)p
{
[self updateCurrentTimeForPlayer:p];
if (updateTimer)
[updateTimer invalidate];
if (p.playing)
{
[playButton setImage:((p.playing == YES) ? pauseBtnBG : playBtnBG) forState:UIControlStateNormal];
updateTimer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:#selector(updateCurrentTime) userInfo:p repeats:YES];
}
else
{
[playButton setImage:((p.playing == YES) ? pauseBtnBG : playBtnBG) forState:UIControlStateNormal];
updateTimer = nil;
}
}
- (void)updateViewForPlayerStateInBackground:(AVAudioPlayer *)p
{
[self updateCurrentTimeForPlayer:p];
if (p.playing)
{
[playButton setImage:((p.playing == YES) ? pauseBtnBG : playBtnBG) forState:UIControlStateNormal];
}
else
{
[playButton setImage:((p.playing == YES) ? pauseBtnBG : playBtnBG) forState:UIControlStateNormal];
}
}
-(void)updateViewForPlayerInfo:(AVAudioPlayer*)p
{
duration.text = [NSString stringWithFormat:#"%d:%02d", (int)p.duration / 60, (int)p.duration % 60, nil];
progressBar.maximumValue = p.duration;
volumeSlider.value = p.volume;
}
-(void)pausePlaybackForPlayer:(AVAudioPlayer*)p
{
[p pause];
[self updateViewForPlayerState:p];
}
-(void)startPlaybackForPlayer:(AVAudioPlayer*)p
{
if ([p play])
{
[self updateViewForPlayerState:p];
}
else
NSLog(#"Could not play %#\n", p.url);
}
- (IBAction)playButtonPressed:(UIButton *)sender
{
if (player.playing == YES)
[self pausePlaybackForPlayer: player];
else
[self startPlaybackForPlayer: player];
}
- (IBAction)volumeSliderMoved:(UISlider *)sender
{
player.volume = [sender value];
}
- (IBAction)progressSliderMoved:(UISlider *)sender
{
player.currentTime = sender.value;
[self updateCurrentTimeForPlayer:player];
}
- (void)dealloc
{
[super dealloc];
[playButton release];
[volumeSlider release];
[progressBar release];
[currentTime release];
[duration release];
[updateTimer release];
[player release];
[playBtnBG release];
[pauseBtnBG release];
}
#pragma mark AVAudioPlayer delegate methods
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)p successfully:(BOOL)flag
{
if (flag == NO)
NSLog(#"Playback finished unsuccessfully");
[p setCurrentTime:0.];
if (inBackground)
{
[self updateViewForPlayerStateInBackground:p];
}
else
{
[self updateViewForPlayerState:p];
}
}
- (void)playerDecodeErrorDidOccur:(AVAudioPlayer *)p error:(NSError *)error
{
NSLog(#"ERROR IN DECODE: %#\n", error);
}
// we will only get these notifications if playback was interrupted
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)p
{
NSLog(#"Interruption begin. Updating UI for new state");
// the object has already been paused, we just need to update UI
if (inBackground)
{
[self updateViewForPlayerStateInBackground:p];
}
else
{
[self updateViewForPlayerState:p];
}
}
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)p
{
NSLog(#"Interruption ended. Resuming playback");
[self startPlaybackForPlayer:p];
}
#pragma mark background notifications
- (void)registerForBackgroundNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(setInBackgroundFlag)
name:UIApplicationWillResignActiveNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(clearInBackgroundFlag)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
- (void)setInBackgroundFlag
{
inBackground = true;
}
- (void)clearInBackgroundFlag
{
inBackground = false;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Play", #"First");
self.tabBarItem.image = [UIImage imageNamed:#"Home"];
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
//Make sure we can recieve remote control events
- (BOOL)canBecomeFirstResponder {
return YES;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
playBtnBG = [[UIImage imageNamed:#"Player.png"] retain];
pauseBtnBG = [[UIImage imageNamed:#"Pause.png"] retain];
[playButton setImage:playBtnBG forState:UIControlStateNormal];
[self registerForBackgroundNotifications];
updateTimer = nil;
duration.adjustsFontSizeToFitWidth = YES;
currentTime.adjustsFontSizeToFitWidth = YES;
progressBar.minimumValue = 0.0;
// Load the the sample file, use mono or stero sample
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:#"Sound1" ofType:#"m4a"]];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
if (self.player)
{
[self updateViewForPlayerInfo:player];
[self updateViewForPlayerState:player];
player.numberOfLoops = 0;
player.delegate = self;
}
[fileURL release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
And, by the way, i want to run thin in tab bar application, so the background mode must be present always.
Here's what you're looking for: https://devforums.apple.com/message/264397 and set your background mode to 'App plays audio' in your app's .plist file.