プロジェクトの要件に従って AVAudioPlayer を開発しています。audioplayer では、ストーリーボードを使用してコーディングを開発しています。私のオーディオ プレーヤーでは、json パーサーを使用してサーバーから URL を取得しています。Audioplayer は正常に再生されています。ただし、ビューは表示されません。空白の画面が表示されます。再生ボタン、一時停止ボタン、進むボタン、巻き戻しボタンなどのすべてのボタンをドラッグ アンド ドロップできます。これらのボタンは AudioviewController にドラッグ アンド ドロップされます。また、接続も提供します。これらはすべて、対応する曲を再生するために Audioviewcontroller に表示されるボタンではありません。Plzは私にどんな体でも問題を教えてくれます. iOSプログラミングは初めてです。よろしくお願いします...
Audioviewcontroller.m
-(void)updateCurrentTimeForPlayer:(AVAudioPlayer *)
{
currentTime.text = [NSString stringWithFormat:@"%d:%02d", (int)p.currentTime / 60, (int)p.currentTime % 60, nil];
progressBar.value = p.currentTime;
}
-(void)updateCurrentTime
{
[self updateCurrentTimeForPlayer:self.avPlayer];
}
-(void)updateViewForPlayerState:(AVAudioPlayer *)p
{
[self updateCurrentTimeForPlayer:p];
if (updateTimer)
[updateTimer invalidate];
if (p.playing)
{
[playButton setImage:(p.playing==YES)? pauseButtonBG:playButtonBG forState:UIControlStateNormal];
updateTimer=[NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateCurrentTime) userInfo:p repeats:YES];
}
else
{
[playButton setImage:(p.playing==YES)? pauseButtonBG:playButtonBG forState:UIControlStateNormal];
updateTimer=nil;
}
}
-(void)updateViewForPlayerStateInBackground:(AVAudioPlayer *)p
{
[self updateCurrentTimeForPlayer:p];
if (p.playing)
{
[playButton setImage:(p.playing==YES)? pauseButtonBG:playButtonBG forState:UIControlStateNormal];
}
else
{
[playButton setImage:(p.playing==YES)? pauseButtonBG:playButtonBG forState:UIControlStateNormal];
}
}
-(void)updateViewForPlayerInfo:(AVAudioPlayer*)p
{
duration.text = [NSString stringWithFormat:@"%d:%02d", (int)p.duration / 60, (int)p.duration % 60, nil];
progressBar.maximumValue = p.duration;
volumeSlider.value = p.volume;
}
// rewind audio player
-(void)rewind
{
AVAudioPlayer *p=rewindTimer.userInfo;
p.currentTime-=SKIP_TIME;
[self updateCurrentTimeForPlayer:p];
}
// forward audio player
-(void)forward
{
AVAudioPlayer *p=forwardTimer.userInfo;
p.currentTime+=SKIP_INTERVAL;
[self updateCurrentTimeForPlayer:p];
}
json parser::
-(void)loadData
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.teluguastrology.com"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
// delegate methods in json
-(void)connection:(NSURLConnection *)connectiondidReceiveResponse:(NSURLResponse *)response
{
[_data setLength:0];
NSLog(@"didReceiveResponse called");
}
//connection did receive data
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSMutableData *)data
{
[_data appendData:data];
}
// connection did finish loading
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *jsonError = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:_data options:kNilOptions error:&jsonError];
if ([jsonObject isKindOfClass:[NSArray class]])
{
//NSArray *jsonArray = (NSArray *)jsonObject;
}
else if ([jsonObject isKindOfClass:[NSDictionary class]])
{
NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
NSArray *array=[[NSArray alloc]init];
array=[jsonDictionary objectForKey:@"audio-urls"];
dataDictionary=[array objectAtIndex:0];
/* NSLog(@"%@",dataDictionary);*/
}
[urlsArray addObject:[dataDictionary objectForKey:@"Meshamu-Aries"]];
NSLog(@"%@",urlsArray);
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// [connection release];
NSLog(@"didFailWithError called");
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
// self.view.backgroundColor=[UIColor redColor];
urlsArray=[[NSMutableArray alloc]init];
[super viewDidLoad];
playButtonBG=[[UIImage imageNamed:@"audio_play.png"]init];
pauseButtonBG=[[UIImage imageNamed:@"audio_pause.png"]init];
[playButton setImage:playButtonBG forState:UIControlStateNormal];
//[self registerForBackGroundNotification];
updateTimer=nil;
rewindTimer=nil;
forwardTimer=nil;
// to adjust the font in label to using following method
duration.adjustsFontSizeToFitWidth=YES;
currentTime.adjustsFontSizeToFitWidth=YES;
progressBar.minimumValue=0.0;
_data=[[NSMutableData alloc]init];
[self loadData];
backgroundImg=[[UIImageView alloc]init];
backgroundImg.image=[UIImage imageNamed:@"image.png"];
[self.view addSubview:backgroundImg];
}
-(void)pausePlaybackForPlayer:(AVAudioPlayer *)p
{
[p pause];
[self updateViewForPlayerState:p];
}
-(void)startPlaybackForPlayer:(AVAudioPlayer *)p
{
if([p play])
{
[self updateViewForPlayerState:p];
}
else
NSLog(@"Could not play %@\n", p.url);
}
-(IBAction)playButtonPressed:(UIButton *)sender
{
if (avPlayer.playing==YES)
[self pausePlaybackForPlayer:avPlayer];
else
[self startPlaybackForPlayer:avPlayer];
}
-(IBAction)rewindButtonPressed:(UIButton *)sender
{
if(rewindTimer)
[rewindTimer invalidate];
rewindTimer=[NSTimer scheduledTimerWithTimeInterval:SKIP_INTERVAL target:self selector:@selector(rewind) userInfo:avPlayer repeats:YES];
}
-(IBAction)rewindButtonReleased:(UIButton *)sender
{
if(rewindTimer)
[rewindTimer invalidate];
rewindTimer=nil;
}
-(IBAction)forwardButtonPressed:(UIButton *)sender
{
if(forwardTimer)
[forwardTimer invalidate];
forwardTimer=[NSTimer scheduledTimerWithTimeInterval:SKIP_TIME target:self selector:@selector(forward) userInfo:avPlayer repeats:YES];
}
-(IBAction)forwardButtonReleased:(UIButton *)sender
{
if(forwardTimer)
[forwardTimer invalidate];
forwardTimer=nil;
}
-(IBAction)volumeSliderMoved:(UISlider *)sender
{
avPlayer.volume=[sender value];
}
-(IBAction)progressBarMoved:(UISlider *)sender
{
avPlayer.currentTime=[sender value];
[self updateCurrentTimeForPlayer:avPlayer];
}
Audioviewcontroller.h
@interface AudioViewController : UIViewController<AVAudioPlayerDelegate,NSURLConnectionDelegate>
{
IBOutlet UILabel *astroName;
IBOutlet UIButton *playButton;
IBOutlet UIButton *forwardButton;
IBOutlet UIButton *rewindButton;
IBOutlet UISlider *volumeSlider;
IBOutlet UILabel *currentTime;
IBOutlet UILabel *duration;
IBOutlet UISlider *progressBar;
UIImage *playButtonBG;
UIImage *pauseButtonBG;
NSTimer *forwardTimer;
NSTimer *rewindTimer;
NSTimer *updateTimer;
UIImageView *backgroundImg;
BOOL inBackground;
NSURLConnection *connect;
NSMutableData *responseData;
NSMutableData *downloadData;
NSMutableString *responseString;
NSMutableArray *urlsArray;
NSMutableData *_data;
NSDictionary *dataDictionary;
}
- (IBAction)playButtonPressed:(UIButton*)sender;
- (IBAction)rewindButtonPressed:(UIButton*)sender;
- (IBAction)rewindButtonReleased:(UIButton*)sender;
- (IBAction)forwardButtonPressed:(UIButton*)sender;
- (IBAction)forwardButtonReleased:(UIButton*)sender;
- (IBAction)volumeSliderMoved:(UISlider*)sender;
-(IBAction)progressBarMoved:(UISlider *)sender;
-(void)registerForBackGroundNotification;
@property(nonatomic,retain) UILabel *astroName;
@property(nonatomic,retain) UIButton *playButton;
@property(nonatomic,retain) UIButton *pauseButton;
@property(nonatomic,retain) UIButton *forwardButton;
@property(nonatomic,retain) UIButton *rewindButton;
@property(nonatomic,retain) UISlider *volumeSlider;
@property(nonatomic,retain) UISlider *progressBar;
@property(nonatomic,retain) UILabel *currentTime;
@property(nonatomic,retain) UILabel *duration;
@property(nonatomic,retain) NSTimer *updateTimer;
@property(nonatomic,retain) AVAudioPlayer *avPlayer;
@property(nonatomic,assign) BOOL *inBackGround;
@property(nonatomic,retain) UIImageView *backgroundImg;
@property(retain, nonatomic) NSMutableData* responseData;
@property(nonatomic,strong) NSData *downloadData;
@property(nonatomic,strong) NSMutableString *responseString;
@property (nonatomic,assign)int selectedIndex;
@end