In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
IOS Learning: AVAudioPlayer plays music files and reads music files in the ipod library
The first step is to import the AVFoundation framework and
Here is the code, all with comments:
[cpp] view plaincopy
/ /
/ / RootViewController.h
/ / SoundDemo
/ /
/ / Created by on 13-6-21.
/ / Copyright (c) 2013 DoubleMan. All rights reserved.
/ /
# import
# import
# import
@ interface RootViewController: UIViewController
{
AVAudioPlayer * player
}
@ property (nonatomic, retain) AVAudioPlayer * player
@ property (nonatomic, retain) UISlider * slider
@ property (nonatomic, retain) NSTimer * timer
@ end
[cpp] view plaincopy
/ /
/ / RootViewController.m
/ / SoundDemo
/ /
/ / Created by on 13-6-21.
/ / Copyright (c) 2013 DoubleMan. All rights reserved.
/ /
# import "RootViewController.h"
@ interface RootViewController ()
@ end
@ implementation RootViewController
@ synthesize player
@ synthesize slider
@ synthesize timer
-(id) initWithNibName: (NSString *) nibNameOrNil bundle: (NSBundle *) nibBundleOrNil
{
Self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]
If (self) {
/ / Custom initialization
}
Return self
}
-(void) viewDidLoad
{
[super viewDidLoad]
UIButton * musicPlay = [UIButton buttonWithType:UIButtonTypeRoundedRect]
MusicPlay.frame = CGRectMake (10,10,90,35)
[musicPlay setTitle:@ "Play" forState:UIControlStateNormal]
[musicPlay addTarget:self action:@selector (playMusic) forControlEvents:UIControlEventTouchUpInside]
[self.view addSubview:musicPlay]
UIButton * pause = [UIButton buttonWithType:UIButtonTypeRoundedRect]
Pause.frame = CGRectMake (115,10,90,35)
[pause setTitle:@ "Pause" forState:UIControlStateNormal]
[pause addTarget:self action:@selector (pause) forControlEvents:UIControlEventTouchUpInside]
[self.view addSubview:pause]
UIButton * stop = [UIButton buttonWithType:UIButtonTypeRoundedRect]
Stop.frame = CGRectMake (220,10,90,35)
[stop setTitle:@ "stop" forState:UIControlStateNormal]
[stop addTarget:self action:@selector (stop) forControlEvents:UIControlEventTouchUpInside]
[self.view addSubview:stop]
Slider = [[UISlider alloc] initWithFrame:CGRectMake (10,65,300,20)]
[slider addTarget:self action:@selector (sliderValueChange:) forControlEvents:UIControlEventValueChanged]
[self.view addSubview:slider]
/ /
NSString * path = [[NSBundle mainBundle] pathForResource:@ "find a reason to fall in love-Chenxi-Ai GE" ofType:@ "wav"]
NSURL * url = [NSURL fileURLWithPath:path]
Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]
/ / set the number of cycles.-1 is a continuous loop.
Player.numberOfLoops =-1
/ / ready to play
[player prepareToPlay]
/ / set playback volume
Player.volume = 50
/ / the current playback position, that is, starting from currentTime, is related to the seekTo method in android.
Player.currentTime = 15
/ / set proxy
Player.delegate = self
Int dur = player.duration
Slider.maximumValue = dur
/ / update the playback progress once a second
Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector (updateSlider) userInfo:nil repeats:YES]
/ / read music files from ipod library
/ / MPMediaQuery * everything = [[MPMediaQuery alloc] init]
/ read condition
/ / MPMediaPropertyPredicate * albumNamePredicate =
/ / [MPMediaPropertyPredicate predicateWithValue: [NSNumber numberWithInt:MPMediaTypeMusic] forProperty: MPMediaItemPropertyMediaType]
/ / [everything addFilterPredicate:albumNamePredicate]
/ /
/ / NSLog (@ "Logging items from a generic query...")
/ / NSArray * itemsFromGenericQuery = [everything items]
/ / for (MPMediaItem * song in itemsFromGenericQuery) {
/ / NSString * songTitle = [song valueForProperty: MPMediaItemPropertyTitle]
/ / NSLog (@ "% @", songTitle)
/ /}
/ /
/ / [everything release]
}
/ / Update playback progress
-(void) updateSlider {
Slider.value = player.currentTime
}
/ / when the progress slider changes, jump to progress playback
-(void) sliderValueChange: (UISlider *) mSlider {
Player.currentTime = mSlider.value
NSLog (@ "value:% .0f", mSlider.value)
}
/ / stop
-(void) stop {
Player.currentTime = 0
[player stop]
}
/ / pause
-(void) pause {
[player pause]
NSLog (@ "pause")
}
/ / start playback
-(void) playMusic {
NSLog (@ "start play")
[player play]
}
# pragma mark-AVAudioPlayerDelegate
-(void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player successfully: (BOOL) flag {
/ / call when the playback is complete, it will only be called when the playback is finished, and will not be adjusted when the playback loop is completed.
[timer invalidate]
NSLog (@ "audioPlayerDidFinishPlaying")
}
/ * if an error occurs while decoding it will be reported to the delegate. , /
-(void) audioPlayerDecodeErrorDidOccur: (AVAudioPlayer *) player error: (NSError *) error {
/ / called in case of decoding error
}
-(void) didReceiveMemoryWarning
{
[super didReceiveMemoryWarning]
/ / Dispose of any resources that can be recreated.
}
-(void) dealloc
{
[player stop]
[player release]
[slider release]
[timer release]
[super dealloc]
}
@ end
Previous IOS Learning: customizing AlertView with UIWindow (the most basic code)
The next IOS Development Learning: MKMapView Custom CalloutView
Top
two
Step on
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.