I have this app that has several view controllers. In the app delegate, I set it so that as soon as the app finishes launching, the background music starts. However, on another view controller, I have this button that plays this video. My problem is that when I play the movie, the background audio overlaps with the movie. My question is, how do I stop the music when I play the movie and play the music after the movie ends.
Here is my app_delegate.h:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface App_Delegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
Here is my App_Delegate.m
#import "App_Delegate.h"
#import "RootViewController.h"
#implementation App_Delegate
#synthesize window;
#synthesize navigationController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
{NSString* soundFilePath = [[NSBundle mainBundle] pathForResource:#"beethoven_sym_5_i" ofType:#"mp3"];
NSURL* soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops=-1;
[player play];
}
// Override point for customization after application launch.
// Set the navigation controller as the window's root view controller and display.
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}
#end
My MovieViewController.h:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#interface MovieViewController : UIViewController {
IBOutlet UIScrollView *sesamescroller;
}
- (IBAction)playsesamemovie:(id)sender;
#end
Finally, my MovieViewController.m
#import "MovieViewController.h"
#interface MovieViewController ()
#end
#implementation MovieViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
- (void)viewDidUnload
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)playsesamemovie:(id)sender {
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"How to make Sesame chicken" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
- (void)dealloc {
[sesamescroller release];
[super dealloc];
}
#end
The code you show has a local variable pointing to the player object. To control the player, other code needs to be able to find it. Like this:
In App_Delegate.h:
#property (strong) AVAudioPlayer *player;
in App_Delegate.m: (where did this underbar come from? Most unconventional!)
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
self.player.numberOfLoops=-1;
[self.player play];
Then, wherever you want to control it:
[((App_Delegate *)([UIApplication sharedApplication].delegate)).player pause];
// ...
[((App_Delegate *)([UIApplication sharedApplication].delegate)).player play];
set scalling mode to your player
for Paused :
[moviePlayerController setScalingMode:MPMoviePlaybackStatePaused];
for Stopped:
[moviePlayerController setScalingMode:MPMoviePlaybackStateStopped];
If possible on iOS, you can use scriptable feature to send message to mute. I did this sometime back for Mac OS X where I used to control iTunes from another app.
First add start to songs in application delegate
.h application delegate
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate,AVAudioPlayerDelegate>
{
AVAudioPlayer *myAudioPlayer;
NSDictionary *config;
NSMutableArray *ARRAY;
}
-(void)stop;
#property(retain,nonatomic) NSDictionary *config;
#property (nonatomic, retain) AVAudioPlayer *myAudioPlayer;
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#end
.m application delegate
#implementation AppDelegate
{
AVAudioPlayer* audioPlayer;
}
#synthesize myAudioPlayer;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
audioPlayer.delegate = self;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
audioPlayer.numberOfLoops = 1;
audioPlayer.delegate=self;
[audioPlayer play];
}
-(void)stop
{
[audioPlayer stop];
}
-(void)star
{
[audioPlayer play];
}
when use required start and stop background music in application
directly call this method -start and -stop
..it really work
Related
playerItem=[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.radio.com.lk/y-fm/"]]];
player=[AVPlayer playerWithPlayerItem:playerItem] ;
[player play];
It's working on Simulator but in the device, it's not. In the console,
I have the following error:
CredStore - performQuery - Error copying matching creds.
Error=-25300, query={
class = inet;
"m_Limit" = "m_LimitAll";
"r_Attributes" = 1;
sync = syna; }
Can anyone help me with a clue?
Please add below code in your app delegate. It may help you
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[AVAudioSession sharedInstance] setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&activationError];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&setCategoryError];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
My answer is for you
ViewController.h
#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) AVPlayerViewController *playerViewController;
#property (nonatomic,strong)AVAudioPlayer *player;
- (IBAction)actionPlay:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize player;
#synthesize playerViewController;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)actionPlay:(id)sender {
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:yourURL];
AVPlayer* playVideo = [[AVPlayer alloc] initWithPlayerItem:playerItem];
playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = playVideo;
playerViewController.player.volume = 0;
playerViewController.view.frame = self.view.bounds;
[self.view addSubview:playerViewController.view];
[playVideo play];
}
I currently have a SettingsViewController which handles starting/stopping music and adjusting the musics volume.
Is it possible to make the music stay on after unwinding the SettingsViewController? And after turning on the music and switching ViewControllers, can I re-open the SettingsViewController and turn off the music as well? Please let me know the limitations.
Here is the code for my SettingsViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface SettingsViewController : UIViewController <AVAudioPlayerDelegate>
{
AVAudioPlayer *audioPlayer;
IBOutlet UISwitch *Switch;
IBOutlet UISlider *Slider;
}
-(IBAction)Switch:(UISwitch *)sender;
-(IBAction)Slider:(UISlider *)sender;
#end
And here is the code for my SettingsViewController.m
#import "SettingsViewController.h"
#interface SettingsViewController ()
#end
#implementation SettingsViewController
-(IBAction)Switch:(UISwitch *)sender{
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
if(sender.tag == 0){
if(sender.on){
[standardDefaults setObject:#"On" forKey:#"keyName"];
//choosing and setting the music file
NSString *music = [[NSBundle mainBundle]pathForResource:#"bgmusic1" ofType:#"mp3"];
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL];
audioPlayer.delegate = self;
audioPlayer.numberOfLoops= -1; //sets the music to loop infinitely
[audioPlayer play]; //plays the music
} else if (sender.on == 0){
[standardDefaults setObject:#"Off" forKey:#"keyName"];
[audioPlayer stop]; //stops the music
}
}
[standardDefaults synchronize];
}
-(IBAction)Slider:(UISlider *)sender{
audioPlayer.volume = sender.value / 100.0; //will adjust the volume of the music according the slider value
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
Let me know what needs to be modified!
Do that in your AppDelegate.
To access it from anywhere, import it's .h file in your ViewControllers .m file and access it with
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
Make the audioplayer to a property in your AppDelegate.h to allow public access (for e.g. your other viewControllers)
#interface AppDelegate: NSAppDelegate
{
IBOutlet UISwitch *Switch;
IBOutlet UISlider *Slider;
}
#property AVAudioPlayer *audioPlayer; // <------
-(IBAction)Switch:(UISwitch *)sender;
-(IBAction)Slider:(UISlider *)sender;
#end
Then adjust each call of your audioplayer in the m file to adopt the change in your h file.
//Instead of [audioplayer doSomething] write...
[self.audioplayer doSomething];
// in modern objective-c you can use also
[_audioplayer doSomething];
To call your audioplayer from other ViewControllers then implement the first mentioned code and call your player like so
[appDelegate.audioplayer doSomething]
Because your audioPlayer will be release when its parent SettingsViewController released.
So you should add it to global and you can make it with Singleton Pattern.
Create BackgroundPlayer.h
#interface BackgroundPlayer: NSObject {
AVAudioPlayer *audioPlayer;
}
+ (id)sharedManager;
#end
and BackgroundPlayer.m
#implementation BackgroundPlayer
static BackgroundPlayer* sharedInstance = nil;
+ (BackgroundPlayer*) sharedInstance {
if (sharedInstance == nil) {
sharedInstance = [[super allocWithZone:NULL] init];
}
return sharedInstance;
}
- (id)init
{
self = [super init];
return self;
}
-(void)playAudio:(NSString *) audioPath
{
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:audioPath] error:NULL];
audioPlayer.delegate = self;
audioPlayer.numberOfLoops= -1; //sets the music to loop infinitely
[audioPlayer play]; //plays the music
}
-(void)setVolum:(float) volum {
if (audioPlayer) {
audioPlayer.volume = sender.value / 100.0;
}
}
Then you just call
NSString *music = [[NSBundle mainBundle]pathForResource:#"bgmusic1" ofType:#"mp3"];
[[BackgroundPlayersharedInstance] playAudio:music];
I have an online music player. I want to add a feature to it that if the song is being played and a call (incoming or outgoing) is made, it should pause the music which call is going on and after the call is disconnected, the music should start again.
here is the code I have:
//
// FirstViewController.m
#import "FirstViewController.h"
CM_EXPORT const CMTime kCMTimeZero;
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize metadatas;
#synthesize toggleButton;
#synthesize slider;
#synthesize mpVolumeView = _mpVolumeView;
#synthesize viewVolume;
- (void)viewDidLoad
{
//[super viewDidLoad];
//slider.transform = CGAffineTransformRotate(slider.transform,270.0/180*M_PI);
//[slider setMaximumValue:2];
//[slider setMinimumValue:0];
//[slider setSelected:YES];
//[[self mpVolumeView] setBackgroundColor:[UIColor clearColor]];
//MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame: [[self mpVolumeView] bounds]];
//[[self mpVolumeView] addSubview:myVolumeView];
//toggleIsOn =TRUE;
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
toggleIsOn=TRUE;
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:self.viewVolume.bounds] ;
[self.viewVolume addSubview:volumeView];
[volumeView sizeToFit];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(IBAction)playButtonPressed:(id)sender
{
if(toggleIsOn){
toggleIsOn=!toggleIsOn;
player = nil;
NSString *stringurl = #"";
stringurl = #"http://majestic.wavestreamer.com:6221/listen.pls";
NSURL *url = [NSURL URLWithString:stringurl];
asset = [AVURLAsset URLAssetWithURL:url options:nil];
playerItem = [AVPlayerItem playerItemWithAsset:asset];
player = [AVPlayer playerWithPlayerItem:playerItem];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[playerItem addObserver:self forKeyPath:#"timedMetadata" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:#"status" options:NSKeyValueObservingOptionNew context:nil];
[player play];
[self.toggleButton setImage:[UIImage imageNamed:#"reload.png"] forState:UIControlStateNormal];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
else {
[self.toggleButton setImage:[UIImage imageNamed:#"playMusic.png"] forState:UIControlStateNormal];
self->player.rate=0.0;
toggleIsOn=!toggleIsOn;
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
[playerItem removeObserver:self forKeyPath:keyPath];
if ([keyPath isEqualToString:#"status"]) {
AVPlayerItem *pItem = (AVPlayerItem *)object;
if (pItem.status == AVPlayerItemStatusReadyToPlay)
{
metadatas.text = #"";
}
}
if ([keyPath isEqualToString:#"timedMetadata"]) {
for (AVAssetTrack *track in playerItem.tracks) {
for (AVPlayerItemTrack *item in player.currentItem.tracks) {
if ([item.assetTrack.mediaType isEqual:AVMediaTypeAudio]) {
NSArray *meta = [playerItem timedMetadata];
for (AVMetadataItem *metaItem in meta) {
NSString *source = metaItem.stringValue;
metadatas.text = source;
}
}
}
}
}
[self.toggleButton setImage:[UIImage imageNamed:toggleIsOn ? #"playMusic.png" :#"stop.png"] forState:UIControlStateNormal];
}
-(IBAction)fbButtonPressed:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.facebook.com"];
if (![[UIApplication sharedApplication] openURL:url])
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
-(IBAction)inButtonPressed:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.linkedin.com"];
if (![[UIApplication sharedApplication] openURL:url])
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
-(IBAction)tweetButtonPressed:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.twitter.com"];
if (![[UIApplication sharedApplication] openURL:url])
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
-(IBAction) sliderChanged:(id)sender
{
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
#end
Also, the code for inserting volume view is there, then also there is no volume controller in the UI. Why so.?
// // FirstViewController.m
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#class AVPlayer;
#class AVPlayerItem;
#interface FirstViewController : UIViewController
{
UIView *viewVolume;
AVAsset *asset;
AVPlayerItem *playerItem;
AVPlayer *player;
NSURL *mURL;
MPVolumeView *_mpVolumeView;
IBOutlet UILabel *metadatas;
IBOutlet UIButton *toggleButton;
BOOL toggleIsOn;
IBOutlet UISlider *slider;
}
-(IBAction)playButtonPressed:(id)sender;
-(IBAction)fbButtonPressed:(id)sender;
-(IBAction)inButtonPressed:(id)sender;
-(IBAction)tweetButtonPressed:(id)sender;
-(IBAction) sliderChanged:(id)sender;
#property (strong, nonatomic) IBOutlet UISlider *slider;
#property (nonatomic, retain) IBOutlet MPVolumeView *mpVolumeView;
#property (nonatomic, retain) IBOutlet UILabel *metadatas;
#property (nonatomic, retain) IBOutlet UIButton *toggleButton;
#property (nonatomic, strong) IBOutlet UIView *viewVolume;
#end
am unable to start the music after call. Please help with possible solutions.
You need to add observer in FirstViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(resumePlayMusic) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void) resumePlayMusic
{
.......
}
And don't forget to remove observer.
Go to your appDelegate file and there you will find the UIApplicationDelegate methods automatically having been implemented for you.
Simply add your music pause and resume code in the methods and everything else will be handled accordingly. Just make sure that you can access your music instance player from the app delegate.
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
i am trying to make a simple audio board application, i am following the Apple documentation on the AVFoundationPlayer, but i keep getting this error (no visible #interface for 'NSBundle' declares the selector 'pathForRescource:ofType:' ) and i have searched for an hour, with no result.
my .h file
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#interface ViewController : UIViewController <AVAudioPlayerDelegate> {
}
//actions
-(IBAction)playSound1;
-(IBAction)playSound2;
-(IBAction)playSound3;
-(IBAction)playSound4;
#property (nonatomic, strong) AVAudioPlayer *player;
#end
my .m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize player;
- (void)viewDidLoad
{
NSString *soundFilePath1 =
[[NSBundle mainBundle] pathForRescource: #"trumpet"
ofType: #"mp3"];
NSURL *fileURL1 = [[NSURL alloc] initFileURLWithPath: soundFilePath1];
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL1 error:nil];
[fileURL1 release];
self.player= newPlayer;
[newPlayer release];
[player prepareToPlay];
[player setDelegate: self];
[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
If i can provide any other details i would be happy to help and thank you for reading.
It's a typo. You have written pathForRescource: and it is pathForResource:ofType:.
- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension
The following code plays a song from the user's music library. Works fine on devices running iOS6, but I get no sound at all on devices running iOS5. What am I doing wrong? A search for AVAudioPlayer issues on iOS5 doesn't turn up much.
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.songUrl error:nil];
[self.audioPlayer setNumberOfLoops:-1];
[self.audioPlayer play];
self.songUrl is valid. (ipod-library://item/item.m4a?id=557492601628322780)
This is how I have implemented in my apps compatible with both iOS 5.0 and iOS 6.0
In your *.h file
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#interface v1MusicPicController : UIViewController <MPMediaPickerControllerDelegate, AVAudioPlayerDelegate>
{
MPMusicPlayerController *musicPlayer;
MPMediaItemCollection *userMediaItemCollection;
}
#property (nonatomic, retain) MPMusicPlayerController *musicPlayer;
#property (nonatomic, retain) MPMediaItemCollection *userMediaItemCollection;
--------- In your *.m file
#implementation v1MusicPicController
#synthesize musicPlayer;
#synthesize userMediaItemCollection;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self setMusicPlayer: [MPMusicPlayerController applicationMusicPlayer]];
// By default, an application music player takes on the shuffle and repeat modes
// of the built-in iPod app. Here they are both turned off.
[musicPlayer setShuffleMode: MPMusicShuffleModeOff];
[musicPlayer setRepeatMode: MPMusicRepeatModeNone];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (IBAction) DoneButtonMusic:(id)sender
{
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = YES; // this is the default
[self presentModalViewController:mediaPicker animated:YES];
}
// To learn about notifications, see "Notifications" in Cocoa Fundamentals Guide.
- (void) registerForMediaPlayerNotifications {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver: self
selector: #selector (handle_NowPlayingItemChanged:)
name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object: musicPlayer];
[notificationCenter addObserver: self
selector: #selector (handle_PlaybackStateChanged:)
name: MPMusicPlayerControllerPlaybackStateDidChangeNotification
object: musicPlayer];
[musicPlayer beginGeneratingPlaybackNotifications];
}
- (void) updatePlayerQueueWithMediaCollection: (MPMediaItemCollection *) mediaItemCollection
{
if (userMediaItemCollection == nil)
{
//NSLog(#"Went here 4");
// apply the new media item collection as a playback queue for the music player
[self setUserMediaItemCollection: mediaItemCollection];
[musicPlayer setQueueWithItemCollection: userMediaItemCollection];
[musicPlayer play];
}
}
// Media picker delegate methods
- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
//NSLog(#"Went here 2");
// We need to dismiss the picker
[self dismissModalViewControllerAnimated:YES];
// Apply the chosen songs to the music player's queue.
[self updatePlayerQueueWithMediaCollection: mediaItemCollection];
}
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
// User did not select anything
// We need to dismiss the picker
[self dismissModalViewControllerAnimated:YES];
}
You may already know this but I use AVAudioPlayer for playing sounds that are in my app bundle.
#property (nonatomic, retain) AVAudioPlayer *myAudioPlayer1;
#synthesize myAudioPlayer1;
// ************************
// PLAY AUDIO
// ************************
[myAudioPlayer1 stop];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"funny_sound" ofType: #"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
myAudioPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[myAudioPlayer1 play];
I'm seeing the same behavior with ipod-library URLs. More specifically, initWithContentsOfURL sets an error code of -43, which is similar to a file not found error. Apparently either this was fixed, or support was added, in iOS 6, but I haven't found any official explanation.
Loading the audio into an NSData and using AVAudioPlayer initWithData also failed for me with a similar error (NSData code 256).
The best solution I've found so far is to use AVPlayer instead of AVAudioPlayer, but it's going to take some rework because the methods and properties are slightly different.