本小节主要说明 HarmonyOS 车载多媒体的使用方法,以音乐 Demo 开发为例,开发步骤如下:

成都创新互联欢迎联系:18982081108,为您提供成都网站建设网页设计及定制高端网站建设服务,成都创新互联网页制作领域十载,包括成都水泥搅拌车等多个领域拥有丰富的网站制作经验,选择成都创新互联,为企业保驾护航!
   
   
       
            
       
           
               
                    
               
                   
                        
                    
            
        
    
       
           
                
           
               
                    
               
                    
               
                    
               
                    
               
                    
            
        
       super.setUIContent(ResourceTable.Layout_play_music_layout);   public class PlayManager {
       ...
       private Player player;
       public synchronized boolean play(String filePath, int startMilliSecond) {
           ...
           FileDescriptor fd = IoUtil.getFileDescriptor(filePath);
           Source source = new Source(fd);
           player.setSource(source);
           boolean isSuccess = player.prepare();
    
           isSuccess = player.rewindTo(startMilliSecond * MICRO_MILLI_RATE, REWIND_NEXT_SYNC);
           // 播放
           isSuccess = player.play();
           isPlaying.set(isSuccess);
           return isSuccess;
       }
    
       public synchronized void pause(int startMilliSecond) {
           ...
           player.pause();
       }
    
       public synchronized void stop() {
           if (player == null) {
               return;
           }
           player.stop();
           isPlaying.set(false);
           LogUtil.info(TAG, "stop success");
           player.release();
           player = null;
       }
   }   // 指定歌曲播放
   String path = "/data/music/files/data/wonderful_life.mp3";
   PlayManager.getInstance().play(path,1);   // 视频布局实现方法
   public class MySurfaceSlice extends AbilitySlice {
       ... 
       public void makeSurfaceView() {
           ... 
           mySurfaceProvider = new SurfaceProvider(this);
           adaptiveBoxLayoutSurfaceView.AdaptiveBoxLayout.LayoutConfig().addComponent(mySurfaceProvider);
       }
   }   public class VideoPlay {
       public synchronized void startPlay() {
           ...   
           ret = playImpl.play();
       }
    
       public synchronized void preParePlay() {
           ...    
           ret = playImpl.prepare();
       }
    
       public synchronized void pausePlay() {
           ...    
           boolean pauseRet = playImpl.pause();;
       }
    
       public synchronized void setSourcePlay(String filePath) {
            ...    
           FileDescriptor fd = IoUtil.getFileDescriptor(filePath);
           Source source = new Source(fd);
           playImpl.setSource(source);
       }
    
       @Override
       public synchronized void onStop() {
           ...    
           super.onStop();
       }
   }   // 调用视频播放类进行播放
   String filePath = "/data/video/files/data/festival.mp4";
   VideoPlay videoPlay = new VideoPlay()
   videoPlay.setSourcePlay(filePath);
   videoPlay.startPlay();