0

私はアマチュア開発者で、ボタンが押されたときに再生されるサウンド ファイル (mp3) を含む iOS アプリを作成しています。ただし、サウンドは再生されません (ios シミュレーターではなく、モバイル デバイスでテストされています)。既にバンドル リソースにサウンド ファイルがあり、ライブラリとバイナリをリンクするために AVFoundation を追加しました。私はxcode 4.6を持っていて、mac osx 10.8.4を持っています。これがviewcontroller.hの私のコードです:

#import <UIKit/UIKit.h>
#import <RevMobAds/RevMobAds.h>
#import <AVFoundation/AVFoundation.h>


@interface ViewController : UIViewController 


- (IBAction)playButton:(id)sender;
- (IBAction)stopButton:(id)sender;

@property (nonatomic, strong) AVAudioPlayer *avPlayer;

- (void)revmob;
@end

Viewcontroller.m:

#import "ViewController.h"
#import <RevMobAds/RevMobAds.h>


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[RevMobAds session] showBanner];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *stringPath = [[NSBundle mainBundle] pathForResource:@"98648__dobroide__20100606-mosquito" ofType:@"mp3"];
    if (stringPath) 
    {
        NSURL *url = [NSURL fileURLWithPath:stringPath];

        NSError *error;

       _avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
        if (!error) 
        {
            [_avPlayer setNumberOfLoops:-1];
        } 
        else  
        {
            NSLog(@"Error occurred: %@", [error localizedDescription]);
        }
    } 
    else 
    {
        NSLog(@"Resource not found");
    }
    [self performSelector:@selector(revmob) withObject:nil afterDelay:10.0];
}

- (void)revmob 
{
    [[RevMobAds session] showFullscreen];
}
- (IBAction)playButton:(id)sender 
{
    [_avPlayer play];
    NSLog(@"Sound playing");
}

- (IBAction)stopButton:(id)sender 
{
    [_avPlayer pause];
    NSLog(@"Sound Stopped");
}
@end
4

2 に答える 2

4

ViewController.h で、AVFoundation ヘッダー ファイルをインポートします。

ViewController.m に移動し、次のアウトレット プロパティと IBAction メソッド定義をインターフェイス セクションに追加します。

@interface ViewController () 

@property (nonatomic, strong) IBOutlet UILabel *trackTitle;
@property (nonatomic, strong) IBOutlet UILabel *playedTime;

- (IBAction)stopSound:(id)sender; 
- (IBAction)playOrPauseSound:(id)sender;

@end

ViewController.xib に戻り、次の接続を行います。

トップラベル -> trackTitle アウトレット

中間ラベル -> 再生時間アウトレット

左ボタン -> IBAction playOrPauseSound

右ボタン -> IBAction stopSound

これで接続が確立され、ViewController.m でコードを完成させることができます。インターフェイス セクションで、次のプロパティを追加します。

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (nonatomic) BOOL isPlaying; 
@property (nonatomic, strong) NSTimer *timer;

ここに画像の説明を入力

ViewController.m に移動し、次のアウトレット プロパティと IBAction メソッド定義をインターフェイス セクションに追加します。

@interface ViewController () 

@property (nonatomic, strong) IBOutlet UILabel *trackTitle;
@property (nonatomic, strong) IBOutlet UILabel *playedTime;

- (IBAction)stopSound:(id)sender; 
- (IBAction)playOrPauseSound:(id)sender;

@end

の viewDidLoad メソッドを変更します。

- (void)viewDidLoad 
{ 
  [super viewDidLoad];

  self.view.backgroundColor = [UIColor whiteColor];
  self.isPlaying = NO; 
  self.trackTitle.text = @"The Stone Foxes - EveryBody Knows";

  NSBundle *mainBundle = [NSBundle mainBundle]; 
  NSString *filePath = [mainBundle pathForResource:@"thestonefoxes-everybodyknows" ofType:@"mp3"]; 
  NSData *fileData = [NSData dataWithContentsOfFile:filePath];

  NSError *error = nil;

  self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&error] 
    [self.audioPlayer prepareToPlay]; 
}


- (IBAction)playOrPauseSound:(id)sender; 
{ 
  if (self.isPlaying)
  { 
    // Music is currently playing 
    [self.audioPlayer pause]; 
    self.isPlaying = NO; 
  } 
  else 
  { 
    // Music is currenty paused/stopped 
    [self.audioPlayer play]; 
    self.isPlaying = YES;

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; 
  } 
}

- (void)updateTime 
{ 
  NSTimeInterval currentTime = self.audioPlayer.currentTime;

  NSInteger minutes = floor(currentTime/60);
  NSInteger seconds = trunc(currentTime - minutes * 60);

  // update your UI with currentTime; 
  self.playedTime.text = [NSString stringWithFormat:@"%d:%02d", minutes, seconds]; 
}

- (IBAction)stopSound:(id)sender 
{ 
  [self.audioPlayer stop];
  self.audioPlayer.currentTime = 0; 
  self.isPlaying = NO; 
}
于 2014-09-19T13:00:43.440 に答える