我可以直接在 ViewController 中使用 AVAudioPlayer 框架播放 mp3 文件作为按钮的操作,但是如果我想播放相同的 mp3 调用 NSObject 类中的选择器,则失败。谢谢你的建议。
从 ViewController (audioPlayerDemo.xcodeproj) 播放 mp3 并且可以正常工作:
框架文件夹中包含的 AVFoundation.framework
APDViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface APDViewController : UIViewController <AVAudioPlayerDelegate>
{
}
@property (strong,nonatomic) AVAudioPlayer *audioPlay;
- (IBAction)playid)sender;
APDViewController.m
@implementation APDViewController
@synthesize audioPlay;
- (IBAction)playid)sender
{
NSError *error = nil;
NSData *dataAudio = [[NSData alloc] initWithContentsOfFile:
@"/Users/User/Documents/AudioPlayerDemo/AudioPlayerDemo/hello world.mp3"options:0 error:&error];
if(error){
NSLog(@" error loading data: %@ ", [error localizedDescription]);
}
else{
audioPlay = [[AVAudioPlayer alloc] initWithData:dataAudio error:&error];
if(error){
NSLog(@" error loading audio: %@ ", [error localizedDescription]);
}
else{
audioPlay.delegate = self;
[audioPlay prepareToPlay];
[audioPlay play];
}
}
}
-(void)audioPlayerDidFinishPlaying:
(AVAudioPlayer *)player successfullyBOOL)flag
{
if (player == audioPlay){
audioPlay = nil;
NSLog(@"audioplay = nil");
}
}
但是,如果我想播放调用 NSObject 类 (AudioPlayClass.xcodeproj) 的相同 .mp3,则没有错误,也没有任何声音。这里的代码不起作用:
框架文件夹中包含的 AVFoundation.framework
APCplayObject.h
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface APCplayObject : NSObject <AVAudioPlayerDelegate>
{
}
@property (strong,nonatomic) AVAudioPlayer *audioPlay;
- (void)playAudio;
@end
APCplayObject.m
#import "APCplayObject.h"
@implementation APCplayObject
@synthesize audioPlay;
-(void)playAudio{
NSError *error = nil;
NSData *dataAudio = [[NSData alloc] initWithContentsOfFile:
@"/Users/User/Documents/AudioPlayerClass/AudioPlayerClass/hello world.mp3"options:0 error:&error];
if(error){
NSLog(@" error loading data: %@ ", [error localizedDescription]);
}
else{
audioPlay = [[AVAudioPlayer alloc] initWithData:dataAudio error:&error];
if(error){
NSLog(@" error loading audio: %@ ", [error localizedDescription]);
}
else{
audioPlay.delegate = self;
[audioPlay prepareToPlay];
[audioPlay play];
}
}
}
-(void)audioPlayerDidFinishPlaying:
(AVAudioPlayer *)player successfullyBOOL)flag
{
if (player == audioPlay){
audioPlay = nil;
NSLog(@"audioplay = nil");
}
}
APCviewController.m,调用者作为一个按钮的 Action :
- (IBAction)callPlayid)sender {
APCplayObject *a = [[APCplayObject alloc] init];
[a playAudio];
}
提前致谢。
Best Answer-推荐答案 strong>
问题是在你开始播放音频后,变量a 超出了范围,因此被销毁了。
当方法退出时,您需要创建一个声明的属性来保存您的 APCplayObject 。
所以,在 APCviewController.h 中你可以这样做:
@property (strong,nonatomic) APCplayObject *playObject;
然后将 callPlay 改为:
- (IBAction)callPlayid)sender {
if (!self.playObject) // Use this line if you only want to create one playObject, take it out to create a new one every time.
self.playObject = [[APCplayObject alloc] init];
[self.playObject playAudio];
}
关于IOS。我不能播放调用 NSObject 类的 mp3,但是它可以直接从 UIViewController 工作,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/17584728/
|