使用AVAudioPlayer播放的MP3无法在设备上运行 [英] MP3 playing using AVAudioPlayer not working on device

查看:151
本文介绍了使用AVAudioPlayer播放的MP3无法在设备上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iOS 4.2在我的3GS iPhone上测试我的应用程序

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

我使用以下代码在我的IBAction中播放声音。它在模拟器(iPad和iPhone)中完美运行 - 我听到了声音。

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.

我还检查过我的手机没有处于静音模式!我听到了通用iphone GUI的其他声音。 (来自日期选择器的示例)。

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?!

推荐答案

所以,我也有这个问题 - 我很确定它是同一个......

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.

突然,声音停止工作 - 最新的sdk版本(版本4.0)和设备上的模拟器(再次运行iOS 4)。

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];

}

最后,我发现你现在需要设置AVAudioSession播放的类型只是为了通过扬声器播放声音,就像它已经做到的那样!在您的app appate applicationDidFinishLaunching事件处理程序中放置以下代码行...

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];

不要忘记在app delegate .h文件中添加include(显然你需要导入AVFoundation框架)如果还没有这样做的话)...

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.

不要因为我认为可能是一个单独的问题而变得明白,那声音仍然无法在模拟器中播放。我见过其他帖子暗示这是事实,但我不确定这是多么普遍。我发现如果我选择iPad 3.2作为我的模拟器,它至少可以解决这个问题。快乐!

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.

这篇关于使用AVAudioPlayer播放的MP3无法在设备上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆