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