【问题标题】:How to Set the Volume in Avplayer如何在 Avplayer 中设置音量
【发布时间】:2011-04-01 06:01:29
【问题描述】:

我有播放 iPod 库中歌曲的应用程序,我还必须在后台播放歌曲,所以我使用 avplayer。但我没有设置它的音量。是他们设置音量的任何方式。

【问题讨论】:

    标签: iphone


    【解决方案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];
    }
    

    【讨论】:

      【解决方案2】:

      您无法使用AVAudioPlayer 播放 ipod-Library 曲目。 AVAssetExportSession 在 IOS.5 中有一个 BUG,所以它无法从 mainbundle 中的 iPod 库中导出,所以如果你想播放 iPod 曲目,你坚持使用 AVPlayer。遗憾的是,只有硬件按钮可以控制音量。

      【讨论】:

        【解决方案3】:

        请检查Adjusting the volume of a playing AVPlayer

        如果你可以使用AVAudioPlayer,你可以很容易地使用它的属性volume

        【讨论】:

          【解决方案4】:

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

          在.h文件中写下这段代码,别忘了实现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];
          }
          

          希望对朋友有帮助。

          【讨论】:

          • 你怎么看,你的 updatevolume 方法每秒会被触发多少次?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-07
          • 2015-05-28
          相关资源
          最近更新 更多