【问题标题】:Playing audio file for specific time播放特定时间的音频文件
【发布时间】:2012-03-14 04:36:28
【问题描述】:

伙计们,我可以使用 Mediaplayer 播放音频文件。但我需要在特定时间播放音频文件。

例如:将 audio1.mp3 播放到 5 分钟。 音频文件只有 10 秒。但它应该一直播放到 5 分钟。

【问题讨论】:

    标签: android media-player soundpool


    【解决方案1】:

    试试这个

     mediaplayerplayer.setLooping(true);<--- this lets the audio to loop...
    

    这是倒计时

    MyCount counter;
    Long s1;
    
    counter= new MyCount(300000,1000);
    counter.start();
    
    public void asdf(View v){  <---- method for onclick of buttons pause and resuming timer
        switch(v.getId()){
            case R.id.button1:<-- for pause
                counter.cancel();
                break;
            case R.id.button2:<--- for resume
                counter= new MyCount(s1,1000);
                counter.start();
        }
    }
    
    public class MyCount extends CountDownTimer {
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
    
        @Override
        public void onFinish() {
            mediaplayer.stop();
            mediaplayer.release();
        }
    
        @Override
        public void onTick(long millisUntilFinished) {
            s1=millisUntilFinished;
        }
    }
    

    这还可以帮助您暂停媒体播放器...以及计时器...并让您将歌曲播放完整 5 分钟

    【讨论】:

    • counter= new MyCount(300000,1000);计数器.start();这是什么,请解释一下
    • 这是 60(秒)*5(分钟)*1000 .. 因为该值必须以毫秒为单位...计数器从该值开始到 1 秒...倒计时计时器..从 5 分钟..
    • 但是我要从哪里开始 mediaplayer.start();
    • 你有你的 mediaplyer,start();在你的代码中......所以把“counter = new MyCount(300000,1000); counter.start();”..在那一行之后......
    • setContentView(R.layout.main);mediaplyer,start();counter= new MyCount(300000,1000);计数器.start();好吗
    【解决方案2】:
       //decleration 
    MediaPlayer mp;
    //specify the path of media file
    String filepath="?";
    
    //onCreate
      mp=new MediaPlayer();
    Uri myUri = Uri.parse(filepath);
    
                   mp.setLooping(true);   
               mp = MediaPlayer.create(ActivityName.this, myUri);
               mp.start(); 
     new CountDownTimer(300000,1000) {
    
                @Override
                public void onTick(long millisUntilFinished) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void onFinish() {
                    // TODO Auto-generated method stub
    if(mp.isPlaying())
                {
                    mp.stop();
                }
    
                }
            }.start();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-05
      相关资源
      最近更新 更多