默认的MediaPlayer接口只能播放mp3格式的音乐,然而我们现在想播放vlc和mp4格式的音乐,
怎么做呢?
这里我们引入AdvancedMediaPlayer接口,AdvancedMediaPlayer接口可以播放vlc和mp4格式的音乐。
怎么将AdvancedMediaPlayer接口“适配”到MediaPlayer接口中呢?
这里引入MediaAdapter类,这个类用来适配vlc和mp4格式的音乐
最后用AudioPlayer类实现MediaPlayer接口,适配的工作交给MediaAdapter类
类结构图如下:
VlcPlayer.java
package com.advance.adapter;/** * @Auther: 谷天乐 * @Date: 2018/9/20 22:58 * @Description: */public class VlcPlayer implements AdvancedMediaPlayer{ @Override public void playVlc(String fileName) { System.out.println("Playing vlc file. Name: "+ fileName); } @Override public void playMp4(String fileName) { //什么也不做 }}
Mp4Player.java
package com.advance.adapter;/** * @Auther: 谷天乐 * @Date: 2018/9/20 22:58 * @Description: */public class Mp4Player implements AdvancedMediaPlayer{ @Override public void playVlc(String fileName) { //什么也不做 } @Override public void playMp4(String fileName) { System.out.println("Playing mp4 file. Name: "+ fileName); }}
MediaPlayer.java
package com.advance.adapter;/** * @Auther: 谷天乐 * @Date: 2018/9/20 22:57 * @Description: */public interface MediaPlayer { public void play(String audioType, String fileName);}
MediaAdapter.java
package com.advance.adapter;/** * @Auther: 谷天乐 * @Date: 2018/9/20 22:58 * @Description: */public class MediaAdapter implements MediaPlayer { AdvancedMediaPlayer advancedMusicPlayer; public MediaAdapter(String audioType){ if(audioType.equalsIgnoreCase("vlc") ){ advancedMusicPlayer = new VlcPlayer(); } else if (audioType.equalsIgnoreCase("mp4")){ advancedMusicPlayer = new Mp4Player(); } } @Override public void play(String audioType, String fileName) { if(audioType.equalsIgnoreCase("vlc")){ advancedMusicPlayer.playVlc(fileName); }else if(audioType.equalsIgnoreCase("mp4")){ advancedMusicPlayer.playMp4(fileName); } }}
AudioPlayer.java
package com.advance.adapter;/** * @Auther: 谷天乐 * @Date: 2018/9/20 22:59 * @Description: */public class AudioPlayer implements MediaPlayer { MediaAdapter mediaAdapter; @Override public void play(String audioType, String fileName) { //播放 mp3 音乐文件的内置支持 if(audioType.equalsIgnoreCase("mp3")){ System.out.println("Playing mp3 file. Name: "+ fileName); } //mediaAdapter 提供了播放其他文件格式的支持 else if(audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")){ mediaAdapter = new MediaAdapter(audioType); mediaAdapter.play(audioType, fileName); }else{ System.out.println("Invalid media. "+ audioType + " format not supported"); } }}
AdvancedMediaPlayer.java
package com.advance.adapter;/** * @Auther: 谷天乐 * @Date: 2018/9/20 22:57 * @Description: */public interface AdvancedMediaPlayer { public void playVlc(String fileName); public void playMp4(String fileName);}
AdapterPatternDemo.java
package com.advance.adapter;/** * @Auther: 谷天乐 * @Date: 2018/9/20 22:59 * @Description: */public class AdapterPatternDemo { public static void main(String[] args) { AudioPlayer audioPlayer = new AudioPlayer(); audioPlayer.play("mp3", "beyond the horizon.mp3"); audioPlayer.play("mp4", "alone.mp4"); audioPlayer.play("vlc", "far far away.vlc"); audioPlayer.play("avi", "mind me.avi"); }}
运行结果
Playing mp3 file. Name: beyond the horizon.mp3Playing mp4 file. Name: alone.mp4Playing vlc file. Name: far far away.vlcInvalid media. avi format not supported