Multimedia play-music play, multimedia-music play
Video and audio playback
Add framework AVFoundation. framework
Import # import <AVFoundation/AVFoundation. h>
Create an audio player
AVAudioPlayer * _ audio
// Obtain local music
NSData * audioData = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "1" ofType: @ "mp3"]; _ audio = [[AVAudioPlayer alloc] initWithData: audioData error: nil]; // volume _ audio. volume = 1; // sets the playback rate _ audio. enableRate = YES; // 1.0 normal 0.5 halved 2.0 twice _ audio. rate = 1; // set audio channel //-1.0 left audio channel, 0.0 dual audio channel, 1.0 right audio channel _ audio. pan = 0.0; // If the audio is not played, currentTime indicates playing from 30 seconds. // If the audio is playing, currentTime indicates playing at 30 seconds. // currentTime can be interpreted as the current playback time. When obtaining the playback time, it is also currentTime _ audio. currentTime = 30; // play [_ audio play];
// Obtain online music
NSURL *url=[NSURL URLWithString:@"http://localhost:8080/downloadSrver/1.mp3"]; NSURLRequest *request=[NSURLRequest requestWithURL:url]; NSData *data=(NSData *)[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; _audio=[[AVAudioPlayer alloc]initWithData:data error:nil];
// Pause Based on the playback status
if (_audio.playing==YES) { [_audio pause]; [button setTitle:@“continue” forState:UIControlStateNormal]; } else { [_audio play]; [button setTitle:@"pause" forState:UIControlStateNormal]; }
// Stop playing
[_audio stop];