cách dễ nhất để phát tệp nhạc như Mp3 với botton tạm dừng là gì? chơi rất đơn giản và nút khác tạm dừng nhạc đó .. bạn có thể viết mã đó không!Phát tệp MP3 bằng iPhone SDK
Trả lời
Đây là các mã dành cho các tác vụ được yêu cầu, appSoundPlayer là thuộc tính của AVAudioPlayer được khai báo trong tệp h. Ví dụ này cũng phát một bài hát trong thư mục tài nguyên.
#pragma mark -
#pragma mark *play*
- (IBAction) playaction {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"songname" ofType:@"mp3"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
self.soundFileURL = newURL;
[newURL release];
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
// Registers the audio route change listener callback function
AudioSessionAddPropertyListener (
kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,
self
);
// Activates the audio session.
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume: 1.0];
[appSoundPlayer setDelegate: self];
[appSoundPlayer play];
[stopbutton setEnabled:YES];
[playbutton setEnabled: NO];
playbutton.hidden=YES;
pausebutton.hidden =NO;
}//playbutton touch up inside
#pragma mark -
#pragma mark *pause*
-(IBAction)pauseaction {
[appSoundPlayer pause];
pausebutton.hidden = YES;
resumebutton.hidden = NO;
}//pausebutton touch up inside
#pragma mark -
#pragma mark *resume*
-(IBAction)resumeaction{
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume:1.0];
[appSoundPlayer setDelegate: self];
[appSoundPlayer play];
playbutton.hidden=YES;
resumebutton.hidden =YES;
pausebutton.hidden = NO;
}//resumebutton touch up inside
#pragma mark -
#pragma mark *stop*
-(IBAction)stopaction{
[appSoundPlayer stop];
[playbutton setEnabled:YES];
[stopbutton setEnabled:NO];
playbutton.hidden=NO;
resumebutton.hidden =YES;
pausebutton.hidden = YES;
}//stopbutton touch up inside
The Apple documentation here nên có mọi thứ bạn cần biết.
cũng here là một hướng dẫn có sẵn.
Chủ đề là
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
và khi bạn muốn tạm dừng;
[audioPlayer pause];
hy vọng điều này sẽ hữu ích.
Đối với âm thanh ngắn hoặc khi nghe nhạc MP3 không chơi tốt trên các mã đề nghị bạn luôn có thể sử dụng:
SystemSoundID soundID;
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound.mp3", [[NSBundle mainBundle] resourcePath]]];
AudioServicesCreateSystemSoundID((CFURLRef)url, &soundID);
AudioServicesPlaySystemSound (soundID);
Đừng quên thêm:
#import <AudioToolbox/AudioToolbox.h>
cảm ơn rất nhiều vì điều này! – rwarner
Cảm ơn, đã làm việc cho tôi. Với ARC: AudioServicesCreateSystemSoundID ((__ bridge CFURLRef) url & soundID); –
Các oalTouch mẫu mã trên trang web dành cho nhà phát triển Apple iOS thực hiện những gì bạn muốn và sẵn sàng chạy. Nó cũng cho thấy làm thế nào để kiểm tra nếu một ứng dụng khác (ví dụ như ipod) đang chơi một tập tin đã có, đó là tiện dụng nếu bạn muốn cho phép người dùng của bạn để nghe nhạc riêng của họ thay vì của bạn.
API DIRAC miễn phí và khá dễ sử dụng. Chúng tôi đã sử dụng nó trong tôi nói ứng dụng Robot http://itunes.apple.com/us/app/daidai/id484833168 và nó đã được tuyệt vời để tôi làm thế nào nó quản lý để thay đổi tốc độ và cường độ của giọng nói
Tôi sợ câu trả lời nói không còn hoạt động trong iOS 7 trở lên. Bạn sẽ cần phải sử dụng đoạn mã sau:
trong file header (.h)
Để xử lý các phương pháp đại biểu như khi chơi các âm thanh đã hoàn tất audioPlayerDidFinishPlaying :, kế thừa từ AVAudioPlayerDelegate.
@property (nonatomic, strong) AVAudioPlayer *player;
trong file thi hành (.m)
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: resourceName
ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
_player = newPlayer;
[_player prepareToPlay];
[_player setDelegate: self];
[_player play];
Tôi thích mã đơn giản và đây là giải pháp của tôi: (. Hãy nhớ thêm nút chuyển đổi để có được chơi nhạc Have fun)
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController()
{
NSURL* bgURL;
AVAudioPlayer* bgPlayer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
bgURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mp3"]];
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:nil];
}
#pragma mark AVAudioPlayer
- (IBAction)toggleMusic:(UISwitch*)sender {
NSLog(@"togging music %s", sender.on ? "on" : "off");
if (bgPlayer) {
if (sender.on) {
[bgPlayer play];
}
else {
[bgPlayer stop];
}
}
}
- 1. Phát tệp MP3 bằng Python
- 2. Cách phát tệp mp3 bằng C++?
- 3. Android: Phát các tệp MP3 bằng AudioTrack bằng ffmpeg
- 4. phát tệp mp3 trong ứng dụng iOS
- 5. Phát tệp mp3 bằng cách sử dụng javascript
- 6. iPhone UIWebView phát MP3. Giao thức loại bỏ?
- 7. Cách phát tệp MP3 trong C?
- 8. phát hiện nháy mắt cho iphone sdk
- 9. Trình phát Jplayer Circle không phát tệp mp3
- 10. mã hóa một tệp trên iphone-sdk
- 11. Phát mp3 trong Haskell?
- 12. ca cao phát mp3
- 13. Tải xuống một tệp lớn - iPhone SDK
- 14. phát mp3 với monotouch
- 15. Cắt các tệp mp3 bằng NAudio
- 16. Cách tạo GUID/UUID bằng SDK iPhone
- 17. iPhone sdk, lưu MPMediaItemCollection?
- 18. iPhone SDK audioSession question
- 19. Tôi làm cách nào để phát tệp mp3?
- 20. Ứng dụng iPhone: ghi âm trong mp3
- 21. Tệp MP3 trực tuyến MVC3
- 22. Phát tệp MP3 được lưu trữ dưới dạng blob
- 23. không thể phát tệp mp3 âm thanh trong phonegap android
- 24. Cách phát tệp .mp3 từ tài nguyên trong C#?
- 25. iPhone SDK - Cách phát video có độ trong suốt?
- 26. iphone SDK phát hiện mạng Wifi và mạng di động
- 27. Cách tải xuống tệp văn bản với iPhone SDK?
- 28. Làm thế nào để phát mp3 bằng Javascript?
- 29. HTML 5 - Phát nhỏ mp3 "nội tuyến"
- 30. Làm thế nào để tôi lập trình phát nhạc MP3 trên iPhone?
bạn có mã mẫu của hướng dẫn này không? – Momi
những lỗi đó là do bạn chưa khai báo chúng trong tệp h. – Nithin
Mã này có hoạt động cho NSURL ... phát âm thanh đệm từ máy chủ không? –