zoukankan      html  css  js  c++  java
  • Android自用 MediaPlayer 播放

    Understanding the MedaiPlayer Oddities:

    1. Once you set the data source of a MediaPlayer, you cannot easily change it to another 

    one - you'll have to create a new MediaPlayer or call the reset() method to reinitialize the state of the player.

    2. After you call prepare(), you can call getCurrentPosition(), getDuration(), and isPlaying() to get the current state of the player. You can also call the setLooping() and setVolume() methods after the call to prepare().

    3. After you call start(), you can call pause(), stop(), and seekTo().

    4.Every MediaPlayer creates a new  thread, so be sure to call the release() method when 

    you are done with the media player, The VideoView takes care of this in the case of video playback, but you'll have to do it manually if you decide to use MediaPlayer instead of VideoView 

    // 方法1 : 使用mediaplayer播放本地raw/下的audio文件的
     private void playLocalAudio() throws Exception {
      MediaPlayer player = MediaPlayer.create(context, R.raw.music_file);
      player.start();
     }
     
     // 方法2 : 使用mediaplayer播放本地raw/下的audio文件的(包括 setDataSource())
     private void playLocalAudio_usingDescriptor() throw Exception {
      AssetFileDescriptor fileDesc = this.getResources()
          .openRawResourceFd(R.raw.music_file);
      MediaPlayer player = null;
      if (fileDesc != null) {
       player = new MediaPlayer();
       player.setDataSource(fileDesc, fileDesc.getStartOffset(),
         fileDesc.getLength());
       fileDesc.close();
       player.prepare();
       player.start();
      }
     }

  • 相关阅读:
    为什么要使用MQ消息中间件?
    趣图:这是招聘超神级别的程序员?
    基于redis的分布式锁的分析与实践
    我的java问题排查工具单
    趣图:看到这些窗户,我想插上几根网线
    SpringBoot+MyBatis+MySQL读写分离(实例)
    IntelliJ IDEA 常用快捷键
    ex3 多分类和神经网络
    9、神经网络的学习
    神经网络--反向传播详细推导过程
  • 原文地址:https://www.cnblogs.com/oakpip/p/1990249.html
Copyright © 2011-2022 走看看