IOS learning: AVAudioPlayer playing music files and reading music files in the ipod Library

Source: Internet
Author: User

The following figure shows the debugging results on the ipad.

 
 


The following is the code with comments in the Code:


[Cpp]
//
// RootViewController. h
// SoundDemo
//
// Created by on 13-6-21.
// Copyright (c) 2013 DoubleMan. All rights reserved.
//
 
# Import <UIKit/UIKit. h>
# Import <AVFoundation/AVFoundation. h>
# Import <MediaPlayer/MediaPlayer. h>
 
@ Interface RootViewController: UIViewController <AVAudioPlayerDelegate>
{
AVAudioPlayer * player;
}
 
@ Property (nonatomic, retain) AVAudioPlayer * player;
@ Property (nonatomic, retain) UISlider * slider;
@ Property (nonatomic, retain) NSTimer * timer;
 
@ End

//
// RootViewController. h
// SoundDemo
//
// Created by on 13-6-21.
// Copyright (c) 2013 DoubleMan. All rights reserved.
//

# Import <UIKit/UIKit. h>
# Import <AVFoundation/AVFoundation. h>
# Import <MediaPlayer/MediaPlayer. h>

@ Interface RootViewController: UIViewController <AVAudioPlayerDelegate>
{
AVAudioPlayer * player;
}

@ Property (nonatomic, retain) AVAudioPlayer * player;
@ Property (nonatomic, retain) UISlider * slider;
@ Property (nonatomic, retain) NSTimer * timer;

@ End

[Cpp]
//
// 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-aige" ofType: @ "wav"];
NSURL * url = [NSURL fileURLWithPath: path];
Player = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: nil];
// Set the number of cycles.-1 indicates a continuous loop.
Player. numberOfLoops =-1;
// Prepare for playing
[Player prepareToPlay];
// Set the playback volume
Player. volume = 50;
// The current playback position, that is, start playing from currentTime. 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 the music file from the ipod Library
// MPMediaQuery * everything = [[MPMediaQuery alloc] init];
//// Read Conditions
// 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 the playback progress
-(Void) updateSlider {
Slider. value = player. currentTime;
}
 
// Jump to progress playback when the progress slider changes
-(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 playing
-(Void) playMusic {
NSLog (@ "start play ");
[Player play];
}
 
# Pragma mark-AVAudioPlayerDelegate
-(Void) audioPlayerDidFinishPlaying :( AVAudioPlayer *) player successfully :( BOOL) flag {
// Called when playback is complete. It is called only when the playback ends, but not during loop playback.
[Timer invalidate];
NSLog (@ "audioPlayerDidFinishPlaying ");
}
 
/* If an error occurs while decoding it will be reported to the delegate .*/
-(Void) audioPlayerDecodeErrorDidOccur :( AVAudioPlayer *) player error :( NSError *) error {
// Call when an error occurs in decoding
}
 
-(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.