About MPMusicPlayerController and does AVAudioPlayer works in iPhoneSimulator? - ios-simulator

From (iPhone) Can I do AudioServicesPlaySystemSound Looping with scheduler?
MPMusicPlayerController doesn't work because it is about iPod music library and I can't add a music file to it since I'm doing an iPhone game and not interacting with the outside iPod library.
Does AVAudioPlayer works in iPhoneSimulator?

Check out following code its a simple AVAudioPlayer example and its work on iPhone simulator too ..
AVAudioPlayer *audioPlayer;
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/azan1.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];

Related

.icloud files on iCloud Drive with NSFileManager

I use theses function to read out the directory content of my app iCloud folder :
+(NSArray *)getiCloudFileList:(NSString *)icloudPath strMask:(NSString *)strMask {
NSError *error;
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:icloudPath error:&error];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF like[cd] %#", strMask];
return [directoryContent filteredArrayUsingPredicate:predicate];
}
This function works fine if the app creates the files. If I copy a file with the Mac OS X Finder into the app folder, for example test1.txt in the directoryContent Array I get the result .test1.txt.icloud.
Gives there another way to get the directory content of an app iCloud folder without the .icloud extension or how can I copy files to an app iCloud folder without the .icloud extension ?

AVAudioPlayer don't work in AVAudioSessionCategoryAmbient mode in iOS 6.0.1?

In my app, I use a MPMusicPlayerController play .mp3 as background music,
and an AVAudioPlayer play sound-effect, just like button press, and so on.
The code is like this:
// Initialization code here.
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AVAudioSession *session =[AVAudioSession sharedInstance];
//The background music and the sound-effect can play simultaneously
[session setCategory: AVAudioSessionCategoryAmbient error:nil];
[session setActive: YES error: nil];
m_sharedPlayer = [[MPMusicPlayerController applicationMusicPlayer] retain];
[m_sharedPlayer setShuffleMode: MPMusicShuffleModeSongs];
[m_sharedPlayer setRepeatMode: MPMusicRepeatModeAll];
[m_sharedPlayer setVolume:0.2];
// choose the first song
[m_sharedPlayer setQueueWithQuery: [MPMediaQuery songsQuery]];
[m_sharedPlayer play];
...
//when need play sound-effect, soundfilename is a NSString
NSData *data =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:soundfilename ofType:nil]];
AVAudioPlayer *audioplayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
audioplayer =1.0;
audioplayer.delegate =self;
[audioplayer prepareToPlay];
[audioplayer play];
...
audioplayer is release after it has finished play.
The code is work in iOS 5.0. But in iOS 6.0, everything changed.
AVAudioPlayer don't play sound anyway.
If I change this line:
[session setCategory: AVAudioSessionCategoryAmbient error:nil];
To:
[session setCategory: AVAudioSessionCategoryPlayback error: nil];
The AVAudioPlayer will play sound, but it will break the play session of MPMusicPlayerController...
How can I find a way to play AVAudioPlayer but without break the background music? Thanks a lot for your help.
OK. I find the solution finally. In iOS 6.0, apple provide a new function call setCategory:withOptions:. It work.
So the code is just like this:
AVAudioSession *session =[AVAudioSession sharedInstance];
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version <6.0) {
[session setCategory:AVAudioSessionCategoryAmbient error:nil];
}
else {
[session setCategory: AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error: nil];
}
Thanks.

How to log into website from iOS device?

Someone I work with created a website that he wants to use to provide a service. They run a php script when the user taps Login that checks username/password/etc
Now I want to write an app that would allow logging in and also retrieving data, as one would on the website. How can I pass the login info from the iOS device to that PHP script that is run on the website? And after I'm logged in, how can I check and retrieve the data?
Do I need to ask him for an API?
responseData = [[NSMutableData data] retain];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://www.yourwebsite.com/login.php"]];
NSString *params = [[NSString alloc] initWithFormat:#"user=myuser&pass=mypass"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

Objective C - ASP.NET Authentication

I am trying to login automatically into an ASP.NET website using NSMutableURLRequest. I am sending through all the post parameters I can think of, and it works well with other websites. (eg. Wordpress based). But I am unable to get it to work for ASP.net websites. Can someone please explain to me how I could go about this issue?
NSString *post = #"__LASTFOCUS=&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUJNjM4NDU2MzY5ZGT%2F7rz73weImm5JbYQQ4q2lRY3HUw%3D%3D&__EVENTVALIDATION=%2FwEWBQK9gI6LAwKY4eTfDQKmw8%2B%2FAQLerrfAAwKQxuCGAT0xfZqPEPwFCfa5fbrvTZXDSnbY&ctl00%24SideBarContent%24UserName=demo&ctl00%24SideBarContent%24Password=demo&ctl00%24SideBarContent%24LoginButton=Login&ctl00%24SideBarContent%24UserTimeZoneCrawler%24offset=02%3A00%2C0";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *postRequest = [[NSMutableURLRequest alloc] init];
[postRequest setURL:[NSURL URLWithString:#"http://asp.net/website/logon.aspx"]];
[postRequest setHTTPMethod:#"POST"];
[postRequest setValue:postLength forHTTPHeaderField:#"Content-Length"];
[postRequest setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[postRequest setHTTPBody:postData];
[postRequest setValue:#"loginCookie=UserName=demo; SessionContext=ydnwyyz5n50mt2zjn3yiarq0" forHTTPHeaderField:#"Cookie"];
Regards,
EZFrag
You might have to pass your username/password in with your URL.
[postRequest setURl:[NSURL URLWithString:#"http://<username>:<password>#asp.net/website/login.aspx"]];
I ended up with letting the users enter their usernames and passwords manually every time. I am still open to any suggestions though.
Regards,
EZFrag

AVAssetReader kills playback (in AVAudioPlayer)

I am using AVAssetReader to read ipod library asset audio data and render a waveform image. this takes place using code I have described in my answer to this question
this sometimes takes place while audio is being played by an instance of AVAudioPlayer.
regardless of wether the audio being played is the same asset that is being read, the moment i hit
[reader startReading];
the audio being played "fades out". (as if the AVAudioPlayer has somehow been told to stop playback). This is odd, as I am not actually playing the audio, just reading it.
I did a search on SO and found this possible solution however i have found that this does not appear to solve the problem.
note - I am able to have several instances of AVAudioPlayer playing, and starting these do not seem to interfere with each other - however
[reader startReading];
will even kill multiple simultaneous instances of AVAudioPlayer, causing them all to synchronously fade out.
any ideas?
answering my own question....
further searching on SO led me to implementing this alternate solution:
- (void)setupAudio {
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
}
this was gleaned from here
**EDIT **UPDATED****
I have since made this into a class that also pre-initialises the audio queue (useful in both simulator and device as it eliminates the startup lag from the playback of the first audio file.
you can find the point1sec.mp3 here: http://www.xamuel.com/blank-mp3s/
#import <AVFoundation/AVFoundation.h>
#import "AudioToolbox/AudioServices.h"
#interface sw_AVAudioPlayerSetup : NSObject
<AVAudioPlayerDelegate> {
}
+ (void)setupAudio ;
+ (void)setupSharedSession ;
#end
#implementation sw_AVAudioPlayerSetup
+ (void)setupSharedSession {
static BOOL audioSessionSetup = NO;
if (audioSessionSetup) {
return;
}
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
audioSessionSetup = YES;
}
+ (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
// delegate callback to release player
[player release];
}
+ (void)setupAudio {
[self setupSharedSession];
NSString *filepath = [[NSBundle mainBundle]
pathForResource:#"point1sec"
ofType:#"mp3"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filepath]) {
AVAudioPlayer* player = [[AVAudioPlayer alloc]
initWithContentsOfURL:
[NSURL fileURLWithPath:filepath]
error:nil];
player.delegate = (id <AVAudioPlayerDelegate>) self;
[player play];
}
}

Resources