您的位置:首页 > 编程语言

几行代码的播放器源代码――是真的能播放的

2011-03-30 21:52 281 查看
大家好,不好意思,我打包的时候,由于文件太大了,所以把MP3文件删除掉了,大家只要把任何一个MP3文件命名为ask放到raw里面就可以了。记得哦

各位朋友,来看看, 这个超简单的播放器,真是几行,太有意思了,哈哈

谁看了都懂得操作的按钮,看图





package com.smart;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.Bundle;

public class Main extends  Service {
private MediaPlayer player;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
//开始播放,问这首哥
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
player = MediaPlayer.create(this, R.raw.ask);
player.start();
}
//停止播放
public void onDestroy() {
super.onDestroy();
player.stop();
}
}

package com.smart;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ServiceDemo extends Activity {

//得到配置文件
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.submit);
button1.setOnClickListener(startIt);
Button button2 = (Button)findViewById(R.id.stop);
button2.setOnClickListener(stopIt);
}

private OnClickListener startIt = new OnClickListener()
{
public void onClick(View v)
{            //调用音频
startService(new Intent("com.liangshan.wuyong.START_AUDIO_SERVICE"));
}
};

private OnClickListener stopIt = new OnClickListener()
{
public void onClick(View v)
{
stopService(new Intent("com.liangshan.wuyong.START_AUDIO_SERVICE"));
finish();       //关闭
}
};

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>

<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开始播放"
/>

<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="关闭播放器"
/>
</LinearLayout>


本文出自 “技术成就梦想” 博客,请务必保留此出处http://llb988.blog.51cto.com/1940549/531141
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐