0

誰でもチュートリアルに案内したり、バックグラウンド ミュージックを再生する実際のコードを表示したりできますか。開始および停止できるようにする必要があります。これは割り当てではないので、ご存じのとおりです。私の課題はすでに完了しています。音楽を追加したいだけですが、方法がわかりません。ありがとう!

4

2 に答える 2

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
{
    ...
}

停止と一時停止:

//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.

詳細については、リファレンスを参照してください: http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html

于 2013-04-25T20:00:02.013 に答える
1

AVPlayerそしてAVAudioPlayer動作するはずです。

于 2013-04-25T20:31:03.857 に答える