保持音频在iphone锁屏中播放

时间:2014-11-22 06:44:26

标签: ios audio background locking

我正在使用xcode 6.1,我仍然是一个菜鸟。

我从一个简单的音频播放器项目中学到了一些基础知识。我需要帮助在手机屏幕锁定时保持音频播放。

这是我的ViewController.m代码

//
//  ViewController.m
//  AudioPlayerTemplate
//
//  Created by ymc-thzi on 13.08.13.
//  Copyright (c) 2013 ymc-thzi. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
self.audioPlayer = [[YMCAudioPlayer alloc] init];
[self setupAudioPlayer:@"audiofile"];
}

/*
 * Setup the AudioPlayer with
 * Filename and FileExtension like mp3
 * Loading audioFile and sets the time Labels
 */
- (void)setupAudioPlayer:(NSString*)fileName
{
//insert Filename & FileExtension
NSString *fileExtension = @"mp3";

//init the Player to get file properties to set the time labels
[self.audioPlayer initPlayer:fileName fileExtension:fileExtension];
self.currentTimeSlider.maximumValue = [self.audioPlayer getAudioDuration];

//init the current timedisplay and the labels. if a current time was stored
//for this player then take it and update the time display
self.timeElapsed.text = @"0:00";

self.duration.text = [NSString stringWithFormat:@"-%@",
                      [self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration]]];

}

/*
 * PlayButton is pressed
 * plays or pauses the audio and sets
 * the play/pause Text of the Button
 */
- (IBAction)playAudioPressed:(id)playButton
{
[self.timer invalidate];
//play audio for the first time or if pause was pressed
if (!self.isPaused) {
    [self.playButton setBackgroundImage:[UIImage imageNamed:@"audioplayer_pause.png"]
                               forState:UIControlStateNormal];

    //start a timer to update the time label display
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                  target:self
                                                selector:@selector(updateTime:)
                                                userInfo:nil
                                                 repeats:YES];

    [self.audioPlayer playAudio];
    self.isPaused = TRUE;

    } else {
    //player is paused and Button is pressed again
    [self.playButton setBackgroundImage:[UIImage imageNamed:@"audioplayer_play.png"]
                               forState:UIControlStateNormal];

    [self.audioPlayer pauseAudio];
    self.isPaused = FALSE;
    }
}

/*
 * Updates the time label display and
 * the current value of the slider
 * while audio is playing
 */
- (void)updateTime:(NSTimer *)timer {
//to don't update every second. When scrubber is mouseDown the the slider will not set
if (!self.scrubbing) {
    self.currentTimeSlider.value = [self.audioPlayer getCurrentAudioTime];
}
self.timeElapsed.text = [NSString stringWithFormat:@"%@",
                         [self.audioPlayer timeFormat:[self.audioPlayer        getCurrentAudioTime]]];

self.duration.text = [NSString stringWithFormat:@"-%@",
                      [self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration] -    [self.audioPlayer getCurrentAudioTime]]];
}

/*
 * Sets the current value of the slider/scrubber
 * to the audio file when slider/scrubber is used
 */
- (IBAction)setCurrentTime:(id)scrubber {
//if scrubbing update the timestate, call updateTime faster not to wait a second and dont   repeat it
[NSTimer scheduledTimerWithTimeInterval:0.01
                                 target:self
                               selector:@selector(updateTime:)
                               userInfo:nil
                                repeats:NO];

[self.audioPlayer setCurrentAudioTime:self.currentTimeSlider.value];
self.scrubbing = FALSE;
}

/*
 * Sets if the user is scrubbing right now
 * to avoid slider update while dragging the slider
 */
- (IBAction)userIsScrubbing:(id)sender {
self.scrubbing = TRUE;
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:0)

启用任何后台模式。

enter image description here