2011-04-01 92 views
0

我有从iPod库播放歌曲的应用程序,我也必须在后台播放歌曲,所以我采取avplayer。但我不设定它的音量。是他们设置音量的任何方式。如何在Avplayer中设置音量

回答

0

要使用滑块和使用此代码,

在.h文件中编写代码和Don “别忘了实施AVAudio框架,

#import UIKit/UIKit.h> 
#import Foundation/Foundation.h> 
#import AVFoundation/AVFoundation.h> 

@interface volumechangerViewController : UIViewController <AVAudioPlayerDelegate> 
{ 
    IBOutlet UIButton *button; 
    IBOutlet UISlider *volumeslider; 
    NSTimer *volumetimer; 
    AVAudioPlayer *audioplayer; 

} 

-(IBAction) pushplay; 
@end 

而且在.m文件,

-(IBAction) pushplay{ 

    //NSError *err; 
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]; 
    audioplayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:filepath] error:NULL]; 
    audioplayer.delegate = self; 
    [audioplayer play]; 

    volumetimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(updatevolume) userInfo:nil repeats:YES]; 
} 

-(void) updatevolume{ 
    [audioplayer setVolume:volumeslider.value]; 
} 

希望这可以帮助朋友。

+1

您如何看待您的updatevolume方法每秒会触发多少次? – Andy 2012-05-26 12:26:24

1

里面音量滑块通知弹出 - 料斗中这会有所帮助:

- (IBAction)volume{ 
    MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame: CGRectMake(10, 37, 260, 20)] autorelease]; 

    UIAlertView *volumeAlert = [[UIAlertView alloc] initWithTitle:@"Volume" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

    [volumeView sizeToFit]; 
    [volumeAlert addSubview:volumeView]; 

    /* 
    for (UIView *view in [volumeView subviews]) { 
     if ([[[view class] description] isEqualToString:@"MPVolumeSlider"]) { 
      volumeViewSlider = view; 
     } 
    }*/ 

    [volumeAlert show]; 
    [volumeAlert release]; 
} 
1

您不能播放使用AVAudioPlayer iPod音乐库的轨道。 AVAssetExportSession在IOS.5中有一个BUG,所以它无法从主库中的iPod库导出,所以如果你想播放iPod曲目,你的卡住了AVPlayer。可悲的是只有硬件按钮可以控制这个音量。