How we can play the audio file continue in sleep mode of iPhone

Apprentice
Posts: 5
Joined: 2009.02
Post: #1
Hi All,
I am using GBMusicTrack file to play my mp3 files and using the AudioToolBox framework. My problem is that when we put our iPhone device in sleep mode then the audio stops play. I have found the some code to set the property of AudioSession as below

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
AudioSessionSetActive(YES);

But i have not got where in my code i'll do it.

Is anybody there to help me out to resolve this issue.
Any help will be appreciated .
And also lemme know that is this because of iPhone OS 2.2 or later versions.
Please help me ........ Sad

Thanks,
Raj...
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #2
You can do it at any time. At initialization is a good time. First, be sure to call:

AudioSessionInitialize(NULL, NULL, NULL, NULL);

By the way, unless you have a very particular need to use GBMusicTrack, I would recommend you use AVAudioPlayer instead, since it does essentially the same thing, but is maintained by Apple.
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2009.02
Post: #3
Hi Another Jake,

First of all very thanks for such coordination...
I have made changes like initialize the AudioSession and other things like that
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_Audi oCategory, sizeof(sessionCategory), &sessionCategory);
AudioSessionSetActive(YES);

In my GBMusicTrack File but it still not playing audio in sleep mode.

Do I need to add some notification in applicationWillResignActive method of delegate class.

and if it not possible to integrate this with GBMusicTrack in that case can u please suggest how we can use AVAudioPlayer to play mp3 file .Its in AVFoundation framework that is supported by iPhone OS 2.2 or later versions.

Awaiting for your reply..
Thanks,

Raj...
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2009.02
Post: #4
HI AnotherJake,

First of all Thanks for your reply.....

I have initialize the AudioSession and other changes to play the audioFile in sleep mode as per in my Last thread in GBMusicTrack file.
But it still not working.
Can u please guide me how we could resolve it.
and if it is not possible with GBMUsicTrack then please guide me how we can play .mp3 file using AVAudioPlayer file.

Awaiting for your reply...

Thanks,
Raj....
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #5
Sorry, we have to approve new members' first few posts so I kept both of yours.

I would think that GBMusicTrack should still work during sleep, but I haven't tested it lately myself, since I now use AVAudioPlayer.

To try out AVAudioPlayer, it's really simple (I just hacked this together for you right now, so I didn't test it, but I think it should get you going):

Code:
#import <AVFoundation/AVFoundation.h>

AVAudioPlayer    *audioPlayer;

void PlayMusicTrack(NSString *musicTrack, BOOL shouldLoop)
{
    if (audioPlayer != nil)
    {
        [audioPlayer release];
        audioPlayer = nil;
    }
    
    NSString    *path = [[NSBundle mainBundle] pathForResource:[musicTrack stringByDeletingPathExtension] ofType:[musicTrack pathExtension]];
    if (path == nil)
    {
        NSLog(@"music track not found -- %@", musicTrack);
        return;
    }
    
    audioPlayer = [[AVAudioPlayer alloc]  initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
    [audioPlayer setDelegate:self];
    if (shouldLoop)
        [audioPlayer setNumberOfLoops:NSIntegerMax];
    [audioPlayer setVolume:musicVolume];
    [audioPlayer play];
}

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
    [audioPlayer play];
}

- (void)applicationWillResignActive:(NSNotification *)notification
{
    [audioPlayer stop];
}

- (void)applicationDidBecomeActive:(NSNotification *)notification
{
    PlayMusicTrack(myCurrentMusicTrack, YES);
}
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #6
Oh, I just remembered to mentioned that you must initialize the audio session *before* you set the session category. Doesn't matter where in your program you do it, but it has to be before calling any other AudioSession functions. ... important little detail Wink
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  iPhone/iPad Audio Glitch - Double Hiss/Static reapz 2 5,157 Dec 8, 2010 06:22 PM
Last Post: reapz
  iPhone/iPod Touch Audio Info musicbender 2 3,657 May 10, 2010 06:29 AM
Last Post: musicbender
  Core Audio mutlichannel audio tachyon 2 6,338 Mar 18, 2005 01:04 AM
Last Post: tachyon
  Physics and 3D engine, how to continue? DoG 2 3,188 Jan 9, 2003 06:58 AM
Last Post: DoG