如何在点击 UIButton 时播放 MP3?

发布于 2024-10-09 01:11:04 字数 80 浏览 0 评论 0原文

任何人都可以给我如何通过点击 UIButton 来播放声音的示例代码吗?

我想使用 AVAudioPlayer 播放 MP3 文件

Can anyone please give me sample code of how sound is played with a UIButton being tapped?

I would like to play an MP3 file using AVAudioPlayer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您的好友蓝忘机已上羡 2024-10-16 01:11:04

像这样的事情应该可以帮助你开始。将其添加到您的视图控制器中,然后将按钮连接到界面生成器中的 playAudio 操作。

在你的标题中 .h

#import <AVFoundation/AVFoundation.h>

@interface ClassName {
    ...
    AVAudioPlayer *audioPlayer;
}
@property (nonatomic, retain) AVAudioPlayer *audioPlayer;

- (IBAction) playAudio;

在你的 .m 中

@synthesize audioPlayer;

- (IBAction) playAudio {
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"audio" withExtension: @"m4a"];
    if (!url){NSLog(@"file not found"); return;}
    NSError *error;
    self.audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error] autorelease];
    [audioPlayer play]
}

something like this should get you started. Add this to your view controller, then hook up the button to the playAudio action in interface builder.

in your header .h

#import <AVFoundation/AVFoundation.h>

@interface ClassName {
    ...
    AVAudioPlayer *audioPlayer;
}
@property (nonatomic, retain) AVAudioPlayer *audioPlayer;

- (IBAction) playAudio;

in your .m

@synthesize audioPlayer;

- (IBAction) playAudio {
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"audio" withExtension: @"m4a"];
    if (!url){NSLog(@"file not found"); return;}
    NSError *error;
    self.audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error] autorelease];
    [audioPlayer play]
}
难理解 2024-10-16 01:11:04
//ViewController.h ,write below code
@interface ViewController : UIViewController<AVAudioRecorderDelegate,AVAudioPlayerDelegate>

//assign property to player
 @property(nonatomic,retain) AVAudioPlayer *player;

//then write in ViewController.m file in ViewDidLoad Method

NSError *soundError;
NSString *path=[[NSBundle mainBundle]pathForResource:@"soundFileName" ofType:@"mp3"]; //.mp3 file for player
NSURL *file=[[NSURL alloc]initFileURLWithPath:path]; //path
_player=[[AVAudioPlayer alloc]initWithContentsOfURL:file error:&soundError]; //player Object

if(_player == nil)
{
    NSLog(@"player is empty because of %@",soundError);
}
else
{
    [_player play];
    _player.volume=1.0;
    [_player setDelegate:self];

}

// for stop player you can use 
    // [_player stop]; //uncomment this line when you wants to stop it.
//ViewController.h ,write below code
@interface ViewController : UIViewController<AVAudioRecorderDelegate,AVAudioPlayerDelegate>

//assign property to player
 @property(nonatomic,retain) AVAudioPlayer *player;

//then write in ViewController.m file in ViewDidLoad Method

NSError *soundError;
NSString *path=[[NSBundle mainBundle]pathForResource:@"soundFileName" ofType:@"mp3"]; //.mp3 file for player
NSURL *file=[[NSURL alloc]initFileURLWithPath:path]; //path
_player=[[AVAudioPlayer alloc]initWithContentsOfURL:file error:&soundError]; //player Object

if(_player == nil)
{
    NSLog(@"player is empty because of %@",soundError);
}
else
{
    [_player play];
    _player.volume=1.0;
    [_player setDelegate:self];

}

// for stop player you can use 
    // [_player stop]; //uncomment this line when you wants to stop it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文