2017-06-06 119 views
0

我见过有关AVAudioPlayer但诺内斯相当多的职位帮助:我不能播放声音了(即使它用于升级到8.3.2之前的Xcode工作)的ObjectiveC AVAudioPlayer量为0

这里是我的代码:

AVAudioPlayer *audioPlayerObj; 
AVAudioSession *session; 
[...] 
- (IBAction)playSound:(id)sender { 
audioPlayerObj.delegate = self; 

if(playButton.isSelected){   
    playButton.selected=NO; 
    (void) [audioPlayerObj stop]; 
} 
else{ 
    session = nil; 
    {NSError * _error; 

     session = [AVAudioSession sharedInstance]; 
     [session setCategory:AVAudioSessionCategoryPlayback 
         error:&_error]; 
     [session setActive:YES error:&_error]; 
     if(_error !=nil) 
     { NSLog(@"could not init the audio player session %@ ", _error); 
     } 

    } 

    NSString * tune =[NSString stringWithFormat:@"%@.wav",[pickerViewArray objectAtIndex:[pickerView selectedRowInComponent:0]]]; 
    NSString *path =[[NSBundle mainBundle] pathForResource:tune ofType:@""]; 

    NSURL * actualFilePath= [NSURL fileURLWithPath:path]; 
NSError *error; 

(void)[audioPlayerObj initWithContentsOfURL:actualFilePath error:&error]; 

    [audioPlayerObj prepareToPlay]; 


    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) 
    { 
    [audioPlayerObj setVolume:0.99 fadeDuration: 1]; 

    } 
    NSLog(@"syst volume : %f", [session outputVolume]); 
    NSLog(@"vol : %f\n",[audioPlayerObj volume]); 

if(error !=nil) 
{ NSLog(@"could not init the audio player %@ ", error); 
    audioPlayerObj=nil; 

} 
else 
{ 
    (void) [audioPlayerObj setNumberOfLoops:-1 ]; 
    (void) [audioPlayerObj play]; 
    NSLog(@"playing with vol : %f\n",[audioPlayerObj volume]); 
    [audioPlayerObj setVolume:0.99 fadeDuration: 1]; 
    playButton.selected=YES; 
} 
} 

`

输出是 syst volume : 0.476119 vol : 0.000000 playing with vol : 0.000000 关于如何提高该卷的任何暗示,将不胜感激。该视图还有一个MPVolumeView,但其上的任何操作只会改变系统音量。

+0

试试这个'AVAudioSession * audioSession = [AVAudioSession sharedInstance] [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];' –

+0

@pawan:上面的发布代码已经设置了AVaudiosession,我很乐意尝试你的建议,但它在哪里改变了一些东西? – toto

回答

0

的错误是在这里:

(void)[audioPlayerObj initWithContentsOfURL:actualFilePath error:&error]; 

应该是:

audioPlayerObj = [[AVAudioPlayer alloc] initWithContentsOfURL:actualFilePath error:&error];