简体   繁体   中英

About AVPlayer's volume at real device

I'm using AVPlayer to play local audio file and audio stream.

Everything work fine at real device except volume. The volume is very low. even i set device volume max. but earphone's volume is normal. i guess some how speaker's volume is become earphone's volume.

and it's also works fine with Simulator.

I have tried the MPMusicPlayerController , AVMutableAudioMix to set volume. but nothing works.

I'm totally confused. Thanks for any help.

You can get the solution from developer.Apple Technical Q&A QA1716

AVPlayer uses the system volume (controlled by the hardware volume switch) for media playback.

Use the MPVolumeView class to present the user with a slider control in your application for setting the system audio output volume

See the MPVolumeView Class Reference for more information.

You can mute the playback of audio with AVPlayer by creating an AVAudioMix with a volume ramp to set the volume to 0 as shown below

Muting the playback of audio with AVPlayer by creating an AVAudioMix with zero volume ramp.

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[self myAssetURL] options:nil];
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];

// Mute all the audio tracks
NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
    AVMutableAudioMixInputParameters *audioInputParams =[AVMutableAudioMixInputParameters audioMixInputParameters];
    [audioInputParams setVolume:0.0 atTime:kCMTimeZero];
    [audioInputParams setTrackID:[track trackID]];
    [allAudioParams addObject:audioInputParams];
}
AVMutableAudioMix *audioZeroMix = [AVMutableAudioMix audioMix];
[audioZeroMix setInputParameters:allAudioParams];

// Create a player item
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem setAudioMix:audioZeroMix]; // Mute the player item

// Create a new Player, and set the player to use the player item 
// with the muted audio mix
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];

// assign player object to an instance variable
self.mPlayer = player;

// play the muted audio
[mPlayer play];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM