service 音乐播放
来源:互联网 发布:一夫多妻体验知乎 编辑:程序博客网 时间:2024/05/16 01:50
1.bofaactivity
package com.example.bkmusic;
import java.io.IOException;
import com.example.bkmusic.R.dimen;
import android.R;
import android.R.drawable;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class AddBKMusic extends Service {
private MediaPlayer mp;
@SuppressWarnings("deprecation")
public void onStart(Intent intent,int startId){
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
try{
mp.start();
}catch(Exception e){
e.printStackTrace();
}
}
});
mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
try{
mp.release();
}catch(Exception e){
e.printStackTrace();
}
return false;
}
});
super.onStart(intent, startId);
}
public void onCreate(){
try{
mp = new MediaPlayer();
mp = MediaPlayer.create(AddBKMusic.this, com.example.bkmusic.R.raw.bk);
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onCreate();
}
public void onDestroy() {
mp.stop();
mp.release();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
2.mainactivity
package com.example.bkmusic;
import android.app.AliasActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AliasActivity {
public Button btu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();
}
private void findView() {
// TODO Auto-generated method stub
btu = (Button) findViewById(R.id.btu);
btu.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,AddBKMusic.class);
startService(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
- 使用Service播放音乐
- 使用service播放音乐
- android 音乐播放service
- Android Service播放音乐
- service 音乐播放
- Service实现音乐播放
- Service后台播放音乐
- Service播放音乐实例
- Service + 音乐播放
- Service后台播放音乐
- Service播放音乐
- Service播放音乐
- android 之 Service 播放音乐
- android-service音乐播放器
- Service——音乐播放
- 简单Service后台音乐播放
- Service (bindService音乐播放器)
- 在Service中播放音乐
- chown与chomd区别
- OpenDaylight环境搭建指南
- MongoDB性能——优化
- C++ && 汇编
- 科技公司与用户隐私的“拔河”战争
- service 音乐播放
- shell学习三十六天----命令替换
- mysql relay-log.info 丢失问题
- Wow64(32位进程)注入DLL到64位进程
- 二叉树前序、中序、后序遍历相互求法
- Java并发的四种风味:Thread、Executor、ForkJoin和Actor
- POJ之路13-POJ2388
- BeanUtils.copyProperties比较
- 学会JAVA可变长度的参数(Varargs)