Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在iphone锁屏中保持音频播放_Ios_Audio_Background_Locking - Fatal编程技术网

Ios 在iphone锁屏中保持音频播放

Ios 在iphone锁屏中保持音频播放,ios,audio,background,locking,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 "ViewCo

我用的是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

启用任何背景模式