带定时器的android媒体播放器服务

时间:2013-10-16 00:15:50

标签: android audio service

我无法在任何地方找到一个可靠的答案,所以我希望有人可以帮我一些代码。我有一个应用程序让用户从字符串中选择一个时间,然后点击播放按钮。然后这首歌播放了那段时间然后停止。问题是我在活动中设置了这个,而不是服务。我找到了如何使用服务播放和停止歌曲的示例,但我还没有找到如何做到这一点,并且仍然能够让用户能够选择歌曲的时间。有人可以给我一些代码片段,告诉我如何能够实现服务而不会失去选择时间长度的能力?

1 个答案:

答案 0 :(得分:0)

使用可以使用它来设置媒体播放器播放的时间

                new CountDownTimer(1000 * lengthOfTime, 1000) {

                    public void onTick(long millisUntilFinished) {
                    }

                    public void onFinish() {

                        if(yourMediaPlayer.isPlaying()){
                            yourMediaPlayer.stop();
                            yourMediaPlayer.release();
                       }
                           }
                    }
                    }.start();

创建您的服务并使用

进行调用
Intent it = new Intent();
it.setAction("your.package.name.whatEverYourServiceName");
startService(it);

然后在你的清单上添加:

 <service
        android:name="your.package.name.whatEverYourServiceName"
        android:label="Your Service Homie"
        android:process=":my_process" >
        <intent-filter>
            <action android:name="your.package.name.whatEverYourServiceName" >
            </action>
        </intent-filter>
    </service>

这是服务样本:

public class whatEverYourServiceName extends Service{


@Override
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);

//Add your code here mate

}


@Override
public void onDestroy(){
    super.onDestroy();

    //Add what do you want to do here when the service is stop like do a backflip

}

@Override
public IBinder onBind(Intent arg0) {

    return null;
}

}

如果你正在使用日食,只需按ctrl + shift + o自动导入包。

相关问题