2014-11-22 206 views
0

即時通訊使用xcode 6.1,我仍然是一個noob。保持音頻播放iPhone鎖屏

我從一個簡單的音頻播放器的項目中學習了一些基礎知識。當手機屏幕鎖定時,我需要幫助保持音頻播放。

這裏是我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 

回答

0

啓用任何背景模式。

enter image description here