IOS AVAudioPlayer的最大音量级别?

时间:2012-02-14 15:06:23

标签: iphone ios avaudioplayer volume

我使用AVAudioPlayer播放.caf文件。我的问题是:它不够响亮我的目的。由于iPhone的振铃声响亮得多,我想知道如何归档这个音量。 我已经尝试过操作.caf文件,但没有取得真正的成功。有什么想法吗?

到目前为止我使用的代码:

    AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume: 0.8];
[appSoundPlayer setDelegate: self];
    [appSoundPlayer play];

编辑:

[appSoundPlayer setVolume: 1.0];

THX 马丁

2 个答案:

答案 0 :(得分:13)

您始终可以将音量设置为实际最大值,例如:

[appSoundPlayer setVolume: 1.0];

除此之外,您可能需要自己编辑文件。

答案 1 :(得分:8)

马丁,你确保系统音量达到最大值吗?如果没有,您希望使用MPMusicPlayerController将系统音量调高到最大值,而不仅仅是avaudioplayer最大值。您可以将其调高为声音片段的长度,然后将其设置回用户设置的原始音量。

// Import headers. Link to "MediaPlayer Framework"
#import <MediaPlayer/MediaPlayer.h>

//set up audio session
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"HsTR_soundEFX_2-1" ofType:@"mp3"]];
audioAlert= [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
audioAlert.delegate = self;
[audioAlert prepareToPlay];//audioAlert is an AVAudioPLayer declared in header

//and whereever you want to play the sound, call the lines below
float currentVolume=[MPMusicPlayerController applicationMusicPlayer].volume; //grab current User volume
[[MPMusicPlayerController applicationMusicPlayer] setVolume:1.0];//set system vol to max
[audioAlert play];
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(setVolumeBackToNormal)userInfo:nil repeats:NO];

//and the setVolumeBackToNormal function calls this line below
[[MPMusicPlayerController applicationMusicPlayer]setVolume:currentVolume];//returns volume to original setting