gpt4 book ai didi

ios - 在iPhone锁定屏幕上保持音频播放

转载 作者:行者123 更新时间:2023-12-03 02:07:18 25 4
gpt4 key购买 nike

我正在使用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

最佳答案

启用任何背景模式。

关于ios - 在iPhone锁定屏幕上保持音频播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27074822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com