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

查看:13
本文介绍了使用 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 播放的类型,只是为了让声音像以前一样通过扬声器播放!将以下代码行放入您的应用委托 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];

不要忘记在您的应用委托 .h 文件中添加包含(如果尚未这样做,显然您还需要导入 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天全站免登陆