编写安卓程序结合service播放视频,包括xml界面、activity和seirvice,列出代码
时间: 2024-03-30 07:38:58 浏览: 43
service 应用的例子代码 android编程
4星 · 用户满意度95%
以下是一个基本的安卓程序结合service播放视频的代码示例,包括xml界面、activity和service:
1. 布局文件activity_main.xml:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
```
2. MainActivity.java文件:
```java
public class MainActivity extends AppCompatActivity {
private SurfaceView mSurfaceView;
private MediaPlayer mMediaPlayer;
private Button mPlayButton;
private boolean mIsPlaying;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);
mPlayButton = (Button) findViewById(R.id.play_button);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDisplay(mSurfaceView.getHolder());
mPlayButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mIsPlaying) {
mMediaPlayer.pause();
mPlayButton.setText("Play");
} else {
mMediaPlayer.start();
mPlayButton.setText("Pause");
}
mIsPlaying = !mIsPlaying;
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
mMediaPlayer.release();
}
}
}
```
3. VideoService.java文件:
```java
public class VideoService extends Service implements MediaPlayer.OnPreparedListener {
private static final String TAG = "VideoService";
public static final String ACTION_PLAY = "com.example.videoservice.action.PLAY";
private MediaPlayer mMediaPlayer;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(ACTION_PLAY)) {
String videoUrl = intent.getStringExtra("videoUrl");
playVideo(videoUrl);
}
return super.onStartCommand(intent, flags, startId);
}
private void playVideo(String videoUrl) {
if (mMediaPlayer == null) {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnPreparedListener(this);
} else {
mMediaPlayer.reset();
}
try {
mMediaPlayer.setDataSource(videoUrl);
mMediaPlayer.prepareAsync();
} catch (IOException e) {
Log.e(TAG, "Error while trying to play video", e);
}
}
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
@Override
public void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
mMediaPlayer.release();
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
```
4. 在AndroidManifest.xml文件中注册服务:
```xml
<service android:name=".VideoService" />
```
5. 在MainActivity中启动服务:
```java
Intent intent = new Intent(this, VideoService.class);
intent.setAction(VideoService.ACTION_PLAY);
intent.putExtra("videoUrl", "http://example.com/video.mp4");
startService(intent);
```
阅读全文