누구나 나를 튜토리얼로 안내하거나 배경 음악을 재생할 실제 코드를 보여줄 수 있습니까? 나는 시작하고 멈출 수 있어야한다. 당신이 알고있는 임무가 아닙니다. 내 임무는 이미 끝났어. 그냥 음악을 추가하고 싶은데 어떻게하는지 모르겠다. 감사! http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.htmliPhone 배경 음악 플레이어
0
A
답변
2
당신은 AVAudioPlayer
//You have to import AvFoundation to use AVAudioPlayer
#import <AVFoundation/AVFoundation.h>
-(void) playMusicFile:(NSString *) songName
{
NSString *musicFile = [[NSBundle mainBundle] pathForResource:songName ofType:@"mp3"];
NSError *soundError = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicFile] error:&soundError];
if(self.audioPlayer == nil)
{
NSLog(@"%@",soundError);
}
else
{
//Delegation is optional but it helps you do stuff after song finished playing etc
[self.audioPlayer setDelegate:self];
//Set number of repeats, 0 is default plays once, negative values makes it play infinitely
[self.audioPlayer setNumberOfLoops:0];
//Prepare to play is not always necessary, but otherwise it can take time to play
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
}
}
//This is the delegate called after the song finished playing, you can use it to play other songs, or do other stuff
-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
...
}
이 중지 및 일시 중지 사용할 수 있습니다.
1
AVPlayer
및 AVAudioPlayer
작동합니다 :
//You can easily stop and pause using:
[self.audioPlayer stop]; //Stop doesn't work as you would expect. It doesn't set the current time to 0 but only undoes the setup you had with the audioplayer.
[self.audioPlayer pause];
//It is possible to call [self.audioPlayer play] after each method and playback will continue from where it left off.
은 더 많은 정보 참조를 참조하십시오
끝내 주셔서 고맙습니다. 나는 당신이 파일 형식이 "mp3"인 것을 보았습니다. 그러나 나는 "배경 음악"과 같은 노래의 실제 이름을 어디에 넣어야합니까? –
backgroundmusic.mp3을 다음과 같이 재생하려면, playMusicFile 함수를 공유했습니다 : [self playMusicFile : @ "backgroundmusic"]. – guenis