2011-04-01 90 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。可悲的是隻有硬件按鈕可以控制這個音量。