MP3 playing using AVAudioPlayer not working on device

老子叫甜甜 提交于 2019-12-18 11:09:29

问题


I am testing my app on my 3GS iPhone with iOS 4.2

I am using the following code which plays a sound in my IBAction. It works perfectly in the simulator (both iPad and iPhone) - I hear the sound.

NSString *pathToMusicFile1 = [[NSBundle mainBundle] pathForResource:@"alarm" ofType:@"mp3"];
NSError *error;
alarmSound = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathToMusicFile1] error:&error]; 
NSLog(@"Song1 Loaded");
if (alarmSound == nil) {
    NSLog(@"Error playing sound");
} else {
    alarmSound.numberOfLoops = 20;
    alarmSound.volume = 1.0;
    [alarmSound play];
}

I have everything declared properly (headers and frameworks etc) as I can hear the sound in the simulator.

I have also checked that my phone is NOT in silent mode!! I hear other sounds from the generic iphone GUI. (example from a datepicker).

I have also cleaned all targets and deleted the app to reinstall.

Any ideas what's wrong?!


回答1:


So, I had this issue too - well I'm pretty sure it's the same one...

We've had an app in the app store for a year or so now and recently we needed to change a bit of content although nothing functional.

Suddenly, the sound stopped working - Both the simulator in the latest sdk version (version 4.0) AND on device too (again running iOS 4).

The code that always worked for us was this...

NSString *sound_file;
if ((sound_file = [[NSBundle mainBundle] pathForResource:@"track1" ofType:@"mp3"])){

NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.delegate = self;
[url release];

[audioPlayer prepareToPlay];
[audioPlayer play];

}

Finally, I found out that you now need to set the type of AVAudioSession playback just to get sound to play through the speaker as it already did! Put following line of code in your app delegate applicationDidFinishLaunching event handler...

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

Not forgetting to add the include in your app delegate .h file (obviously you need to import AVFoundation framework too if not already done so)...

#import <AVFoundation/AVAudioSession.h>

Hopefully this'll now make your sound play on device.

Don't get cuaght out by what I think might be a separate issue, and that's the sound still doesn't play in the simulator. I've seen other posts suggesting this is the case, but I'm not sure how widespread this is. I found out that if I selected iPad 3.2 as my simulator, it at least worked on that. The joy!

What seems crazy to me is that, surely this must be effecting loads of people and yet it's quite hard to find info or suggestions about something that should be quite a well known issue - after all, I've seen loads of posts on forums that don't seem to have been answered.

Anywayz, hope this helps somebody else.




回答2:


It is extremely important to have the "audioPlayer" variable as a property. Otherwise with ARC, it gets released before it gets a chance to play!

It took me half an hour and some googling for this A-ha moment.

My sound works perfectly now!




回答3:


My problem was that iOS was autoreleasing my AVAudioPlayer instance before the file got a chance to play. Strangely this only happened with MP3s and not AU files. Solution was to not set it as autorelease and then release it in the delegate method...

- (void)playSound:(NSString *)sound
{
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:sound ofType:@""]] error:NULL];
    audioPlayer.delegate = self;
    [audioPlayer play];
}

...then in the delegate method...

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    [player release];
}

...works great now.




回答4:


have you tried [NSURL URLWithString:pathToMusicFile1]; instead of fileURLWithPath:?



来源:https://stackoverflow.com/questions/4640880/mp3-playing-using-avaudioplayer-not-working-on-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!